You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring MVC同一控制器内方法调用失败,求正确实现方式

Hey Sachin, let's figure out why your method call isn't working and fix it up!

First, let's diagnose the core issue: Your getPaymentDetails method uses @PathVariable to fetch the msg value, but when you do redirect:/getPaymentDetails?msg=msg, you're passing msg as a request parameter (not a path variable). That's why Spring can't bind the value correctly, leading to the failed call.

Here are a few solid ways to achieve what you want:

1. Switch to @RequestParam (keep the redirect approach)

Modify the getPaymentDetails method to accept msg as a request parameter, which matches how you're passing it in the redirect URL:

@RequestMapping(value="/getDonationDetails" , method={RequestMethod.GET,RequestMethod.POST})
public String getDonationDetails() throws IOException {
    return "redirect:/getPaymentDetails?msg=hello";
}

@RequestMapping(value="/getPaymentDetails", method={RequestMethod.GET, RequestMethod.POST})
// Replace @PathVariable with @RequestParam
public String getPaymentDetails(@RequestParam String msg){
    System.out.println("message is " + msg);
    return "success";
}

2. Adjust redirect to use path variables (if you need @PathVariable)

If you really want to stick with @PathVariable, update both the redirect URL and the @RequestMapping of getPaymentDetails to include the path variable:

@RequestMapping(value="/getDonationDetails" , method={RequestMethod.GET,RequestMethod.POST})
public String getDonationDetails() throws IOException {
    // Pass msg as a path segment in the redirect URL
    return "redirect:/getPaymentDetails/hello";
}

// Update the mapping to include the {msg} path variable
@RequestMapping(value="/getPaymentDetails/{msg}", method={RequestMethod.GET, RequestMethod.POST})
public String getPaymentDetails(@PathVariable String msg){
    System.out.println("message is " + msg);
    return "success";
}

3. Direct method call (no redirect, better for same-controller logic)

Since both methods live in the same controller, you don't need a redirect at all—just call the method directly. Use Model or ModelMap to pass data between methods instead of URL parameters:

@RequestMapping(value="/getDonationDetails" , method={RequestMethod.GET,RequestMethod.POST})
public String getDonationDetails(Model model) throws IOException {
    // Add data to the model to pass to the target method
    model.addAttribute("msg", "hello");
    // Call getPaymentDetails directly
    return getPaymentDetails(model);
}

@RequestMapping(value="/getPaymentDetails", method={RequestMethod.GET, RequestMethod.POST})
public String getPaymentDetails(@RequestParam(required = false) String msg, Model model){
    // Handle both direct calls (get from model) and URL requests (get from param)
    String message = msg != null ? msg : (String) model.getAttribute("msg");
    System.out.println("message is " + message);
    return "success";
}

Note: If your methods rely on request-specific objects like HttpServletRequest or HttpServletResponse, make sure to pass those along when calling the method directly.

内容的提问来源于stack exchange,提问作者Sachin HR

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 03:14:05