需要帮助迭代Spring mvc中列表对象的列表

时间:2014-11-25 10:59:56

标签: java jsp spring-mvc iterator

我搜索了几个例子,但还没有得到。我将GOOD对象​​的List从控制器传递到jsp页面。在这个对象中,我有UserName和FUNCTIONS列表,这是另一个对象。我想遍历GOOD列表并希望显示名称和相应的功能但不能这样做。 我的GOOD豆是:

public List<Function> userFunctionsList = new ArrayList<Function>();
public String userName;

我的ACCESS Bean是:

public List<Good> goodBeanList = new ArrayList<Good>();
public List<Good> getGoodBeanList() {
        return goodBeanList;
    }
    public void setGoodBeanList(
            List<Good> goodBeanList) {
        this.goodBeanList = goodBeanList;
    }

我的控制器类:

    public ModelAndView getUserDetails(PortletRequest request, PortletResponse response)
        {
        List<Good> searchedUsersList = null;
        List<Good> finalList = new ArrayList<ManageCorporateServices>();
        Access accessBean = new Access();
        searchedUsersList = goodService.getSearchedUsers(contractId,userName);

        for(Good goodBean : searchedUsersList)
        {
          userFunctionsList = goodService.getUserFunctions(goodBean.getUserName());
          goodBean.setUserFunctionsList(userFunctionsList);
          finalList.add(goodBean);
        }
    access.setGoodBeanList("finalList");
    modelAndView.adObject("access",access);

在JSP中:

<form:form commandName="access" action="${updateFunctionNew}">
    <c:forEach var="userNamesListValue" items="${access.goodBeanList }">
        <tr>
            <td>
                <a href="${selectedUserName}" title="selectedUserName">${userNamesListValue.userName}</a>
            </td>
            <c:forEach  varStatus = "count" var="userFunctionsList" items="${userNamesListValue.userFunctionsList}">
            <td>${userFunctionsList.functionName} <form:checkbox  path="userFunctionsList[${count.index }].updatedValue" value="${userFunctionsList.updatedValue}" /></td>
            </c:forEach>
            <td><button type="Submit" class="pink">Save</button>
            </td>
        </tr>
    </c:forEach>
</form:form>

我在Access Bean错误中获取userFunctionsList [0]不可读的属性。有人请帮帮我。

1 个答案:

答案 0 :(得分:0)

以下

<form:checkbox  path="userFunctionsList[${count.index }].updatedValue"

附加到命令对象,在您的情况下是 Access 对象。这就是您收到错误的原因,它针对 Access bean搜索userFunctionsList而不是 Good bean

要解决此问题,请在外部循环中添加索引,并更正路径,例如

<c:forEach var="userNamesListValue" items="${access.goodBeanList }" varStatus="accessCount"
  ....
  <form:checkbox  path="goodBeanList[${accessCount.index}].userFunctionsList[${count.index }].updatedValue"