I am trying to have 2 submit buttons post to a form, with each button action mapped to different controllers. Here are my mappings
我试图将2个提交按钮发布到表单,每个按钮操作映射到不同的控制器。这是我的映射
@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 -
从我的映射中可以看出,我依靠使用params来区分点击的按钮。问题是它有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)
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!
5 个解决方案
#1
35
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.
请求映射的作用是扫描post请求,如果它包含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.
}
#2
31
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
}
}
#3
9
If You have more controller methods with the same @RequestMapping
that differs only in params
attribute, You have to explicitly write:
如果你有更多的控制器方法使用相同的@RequestMapping只在params属性中有所不同,你必须明确地写:
- 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"
假定哪个参数存在于请求中,例如PARAMS = “保存”
哪个参数不应该出现在请求中,例如PARAMS = “!救”
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 ...
这应该修复错误为HTTP路径映射的不明确的处理程序方法...
See Spring Web API 4.0.x - RequestMapping#params
请参阅Spring Web API 4.0.x - RequestMapping #params
#4
6
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
}
}
#5
2
One more solution:
还有一个解决方案
@RequestMapping(value="/save", method={RequestMethod.POST}, params={"save=Save"})
@RequestMapping(value="/save", method={RequestMethod.POST}, params={"renew=Renew"})
#1
35
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.
请求映射的作用是扫描post请求,如果它包含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.
}
#2
31
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
}
}
#3
9
If You have more controller methods with the same @RequestMapping
that differs only in params
attribute, You have to explicitly write:
如果你有更多的控制器方法使用相同的@RequestMapping只在params属性中有所不同,你必须明确地写:
- 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"
假定哪个参数存在于请求中,例如PARAMS = “保存”
哪个参数不应该出现在请求中,例如PARAMS = “!救”
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 ...
这应该修复错误为HTTP路径映射的不明确的处理程序方法...
See Spring Web API 4.0.x - RequestMapping#params
请参阅Spring Web API 4.0.x - RequestMapping #params
#4
6
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
}
}
#5
2
One more solution:
还有一个解决方案
@RequestMapping(value="/save", method={RequestMethod.POST}, params={"save=Save"})
@RequestMapping(value="/save", method={RequestMethod.POST}, params={"renew=Renew"})