Using stripes indexed properties to populate a map

342 views Asked by At

Hope you can help me find an answer to problem that's been keeping me busy 2 days already.

I'm building an app for making theater-reservations as part of a Java course i've been following.

I'm trying to use the stripes index feature to populate a map object using stripes text input fields.

See the code below.

JSP:

<c:set var="voorstellingen" value="${actionBean.theater.voorstellingen}" />
    <s:form beanclass="theater.action.TheaterActionBean" method="get">            
        <d:table name="${actionBean}">    
            <d:column title="naam">
                Naam:<s:text name="naam" title="Naam" size="30">Voer uw naam in</s:text>
            </d:column>
            <d:column title="telefoon" >    
                Telefoonnummer:<s:text name="telefoon" title="Telefoon"  size="30">Voer uw telefoonnummer in</s:text>
            </d:column>
        </d:table>
        <p></p>
        <table id="voorstelling">
            <thead>
                <tr>
                    <th>Voorstelling</th>
                    <th>Datum</th>
                    <th>Aantal vrij plaatsen</th>
                    <th>Aantal te reserveren</th>
                </tr>
            </thead>
            <tbody>
                <c:forEach items="${voorstellingen}" var="voorstelling" >
                <tr>
                    <td><c:out value="${voorstelling.naam}" /></td>  
                    <td><c:out value="${voorstelling.datum.time}" / </td>
                    <td><c:out value="${voorstelling.aantalVrij}" /></td>
                    <td><c:out value="${voorstelling.id}" /></td>
                    <td><s:text name="aantal[${voorstelling.id}]"></s:text>
                </tr>
                </c:forEach>
            </tbody>
        </table>
        <s:submit name="reserveer" value="Reserveren"/>
    </s:form>

Code above generates a table in which you can put the client details, then a section which shows the theatre performances and a text box in which you can put the number of tickets you want to order. Pressing "Reserveer" forwards to the event handler in the actionBean.

Following is the relevant code in my ActionBean

public class TheaterActionBean extends BaseActionBean {

private static final String FORMVIEW = "/WEB-INF/jsp/theater.jsp";
private String naam = null;
private String telefoon=null;
//private int aantal = 0;
private int id = 0;
private Map<Integer,Integer> aantal = null;

public String message = null;

...... 

 public Resolution reserveer() throws TheaterException{
   message = message +  "Telefoon:" + telefoon + "Naam:" + naam + "Aantalreserveren:" + aantal;
   Theater theater=getContext().getCurrentTheater();
   if (aantal != null){
        Iterator it = aantal.entrySet().iterator();
        while (it.hasNext()){
            Map.Entry paar= (Map.Entry)it.next();
            message = message + "key " + paar.getKey() + "value" + paar.getValue();
        }
   }
   return new ForwardResolution(FORMVIEW);
}

......

public void setAantal(Map<Integer,Integer> aantal){
    this.aantal = aantal;
}

Reading through some other sources like

http://www.coderanch.com/t/555416/oa/Stripes-Nested-Indexed-Properties-Select and https://stripesframework.atlassian.net/wiki/display/STRIPES/Indexed+Properties

i'm expecting following

<s:text name="aantal[${voorstelling.id}]"></s:text>

to map to the Map attribute named aantal in my ActionBean but ik keeps giving back a null value.

My goal is to use the map to process the separate reservations for each theatre performance using the underlying model.

What do i need to do to get this to work with stripes ?

Kind regards,

Jos

1

There are 1 answers

1
Richard Osseweyer On

Though your code examples contain some ellipses, it may be that you did not implement a getter for aantal:

public Map<Integer,Integer> getAantal(){
    return aantal;
}

Not entirely sure about it, but I believe Stripes introspects the getter on indexed properties in the ActionBean to get the generic type information for binding.