Question

I am new to Struts 2 & JSP. The question might be silly; please bear with me, if so. I have the following structure created in my Java program - I have an ArrayList comprising of HashMaps. One sample is as follows:

[{Name=S S. Peter, Email=xyz@yahoo.com, Type=P/C, Location=New York}, {Name=Tom Hanks, Email=tom.hanks@gmail.com, Type=C/A, Location=null}, {Name=Carl Zeiss, Email=null, Type=C/A, Location=null}]

Here the List name is ComboMeals and the HashMap name is authors.

I want to display these values in the form of a table. I am using the following construct in JSP:

    <s:iterator value="comboMeals">
 <div class="Row" align="left">
     <s:iterator value="authors" var="numbers"> 
        <s:set var="authorTypeKey" value="key" />
            <div class="Cell" align="left">
                <s:if test="%{#authorTypeKey=='Type'}">
                    <p><s:textfield name="selectedTypeTxtBox" value="%{authors.Type}"></s:textfield><br>
                    <s:select list="listOfTypes" name="selectedType" value="%{authors.Type}" onchange="getSelectedType(this)"></s:select><br><br></p>
                </s:if>
                <s:elseif test="%{#authorTypeKey=='Name'}">
                    <p><s:textfield name="selectedNameTxtBox" value="%{authors.Name}"></s:textfield><br>
                    <s:select  list="listOfNames" name="selectedName" value="%{authors.Name}" onchange="getSelectedName(this)"></s:select><br><br></p>
                </s:elseif>
                <s:elseif test="%{#authorTypeKey=='Location'}">
                     <p><s:textfield  name="selectedLocationTxtBox" value="%{authors.Location}"></s:textfield><br>
                    <s:select list="listOfLocations" name="selectedLocation" value="%{authors.Location}" onchange="getSelectedLocation(this)"></s:select><br><br></p>
                </s:elseif>
                <s:elseif test="%{#authorTypeKey=='Email'}">
                     <p><s:textfield name="selectedEmailTxtBox" value="%{authors.Email}"></s:textfield><br>
                    <s:select list="listOfEmails" name="selectedEmail" value="%{authors.Email}" onchange="getSelectedEmail(this)"></s:select><br><br></p>
                </s:elseif>
            </div>
      </s:iterator>
 </div> 

--Please ignore the JavaScript for onChange event (this is for s:select).

But it is not displaying any values. Could you please help.

Was it helpful?

Solution

Since you are using var, read the values from it, not from the iterable source (without specifying an index): change all your value="%{authors.Something}" in your select boxes with value="%{numbers.Something}".

Also read:

  1. All the ways to read an iterated object (index, var, top)
  2. How to deal with nested Collections / Maps in Struts2 (by creating wrapping objects)

P.S: for a better help, next time consider posting your java objects and declaration, instead (or in addition to) the content of your objects.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top