Spring MVC - 表单的多个提交按钮

发布于 2024-12-28 03:39:07 字数 1152 浏览 1 评论 0原文

我试图将 2 个提交按钮发布到表单,每个按钮操作映射到不同的控制器。这是我的映射

@RequestMapping(value="/save", method=RequestMethod.POST, params="save")
@RequestMapping(value="/save", method=RequestMethod.POST, params="renew")

,我的提交按钮如下所示 -

<input type="submit" name="save" class="button" value="Save" />
<input type="submit" name="renew" class="button" value="Renew" />

正如您从我的映射中看到的,我依靠使用参数来区分单击了哪个按钮。问题是它在 90% 的情况下都能正常工作,但有时我会遇到以下异常 -

java.lang.IllegalStateException: Ambiguous handler methods mapped for HTTP path 'http://localhost:8090/myapp/save': {public java.lang.String com.myapp.SaveController.save(MyEntity,javax.servlet.http.HttpSession), public java.lang.String com.myapp.SaveController.saveAndRenew(MyEntity,javax.servlet.http.HttpSession)}
org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.lookupHandlerMethod(AbstractHandlerMethodMapping.java:248)
org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.getHandlerInternal(AbstractHandlerMethodMapping.java:194)

奇怪的是,当发生这种情况并且我重新提交页面时,之后一切正常。有没有更好的方法来实现我想要做的事情?

谢谢!

I am trying to have 2 submit buttons post to a form, with each button action mapped to different controllers. Here are my mappings

@RequestMapping(value="/save", method=RequestMethod.POST, params="save")
@RequestMapping(value="/save", method=RequestMethod.POST, params="renew")

And my submit buttons look like these -

<input type="submit" name="save" class="button" value="Save" />
<input type="submit" name="renew" class="button" value="Renew" />

As you can see from my mapping, I am relying on the use of params to differentiate what button was clicked on. The problem is that it works 90% of the time but sometimes I get the exception below -

java.lang.IllegalStateException: Ambiguous handler methods mapped for HTTP path 'http://localhost:8090/myapp/save': {public java.lang.String com.myapp.SaveController.save(MyEntity,javax.servlet.http.HttpSession), public java.lang.String com.myapp.SaveController.saveAndRenew(MyEntity,javax.servlet.http.HttpSession)}
org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.lookupHandlerMethod(AbstractHandlerMethodMapping.java:248)
org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.getHandlerInternal(AbstractHandlerMethodMapping.java:194)

Strangely, when this happens and I re-submit the page, everything works fine afterwards. Is there a better way to achieve what I'm trying to do ?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

一抹微笑 2025-01-04 03:39:07

如果表单指定了这些按钮:

input type="submit" class="button" name="save" value="Save"
input type="submit" class="button" name="delete" value="Delete"
input type="submit" class="button" name="cancel" value="Cancel"

您可以根据用一个控制器按下的按钮定向到不同的 url 请求。

对于取消按钮,

@RequestMapping(params = "cancel", method = RequestMethod.POST)
public String cancelUpdateUser(HttpServletRequest request) {
    return "redirect:/users.html";
}

请求映射的作用是扫描 post 请求,如果它包含参数 name = cancel。

对于保存按钮,

@RequestMapping(params = "save", method = RequestMethod.POST)
public String saveUser(HttpServletRequest request, @ModelAttribute User user, BindingResult result, SessionStatus status) {
    // validate your result
    // if no errors, save it and redirect to successView.
}

if the form has these buttons specified:

input type="submit" class="button" name="save" value="Save"
input type="submit" class="button" name="delete" value="Delete"
input type="submit" class="button" name="cancel" value="Cancel"

you may direct to different url request according to button pressed with one controller.

for cancel button,

@RequestMapping(params = "cancel", method = RequestMethod.POST)
public String cancelUpdateUser(HttpServletRequest request) {
    return "redirect:/users.html";
}

what request mapping does is to scan post request if it contains params name = cancel.

for save button,

@RequestMapping(params = "save", method = RequestMethod.POST)
public String saveUser(HttpServletRequest request, @ModelAttribute User user, BindingResult result, SessionStatus status) {
    // validate your result
    // if no errors, save it and redirect to successView.
}
温折酒 2025-01-04 03:39:07

为什么不:

<input type="submit" name="action" value="save" />

然后:

@RequestMapping(value="/save", method=RequestMethod.POST)
public String handlePost(@RequestParam String action){

    if( action.equals("save") ){
       //handle save
    }
    else if( action.equals("renew") ){
       //handle renew
    }

} 

Why not:

<input type="submit" name="action" value="save" />

and then:

@RequestMapping(value="/save", method=RequestMethod.POST)
public String handlePost(@RequestParam String action){

    if( action.equals("save") ){
       //handle save
    }
    else if( action.equals("renew") ){
       //handle renew
    }

} 
━╋う一瞬間旳綻放 2025-01-04 03:39:07

如果您有多个具有相同 @RequestMapping 的控制器方法,仅在 params 属性上有所不同,则必须显式编写:

  • 哪个参数应该出现在请求中,例如params="save"
  • 哪个参数不应该出现在请求中,例如 params="!save"

在您的情况下:

@RequestMapping(value="/save", method=RequestMethod.POST, params={"save", "!renew"})
@RequestMapping(value="/save", method=RequestMethod.POST, params={"renew", "!save"})

这应该修复错误不明确映射到 HTTP 路径的处理程序方法...

请参阅 Spring Web API 4.0.x - RequestMapping#params

If You have more controller methods with the same @RequestMapping that differs only in params attribute, You have to explicitly write:

  • which parameter is supposed to be present in the request, e.g. params="save"
  • which parameter is NOT supposed to be present in the request, e.g. params="!save"

In Your case:

@RequestMapping(value="/save", method=RequestMethod.POST, params={"save", "!renew"})
@RequestMapping(value="/save", method=RequestMethod.POST, params={"renew", "!save"})

This should fix error Ambiguous handler methods mapped for HTTP path ...

See Spring Web API 4.0.x - RequestMapping#params

橪书 2025-01-04 03:39:07

只需使用与此类似的方法创建一个控制器

@RequestMapping(value="/save", method=RequestMethod.POST)
public String handlePost(@RequestParam(required=false , value = "save") String saveFlag , @RequestParam(required=false , value = "renew") String renewFlag){

if(saveFlag != null{
   //handle save
}
else if(renewFlag !=null{
   //handle renew
}

} 

Just create one controller with a method similar to this

@RequestMapping(value="/save", method=RequestMethod.POST)
public String handlePost(@RequestParam(required=false , value = "save") String saveFlag , @RequestParam(required=false , value = "renew") String renewFlag){

if(saveFlag != null{
   //handle save
}
else if(renewFlag !=null{
   //handle renew
}

} 
囍孤女 2025-01-04 03:39:07

另一种解决方案:

@RequestMapping(value="/save", method={RequestMethod.POST}, params={"save=Save"})
@RequestMapping(value="/save", method={RequestMethod.POST}, params={"renew=Renew"})

One more solution:

@RequestMapping(value="/save", method={RequestMethod.POST}, params={"save=Save"})
@RequestMapping(value="/save", method={RequestMethod.POST}, params={"renew=Renew"})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文