java 在一个表单中有多个提交按钮并确定在控制器中按下了哪个按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2025280/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 18:59:10  来源:igfitidea点击:

Have multiple submit buttons in a form and determine which was pressed in a controller

javaspringjspjstl

提问by David Buckley

In my Spring application, I have a jsp that has a form where I want to have multiple submit buttons that go to the same controller. I need to be able to determine which button was pressed in the controller. The form displays a number of items to the user and they can select one of the items with the only difference being the ID of the selected item.

在我的 Spring 应用程序中,我有一个 jsp,它有一个表单,我希望将多个提交按钮发送到同一个控制器。我需要能够确定在控制器中按下了哪个按钮。该表单向用户显示了许多项目,他们可以选择其中一个项目,唯一的区别是所选项目的 ID。

In the jsp, I'm creating the form like this:

在jsp中,我正在创建这样的表单:

<form:form method="post" commandName="myCommand">

    <c:forEach items="${model.availableitems}" var="item">
      <span class="item">${item.description}</span> 
      <input type="hidden" name="id" value="${item.ID}"/>
      <input type="submit" name="SelectButton" value="Select" />
    </c:forEach>

  </div> 
</form:form>

However, this gives me a "Data binding errors: 1" message in the log and the form isn't submitted to the controller.

但是,这在日志中给了我一个“数据绑定错误:1”消息,并且表单没有提交给控制器。

I tried changing the myCommand.id from an int to a String, but then the value when it's submitted is id1,id2, id3... (all of the IDs in a comma delimited list) with no way of determining which button was pressed. I don't want to have to specify different actions for each button as the number of items can grow, and the action for them is all the same, just with a different ID.

我尝试将 myCommand.id 从 int 更改为 String,但是提交时的值是 id1,id2, id3...(逗号分隔列表中的所有 ID),无法确定按下了哪个按钮. 我不想为每个按钮指定不同的动作,因为项目的数量会增加,而且它们的动作都是一样的,只是 ID 不同。

How can I have multiple buttons in this form and get the value in the controller?

如何在这种形式中有多个按钮并在控制器中获取值?

回答by Kaleb Brasee

Why not just use a separate form for each item?

为什么不为每个项目使用单独的表格?

<c:forEach items="${model.availableitems}" var="item">
  <form:form method="post" commandName="myCommand">
    <span class="item">${item.description}</span> 
    <input type="hidden" name="id" value="${item.ID}"/>
    <input type="submit" name="SelectButton" value="Select" />
  </form:form>        
</c:forEach>

回答by MoreThanChaos

instead of using submit button use a button which calls function which sets the value of hidden field and calls form.submit()

而不是使用提交按钮使用一个按钮调用函数,该函数设置隐藏字段的值并调用 form.submit()

you can generate buttons using loop

您可以使用循环生成按钮

回答by calvin

You have 3 options -
1. Use multiple forms and have a submit button in each form as suggested above. The generated code wouldn't be very pretty to look at and it's usually considered bad practice to have multiple forms in a page.
2. Use a Javascript hack to set a hidden variable when you press the submit button.
3. If you have to handle fallback (without JavaScript), then there's a roundabout way to find out which button was clicked. Here's an example -

您有 3 个选项 -
1. 使用多个表单,并按照上面的建议在每个表单中都有一个提交按钮。生成的代码看起来不是很漂亮,在一个页面中有多个表单通常被认为是不好的做法。
2. 当您按下提交按钮时,使用 Javascript hack 设置隐藏变量。
3. 如果您必须处理回退(不使用 JavaScript),那么有一种迂回的方法可以找出点击了哪个按钮。这是一个例子——

<form:form method="post" commandName="myCommand">            
    <c:forEach items="${model.availableitems}" var="item">            
        <span class="item">${item.description}</span>             
        <input type="submit" name="${item.ID}" value="Select" />            
    </c:forEach>
</form:form>                    

This set a unique id to each button. Since the request params go as key value pairs, we'll have to do a reverse lookup in this case as we have the value in the key position. Here's the java side of it.. not sure if this is the most efficient way to do it but it works.

这为每个按钮设置了一个唯一的 id。由于请求参数是键值对,在这种情况下我们必须进行反向查找,因为我们在键位置有值。这是它的 java 方面.. 不确定这是否是最有效的方法,但它有效。

String searchButtonName(final HttpRequest request) { 
    String buttonName = ""; 
    Map<String, String[]> paramMap = request.getParameterMap(); 
    if (MapUtils.isNotEmpty(paramMap)) { 
            for (Map.Entry<String, String[]> entry : paramMap.entrySet()) {
                    /* Search for the button name as given in 
                       the 'value' attribute for the input tag */
                    if ("Select".equals(entry.getValue()[0])) { 
                            buttonName = entry.getKey(); 
                            break; 
                    } 
            } 
    } 
    return buttonName; 
} 

回答by entrix

You can use annotation parameter @RequestParam("SelectButton") String submitin argument list of your controller.

您可以@RequestParam("SelectButton") String submit在控制器的参数列表中使用注释参数。