我对Spring MVC很陌生,所以请对我很轻松。
我很难理解如何在Spring MVC中实现以下要求:
userList.jsp
<spring:url value="strFormAction" var="/rest/security/user/list" />
<form:form id="userListForm" method="GET" action="${strFormAction}" modelAttribute="user">
<form:select id="strRegionId" path="${strRegionId}" cssClass="form-control" onchange="updateUsersList('1');">
<spring:message var="strSelectRegionLabel" code="select.region" />
<form:option value="0" label="${strSelectRegionLabel}" />
<form:options items="${regions}" itemValue="intId" itemLabel="strNameFr" />
</form:select>
<form:select id="strArchived" path="${strArchived}" cssClass="form-control">
<spring:message var="strYesLabel" code="yes" />
<form:option value="true" label="${strYesLabel}"/>
<spring:message var="strNoLabel" code="no" />
<form:option value="false" label="${strNoLabel}"/>
</form:select>
<table>
...
<c:forEach items="${users}" var="user">
...rows generated here...
</c:forEach>
...
</table>
</form:form>
UserController.java
@RequestMapping(value = "/list", method = RequestMethod.GET)
public String processUserList( @ModelAttribute("user") User user,
@RequestParam(value = "strRegionId", required = false) String strRegionId,
@RequestParam(value = "strArchived", required = false) String strArchived,
@RequestParam(value = "strSortBy", required = false) String strSortBy,
Model model) {
int intRegionId = strRegionId != null && strRegionId.equals("0") == false ? Integer.valueOf(strRegionId) : 0;
boolean booArchived = strArchived != null && strArchived.length() > 0 ? Boolean.valueOf(strArchived) : false;
int intSortBy = strSortBy != null && strSortBy.length() > 0 ? Integer.valueOf(strSortBy) : 1;
List<Region> regions = this.locationService.lstRegions();
model.addAttribute("strRegionId", String.valueOf(intRegionId));
model.addAttribute("strArchived", String.valueOf(booArchived));
model.addAttribute("strSortBy", String.valueOf(intSortBy));
List<User> users = this.securityService.listUsersByRegionAndArchiveState(intRegionId, booArchived, intSortBy);
model.addAttribute("user", new User());
model.addAttribute("users", users);
model.addAttribute("regions", regions);
return "user/userList";
}
在我没有在表单中提供modelAttribute的情况下,我似乎无法使用Spring表单taglib。然后我从我的控制器中放置了一个虚拟的modelAttribute,但现在我得到了:
javax.servlet.ServletException:javax.servlet.jsp.JspException:org.springframework.beans.NotReadablePropertyException:无效的属性&#39; 0&#39; bean类[spring4base.model.security.User]:Bean属性&#39; 0&#39;不可读或getter方法无效:getter的返回类型是否与setter的参数类型匹配?
正如我之前所说,该页面并不是由任何特定的POJO支持。这是一个搜索页面,必须根据之前选择的过滤器(区域,存档状态)返回用户列表(用户实体bean)。每次更改下拉列表时,表单都必须自行提交(用户选择一个区域,提交在同一个映射上完成,然后用户列表仅重新加载来自该特定区域的用户)。
我来自Struts 1,我们需要为每一页创建ActionForm。从我从文档中读到的内容来看,表格现在不是必需的,所以我真的很期待解决这个问题。
非常感谢任何帮助。
答案 0 :(得分:4)
我只想创建包含搜索条件的辅助类,例如:
public class UserSearchCriteria {
private String regionId;
private Boolean archived;
private String sortBy;
// Getters and setters
}
然后我会像这样修改你的控制器方法(缺少一些代码,但这应该给你一个想法)。
@RequestMapping(value = "/list", method = RequestMethod.GET)
public String processUserList(@ModelAttribute("searchCriteria") UserSearchCriteria userSearchCriteria, Model model) {
// Retrieve users and perform filtering based on search criteria
List<User> users = this.securityService.listUsers(searchCriteria);
model.addAttribute("users", users);
model.addAttribute("regions", regions);
return "user/userList";
}
然后你会使用这样的过滤形式:
<spring:url value="/rest/security/user/list" var="formAction" />
<form:form id="userListForm" method="GET" action="${formAction}" modelAttribute="searchCriteria">
<form:select path="regionId" cssClass="form-control" onchange="updateUsersList('1');">
<spring:message var="strSelectRegionLabel" code="select.region" />
<form:option value="0" label="${strSelectRegionLabel}" />
<form:options items="${regions}" itemValue="intId" itemLabel="strNameFr" />
</form:select>
<form:select path="archived" cssClass="form-control">
<spring:message var="strYesLabel" code="yes" />
<form:option value="true" label="${strYesLabel}"/>
<spring:message var="strNoLabel" code="no" />
<form:option value="false" label="${strNoLabel}"/>
</form:select>
您的代码段中的表单中有多处错误。例如,path
属性接受包含要绑定的属性的名称(或路径)的String,您传递了一些变量。您的value
我认为您已var
和<spring:url>
切换。
尝试一下,这不是完整的解决方案,但希望它会给你一些如何实现这一点的指示。如果您遇到任何问题,请发表评论,我会更新答案。