0

我想移动到要返回视图的列表中的下一条记录。下面是我试图实现的一个示例,我想返回此列表中的第二条和第三条记录,但是我想一次从列表中返回一条记录:

这会将 List 中的最后一条记录返回到视图:

 model.addAllAttributes(citizenManager.getListOfCitizens(citizen));

使用下面的代码无法编译:我收到以下消息“模型类型中的方法 addAllAttributes(Collection) 不适用于参数(公民)”

model.addAllAttributes(citizenManager.getListOfCitizens(citizen).get(2));

Jsp - 只是表单上项目的一个示例

<form:form id="citizenRegistration" name ="citizenRegistration" method="POST" commandName="citizens" action="citizen_registration.htm">

<li><form:label for="weight" path="weight">Enter Weight <i>(lbs)</i></form:label>
<form:input path="weight" id="weight" title="Enter Weight"/><form:errors path="weight" class="errors"/>
</li> 
                                                <li><form:label for="height" path="height">Enter Height <i>(feet)</i></form:label>
<form:input path="height" id="height" title="Enter Height"/><form:errors path="height" class="errors"/>
</li> 
                                                <li>
<form:label for="skinColorId" path="skinColorId">Select Skin Color</form:label>     
<form:select path="skinColorId" id="skinColorId" title="Select Skin Color">
<form:options items = "${skinColor.skinColorList}" itemValue="colorCode" itemLabel="colorDesc"/>
</form:select>          
<form:errors path="skinColorId" class="errors"/><label class="colorPreviewer" id="skinColorPreviewer">color previewer</label>
</li>
4

1 回答 1

1

我们只能假设getListOfCitizens(citizen)返回 a Collectionof Citizens。因此,getListOfCitizens(citizen).get(2)只返回一个Citizens.

方法Model#addAllAttributes()期望 aCollection并且您给它 a Citizens

如果将整个集合添加到模型中,则可以在视图中获取所需的任何部分。您可能想使用另一种模型方法Model#addAttribute(String, Object )

model.addAttribute("citizens", citizenManager.getListOfCitizens(citizen));

在您看来,只需使用它来引用它,这取决于您的视图技术,如${citizens}. 您甚至可以遍历它或在索引位置获取特定元素。快速搜索此站点或 Google 将告诉您如何操作。

<form>
    <c:forEach items="${citizens}" var="element">
        The element value is ${element} <br/>
        <!-- put some inputs here with ${element} -->
    </c:forEach>
<form>
于 2013-03-05T02:24:28.460 回答