我在 Spring MVC 中遇到问题。在表单中选择某些内容并提交表单效果很好,为非 boolean 值选择默认值也可以,但我在选择 boolean 值的默认值时遇到问题。
我的 jsp 文件看起来像这样:
<form:form commandName="filterData" id="user_filter_form" action="${listUrl}" method="POST">
<form:label path="active">Active</form:label><br/>
<form:select path="active">
<option value="">--</option>
<option value="true">Yes</option>
<option value="false">No</option>
</form:select>
<form:label path="email">E-mail</form:label><br/>
<form:input path="email" type="search"/>
<button class="b not-ui" type="submit" style="margin-right: 25px">Search</button>
</form:form>
模型对象是这样的:
public class UserFilterData {
private Boolean active;
private String email;
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Boolean getActive() {
return active;
}
public void setActive(Boolean active) {
this.active = active;
}
}
正如我所说,提交工作正常,但是当我像这样创建 Controller 时,“电子邮件”字段填充为“test@example.com”,但选择框选择了第一个选项“---”
@RequestMapping(value = "/filter")
public String filterAction(Model model) {
UserFilterData data = new UserFilterData();
data.setEmail("test@example.com");
data.setActive(Boolean.TRUE);
model.addAttribute("filterData", data);
return "users/index;
}
我还尝试将 jsp 中的“true”更改为 ${true} 或 TRUE 或 1,但没有成功。如果我使用 ${filterData.active}
在 jsp 中回显 filterData,结果为 true
,但未选择适当的选项。有人知道我做错了什么吗?
最佳答案
您可能需要使用表单选项标签。
<form:option value="true" label=""/>
<form:option value="false" label=""/>
关于java - 在 <form :select> in SpringMVC 中选择默认 boolean 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24626118/