简体   繁体   中英

Spring MVC moving to next record in list

I would like to move to the next record in a List that i am returning to a View. Under is an example of what i am trying to achieve i would like to return the second and third record in this list however i would like to return one record at a time from the list:

This returns the last record in the List to the view:

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

Using the under code does not compile: I get the following message 'The method addAllAttributes(Collection) in the type Model is not applicable for the arguments (Citizens)'

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

Jsp - just an example of the items on the form

<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>

We can only assume that getListOfCitizens(citizen) returns a Collection of Citizens . Therefore, getListOfCitizens(citizen).get(2) returns just a Citizens .

The method Model#addAllAttributes() is expecting a Collection and you are giving it a Citizens .

If you add the whole collection to your model, you can then get whichever part of it you want in your view. You might want to use another method of model, Model#addAttribute(String, Object) as so

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

In your view, just use it reference it, depending on your view technology, as ${citizens} . You can even loop through it or get specific elements at index positions. A quick search of this site or Google will show you how.

<form>
    <c:forEach items="${citizens}" var="element">
        The element value is ${element} <br/>
        <!-- put some inputs here with ${element} -->
    </c:forEach>
<form>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM