如何在 Spring MVC 表单中设置选定的值:从控制器中选择?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6181277/
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
How do I set the selected value in a Spring MVC form:select from the controller?
提问by
In my controller:
在我的控制器中:
@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);
}
}
The select form field in my view:
我视图中的选择表单字段:
<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>
It was my understanding that the form backing bean would be mapped based upon the modelAttribute attribute in the form. I'm obviously missing something here.
我的理解是,表单支持 bean 将根据表单中的 modelAttribute 属性进行映射。我显然在这里遗漏了一些东西。
采纳答案by
It appears the issue was not related to my setup. The problem was that the itemValue was set to the company id property, while the comparison was being done to the company name property on my form backing bean. So the two were not equal, and therefore, no item was set to selected.
看来问题与我的设置无关。问题是 itemValue 被设置为公司 id 属性,而比较是在我的表单支持 bean 上与公司名称属性进行比较。所以两者不相等,因此,没有项目被设置为选择。
The above code works just fine, and setting the value in the userForm for a particular property will set that value as selected in select form fields so long as the value of one of the items in the items collection is equal to the form value. I changed my form field to look like this, which pulls the companyName instead of the id.
上面的代码工作得很好,并且在 userForm 中为特定属性设置值会将该值设置为在选择表单字段中选择的值,只要 items 集合中的一个项目的值等于表单值。我将表单字段更改为如下所示,它提取的是 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>
回答by Ayman Al-Absi
The easiest solution is to override the toString() method in the model class. In this case just change the class UserForm by overriding toString() like below:
最简单的解决方案是覆盖模型类中的 toString() 方法。在这种情况下,只需通过覆盖 toString() 来更改类 UserForm ,如下所示:
@Override
public String toString() {
return this.getCompanyName();
}
Spring then will automatically select the correct value in form:option
Spring 然后会自动在 form:option 中选择正确的值
回答by Dario Zamuner
I was struggling some time on the same issue. This is the select field I had
我在同一个问题上挣扎了一段时间。这是我的选择字段
<form:select path="origin" items="${origins}" itemValue="idOrigin" itemLabel="name" />
Since I had a PropertyEditor in place for my entity I couldn't write something like
因为我的实体有一个 PropertyEditor,所以我不能写类似的东西
<form:select path="origin.idOrigin" items="${origins}" itemValue="idOrigin" itemLabel="name" />
that worked fine, but was not parsed by the PropertyEditor.
效果很好,但没有被 PropertyEditor 解析。
So, thinking about the Spring's need to determine equality between entities, I came out implementing equals and hashcode in my Origin entity using only the idOrigin property, and it worked!
因此,考虑到 Spring 需要确定实体之间的相等性,我仅使用 idOrigin 属性在我的 Origin 实体中实现了 equals 和 hashcode,并且它起作用了!
回答by NIrav Modi
You can also try like this
你也可以这样试试
<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>
回答by Erhan Bagdemir
it is not so complicated. You need 2 beans: a form backing bean and a select model in your domain model.
它没有那么复杂。您需要 2 个 bean:域模型中的一个表单支持 bean 和一个选择模型。
Here is my model, a list of strings, for :
这是我的模型,一个字符串列表,用于:
/* 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);
Here is your form, basically :
这是你的表格,基本上:
<form:form command="user">
<form:select path="gender">
<form:options items="${mySelectValues}"></form:options>
</form:select>
</form:form>
und here is my backing object:
这是我的支持对象:
public class User {
private String gender;
/* accessors */
}
Spring framework selects automaticaly using value of "gender" field.
Spring 框架使用“性别”字段的值自动选择。

