Spring MVC表单没有模型对象支持

Cha*_*rin 6 spring spring-mvc

我对Spring MVC很新,所以请对我很轻松.

我很难理解如何在Spring MVC中实现以下要求:

  • JSP列表表单,从数据库列出用户(服务,存储库的东西都正常工作).
  • 表单不受模型属性对象的支持.这是一个列表/查找表单!
  • 我需要列出符合几个"过滤器"字段的标准的用户,例如:
    • 地区(下拉列表)
    • 用户是否已存档?(是/否下拉列表)

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>
Run Code Online (Sandbox Code Playgroud)

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";
}
Run Code Online (Sandbox Code Playgroud)

在我没有在表单中提供modelAttribute的情况下,似乎根本无法使用Spring表单taglib.然后我从我的控制器中放置了一个虚拟的modelAttribute,但现在我得到:

javax.servlet.ServletException:javax.servlet.jsp.JspException:org.springframework.beans.NotReadablePropertyException:bean类的属性'0'无效[spring4base.model.security.User]:Bean属性'0'不可读或没有无效的getter方法:getter的返回类型是否与setter的参数类型匹配?

正如我之前所说,该页面并不是由任何特定的POJO支持.这是一个搜索页面,必须根据之前选择的过滤器(区域,存档状态)返回用户列表(用户实体bean).每次更改下拉列表时,表单都必须自行提交(用户选择一个区域,提交在同一个映射上完成,然后用户列表仅重新加载来自该特定区域的用户).

我来自Struts 1,我们需要为每一页创建ActionForm.从我从文档中读到的内容来看,这些天不需要表单,所以我真的很期待解决这个问题.

任何帮助将不胜感激.

Boh*_*rdt 4

我只是创建包含您的搜索条件的辅助类,例如:

public class UserSearchCriteria {
    private String regionId;
    private Boolean archived;
    private String sortBy;

    // Getters and setters
}
Run Code Online (Sandbox Code Playgroud)

然后我会像这样修改你的控制器方法(缺少一些代码,但这应该给你这个想法)。

@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";
}
Run Code Online (Sandbox Code Playgroud)

然后你可以像这样使用你的过滤表单:

<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>
Run Code Online (Sandbox Code Playgroud)

您的代码片段中的表单存在多个错误。例如,path属性采用包含要绑定的属性名称(或路径)的字符串,您向其传递了一些变量。我想你也已经value切换var了。<spring:url>

尝试一下,它不是完整的解决方案,但希望它能为您提供一些如何实现这一点的指导。如果您遇到任何问题,请发表评论,我会更新答案。