Spring MVC - 表单的多个提交按钮
我试图将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果表单指定了这些按钮:
您可以根据用一个控制器按下的按钮定向到不同的 url 请求。
对于取消按钮,
请求映射的作用是扫描 post 请求,如果它包含参数 name = cancel。
对于保存按钮,
if the form has these buttons specified:
you may direct to different url request according to button pressed with one controller.
for cancel button,
what request mapping does is to scan post request if it contains params name = cancel.
for save button,
为什么不:
然后:
Why not:
and then:
如果您有多个具有相同
@RequestMapping
的控制器方法,仅在params
属性上有所不同,则必须显式编写:params="save"
params="!save"
在您的情况下:
这应该修复错误不明确映射到 HTTP 路径的处理程序方法...
请参阅 Spring Web API 4.0.x - RequestMapping#params
If You have more controller methods with the same
@RequestMapping
that differs only inparams
attribute, You have to explicitly write:params="save"
params="!save"
In Your case:
This should fix error Ambiguous handler methods mapped for HTTP path ...
See Spring Web API 4.0.x - RequestMapping#params
只需使用与此类似的方法创建一个控制器
Just create one controller with a method similar to this
另一种解决方案:
One more solution: