1

EDITED: I tried asking this in another question, since I felt that the originally asked question was sufficiently answered. I got chastised for doing that. So I've edited this question accordingly. Basically, I don't know the right syntax to access the properties of the List of Employees that are in the "value" of each Map entry.


I have a TreeMap stored in session variable. The "key" of the TreeMap holds a String. The "value" of the holds a List of Objects. The TreeMap is populated in the Action class. A sample TreeMap might look like this:

ArrayList<Employee> employeeList1 = new ArrayList<Employee>();
Employee myEmployee = new Employee();
myEmployee.setEmployeeId("123");
myEmployee.setEmployeeName("John Doe");
employeeList1.add(myEmployee);

myEmployee = new Employee();
myEmployee.setEmployeeId("456");
myEmployee.setEmployeeName("Jane Doe");
employeeList1.add(myEmployee);
...

TreeMap<String,Employee> availableSupervisorsMap = new TreeMap<String,Employee>();
availableSupervisorsMap.put("A", employeeList1);
availableSupervisorsMap.put("B", employeeList2);
availableSupervisorsMap.put("C", employeeList3);
session.setAttribute("availableSupervisorsMap", availableSupervisorsMap);

In the JSP, I want to show a select box with each "key" as an optgroup label, and each employeeId and employeeName in the "value"'s list of Employee objects as the option value and display, respectively. I tried the code below, to no avail:

<s:select name="availableIds" list="%{#session.availableSupervisorsMap}" multiple="true">
  <s:optgroup label="%{key}">
    <s:iterator value="%{value}">
        <option value="<s:property value="employeeId"/>">
            <s:property value="employeeName"/>
        </option>
     </s:iterator>
   </s:optgroup>
</s:select>

So the select box SHOULD look something like this, minus the bullet points of course:

  • A
    • John Doe
    • Jane Doe
  • B
    • 1st employeeName of employeeList2
    • 2nd employeeName of employeeList2
    • xth employeeName of employeeList2
  • C
    • 1st employeeName of employeeList3
    • 2nd employeeName of employeeList3
    • xth employeeName of employeeList3

when I try the JSP code above, the following HTML is generated:

<select name="availableIds" id="AssignmentSupervisors_availableIds" multiple="multiple">

<option value="A">[Employee:
=========================================================
 employeeName =            John Doe
 employeeId =              123
=========================================================
, Employee:
=========================================================
 employeeName =            Jane Doe
 employeeId =              456
]</option>

<optgroup 
>

I have verified that the map is populated exactly as I expect it to be. So it's making it to the JSP with the correct data.

Roman C
  • 49,761
  • 33
  • 66
  • 176
CoachJon
  • 77
  • 1
  • 8
  • I don't know what other users did, I've not downvoted your other question, just commented it, and the meaning was: if you've used a wrong syntax here, and from the answer you've got the right syntax, when you ask a second question, please use the right syntax from this answer, not the wrong syntax from this question. – Andrea Ligios Sep 23 '16 at 20:17
  • 1
    @AndreaLigios I wanted to finish my testing first...and I did. Thank you so much! :) – CoachJon Sep 26 '16 at 17:20

1 Answers1

2
  1. You're confusing %{} with # and #{} (three different things);
  2. When you iterate a collection, the current object is pushed onto the value stack, so you don't need to mention the collection anymore. You can however get a reference to the current object by using the var keyword;
  3. You're misusing the IteratorStatus.

The correct code is easier than you think:

<s:select name="availableIds" list="%{#session.availableSupervisorsMap}" multiple="true">
  <s:optgroup label="key">
    <s:iterator value="value" var="currentRow">
        <option value="%{#currentRow.employeeId}">
            <s:property value="%{#currentRow.employeeName}"/>
        </option>
     </s:iterator>
   </s:optgroup>
</s:select>
Community
  • 1
  • 1
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243