Spring MVC - 多个提交按钮到一个窗体 [英] Spring MVC - Multiple submit button to a Form

查看:227
本文介绍了Spring MVC - 多个提交按钮到一个窗体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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" />

正如你从我的映射中看到的,我依靠使用params来区分什么按钮点击。问题是它在90%的时间工作,但有时我得到以下异常 -

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 ?

谢谢!

推荐答案

为什么不:

<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
    }

} 

这篇关于Spring MVC - 多个提交按钮到一个窗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆