【问题标题】:How do I set the selected value in a Spring MVC form:select from the controller?如何在 Spring MVC 表单中设置选定值:从控制器中选择?
【发布时间】:2011-05-30 22:32:51
【问题描述】:

在我的控制器中:

@Controller
public class UserController {

    @RequestMapping(value="/admin/user/id/{id}/update", method=RequestMethod.GET)
    public ModelAndView updateUserHandler(@ModelAttribute("userForm") UserForm userForm, @PathVariable String id) {

        Map<String, Object> model = new HashMap<String, Object>();
        userForm.setCompanyName("The Selected Company");
        model.put("userForm", userForm);

        List<String> companyNames = new ArrayList<String>();
        companyNames.add("First Company Name");
        companyNames.add("The Selected Company");
        companyNames.add("Last Company Name");

        model.put("companyNames", companyNames);

        Map<String, Map<String, Object>> modelForView = new HashMap<String, Map<String, Object>>();
        modelForView.put("vars", model);

        return new ModelAndView("/admin/user/update", modelForView);
    }
}

我视图中的选择表单域:

<form:form method="post" action="/admin/user/update.html" modelAttribute="userForm">
<form:select path="companyName" id="companyName" items="${vars.companyNames}" itemValue="id" itemLabel="companyName" />
</form:form>

据我了解,表单支持 bean 将根据表单中的 modelAttribute 属性进行映射。我显然在这里遗漏了一些东西。

【问题讨论】:

  • 看来问题与我的设置无关。问题是 itemValue 设置为公司 id 属性,而对我的表单支持 bean 上的公司名称属性进行了比较。所以两者不相等,因此没有设置为选中的项目。

标签: spring forms spring-mvc


【解决方案1】:

看来问题与我的设置无关。问题是 itemValue 设置为公司 id 属性,而对我的表单支持 bean 上的公司名称属性进行了比较。所以两者不相等,因此没有设置为选中的项目。

上面的代码工作得很好,只要在项目集合中的一个项目的值等于表单,在 userForm 中为特定属性设置值就会将该值设置为在选择表单字段中选择的值价值。我将表单字段更改为如下所示,它提取的是 companyName 而不是 id。

<form:form method="post" action="/admin/user/update.html" modelAttribute="userForm">
<form:select path="companyName" id="companyName" items="${vars.companyNames}" itemValue="companyName" itemLabel="companyName" />
</form:form>

【讨论】:

  • 请注意,作者写的“比较”是表单支持bean上的“等于”方法。
【解决方案2】:

最简单的解决方案是覆盖模型类中的 toString() 方法。 在这种情况下,只需通过覆盖 toString() 来更改类 UserForm,如下所示:

@Override
public String toString() {
    return this.getCompanyName();
}

Spring 会自动在 form:option 中选择正确的值

【讨论】:

    【解决方案3】:

    我在同一个问题上苦苦挣扎了一段时间。 这是我的选择字段

    <form:select path="origin" items="${origins}" itemValue="idOrigin" itemLabel="name" />
    

    因为我的实体有一个 PropertyEditor,所以我不能写类似的东西

    <form:select path="origin.idOrigin" items="${origins}" itemValue="idOrigin" itemLabel="name" />
    

    效果很好,但没有被 PropertyEditor 解析。

    因此,考虑到 Spring 需要确定实体之间的相等性,我只使用 idOrigin 属性在我的 Origin 实体中实现了 equals 和 hashcode,并且它起作用了!

    【讨论】:

      【解决方案4】:

      你也可以这样试试

      <form:select id="selectCategoryId" path="categoryId"
            class="selectList adminInput admin-align-input" multiple="">
            <option value="0">-- Select --</option>
            <c:forEach items="${categories}" var="category">
                  <option <c:if test="${category.key eq workflowDTO.categoryId}">selected="selected"</c:if>    value="${category.key}">${category.value} </option>
              </c:forEach>
      </form:select>
      

      【讨论】:

        【解决方案5】:

        没那么复杂。您需要 2 个 bean:一个表单支持 bean 和一个域模型中的选择模型。

        这是我的模型,一个字符串列表,用于:

        /* in controller: my select model is a list of strings. However, it can be more complicated, then you had to use PropertyEditors for String <-> Bean conversions */
        
            List<String> mySelectValues = new ArrayList<String>();
            mySelectValues.add("M");
            mySelectValues.add("F");
            modelMap.addAttribute("mySelectValues", mySelectValues);
        

        这是你的表格,基本上:

        <form:form command="user">
          <form:select path="gender">
             <form:options items="${mySelectValues}"></form:options>                                                                                                       
          </form:select>    
        </form:form>
        

        这是我的支持对象:

        public class User {
            private String gender;
            /* accessors */
        }
        

        Spring 框架使用“性别”字段的值自动选择。

        【讨论】:

        • 你能包含完整的控制器吗?这可能会给我更多的洞察力,因为我不确定我是否理解。另外,是否有必要在表单标签上使用命令属性?我目前正在使用 modelAttribute。
        • 如果我想为自定义对象而不是String实现相同的目标需要做什么?
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-10
        • 2018-04-11
        • 2014-06-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多