Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: indentation

...

Code Block
xml
xml
title"Using indexed properties in a JSP"
 
<stripes:form action="/bugzooky/EditPeople.action"> 
    <table class="display"> 
        <tr> 
            <th>ID</th> 
            <th>Username</th> 
            <th>First Name</th> 
            <th>Last Name</th> 
            <th>Email</th> 
        </tr> 
        <c:forEach items="${personManager.allPeople}" var="person" varStatus="loop"> 
            <tr> 
                <td> 
                    ${person.id} 
                    <stripes:hidden name="people[${loop.index}].id" value="${person.id}"/> 
                </td> 
<td><stripes                <td>
                    <stripes:text name="people[${loop.index}].username" value="${person.username}"/></td> 
<td><stripes>
                </td> 
                <td>
                    <stripes:text name="people[${loop.index}].firstName" value="${person.firstName}"/></td> 
<td><stripes>
                </td> 
                <td>
                    <stripes:text name="people[${loop.index}].lastName" value="${person.lastName}"/></td> 
<td><stripes>
                </td> 
                <td>
                    <stripes:text name="people[${loop.index}].email" value="${person.email}"/></td>>
                </td> 
            </tr> 
            <c:set var="newIndex" value="${loop.index + 1}" scope="page"/>
        </c:forEach> 
        <%-- And now, an empty row, to allow the adding of new users. --%> 
        <tr> 
            <td></td> 
            <td></td> 
<td><stripes 
            <td>
                <stripes:text name="people[${newIndex}].username"/></td> 
<td><stripes>
            </td> 
            <td>
                <stripes:text name="people[${newIndex}].firstName"/></td> 
<td><stripes>
            </td> 
            <td>
                <stripes:text name="people[${newIndex}].lastName"/><>
            </td> 
<td><stripes            <td>
                <stripes:text name="people[${newIndex}].email"/><>
            </td> 
        </tr> 
    </table> 

    <div class="buttons"><stripes>
        <stripes:submit name="Save" value="Save Changes"/><>
    </div> 
</stripes:form> 

It's pretty easy with EL. Using the c:forEach tag the varStatus (which contains the index of the current iteration) is assigned to the name loop. Then, in the form fields the loop.index is inserted into the form field name using EL. E.g. people[${loop.index}].username will translate at runtime into people[0].username, people[1].username etc.

...

Code Block
title"Using indexed properties in an ActionBean"
 
private List<Person> people; 

@ValidateNestedProperties ({ 
    @Validate(field="username", required=true, minlength=3, maxlength=15), 
    @Validate(field="firstName", required=true, maxlength=25), 
    @Validate(field="lastName", required=true, maxlength=25), 
    @Validate(field="email", mask="[\\w\\.]+@[\\w\\.]+\\.\\w+") 
}) 
public List<Person> getPeople() { return people; } 
public void setPeople(List<Person> people) { this.people = people; } 

...

Code Block
titleSpecifying validations of indexed properties
 
@ValidateNestedProperties({ 
    @Validate(field="phoneNumber", required=true), 
    @Validate(field="pets.age", required=true, maxvalue=100), 
    @Validate(field="pets.nicknames.name", required=true, maxlength=50), 
}) 
private Person person; 

...

Code Block
xml
xml
titleMap/Indexed properties with String keys in the JSP
 
<stripes:form ... > 
    ... 
    <table> 
        <c:forEach items="${toolParams}" var="toolParam"> 
            <tr> 
                <td>${toolParam.name}:</td> 
                <td><stripes:text name="toolParameters['${toolParam.name}']"/></td> 
            </tr> 
        </c:forEach> 
    </table> 
    ... 
</stripes:form> 

The relevant section of the ActionBean:

Code Block
titleMap/Indexed properties with String keys in the ActionBean
 
private Map<String,Double> toolParameters; 

public Map<String,Double> getToolParameters() { return toolParameters; } 
public void setToolParameters(Map<String,Double> toolParameters) { 
    this.toolParameters = toolParameters; 
} 

...

The last point is worth explaining in more detail. What is means is that you can have ActionBean properties that looks like this:

Code Block
 
public Map<Date,List<Appointment>> getAppoinments() { return appointments; } 
public void setAppoinments(Map<Date,List<Appointment>> appointments) { 
    this.appointments = appointments; 
} 

and have input fields that look like this:

Code Block
xml
xml
 
Note: <stripes:text name="appointments[${date}][${idx}].note"/>