Struts2: Updating the values of a List Of Objects inside a Map
According to your latest update. If you are using TreeMap
Struts2 cannot correctly determine type of elements inside it. Change declaration of testTreeMap
from TreeMap
to Map
.
private Map<String,ObjectCList> testTreeMap = new TreeMap<String,ObjectCList>();
Or annotate testTreeMap
with com.opensymphony.xwork2.util.Element
annotation to tell Struts2 what type are elements inside map.
@Element(value = ObjectCList.class)
private TreeMap<String,ObjectCList> testTreeMap = new TreeMap<String,ObjectCList>();
How do I show elements of a Map's value (List of Objects) in a Struts 2 optgroup?
- You're confusing
%{}
with#
and#{}
(three different things); - 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; - 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>
How to pass a MapObjectA, ListObjectB to action in Struts 2
I made it work after the following change.
<s:iterator value="event.planMap" status="mStat" >
<h4>Plan Type: <s:property value='key' /></h4>
<table id="plan">
<s:iterator value="value.details" status="stat">
<tr>
<td><s:textfield id="name" name="event.planMap['%{key}'].details[%{#stat.index}].name" /></td>
<td><s:textfield id="text" name="event.planMap['%{key}'].details[%{#stat.index}].text" /></td>
<td><s:textfield id="contact" name="event.planMap['%{key}].details[%{#stat.index}].contact" /></td>
</tr>
</s:iterator>
Struts2 binding a map inside an object to an action attribute
In your case, objB is a List
contanining an HashMap
, then one HashMap
for every element of the List
.
objA
|--- objB[0]
|-- objC[A]
|-- objC[B]
|-- objC[C]
|--- objB[1]
|-- objC[X]
|-- objC[Y]
|-- objC[Z]
|--- objB[n]
|-- objC[N1]
|-- objC[N2]
|-- objC[N3]
Then you need two iterators and the following OGNL
notation, to refer to the single elements with the name
attribute:
<s:iterator value="objA.objB" var="listRow" status="listStatus">
<!-- Iterating the List -->
<s:iterator value="#listRow.myMap" var="mapRow" >
<!-- Iterating the Map -->
<li>
<label>
<s:property value="#mapRow.key"/>
</label>
<span>
<s:textfield value="%{#mapRow.value.attr12}"
name="objA.objB[#listStatus.index].myMap[#mapRow.key].attr112"
/>
</span>
</li>
</s:iterator>
</s:iterator>
show a list of arrays of objects List(Object[]) in struts select tag s:select
Well, no answer. I converted the Obect[] to a Map
and used the map in the select tag
Individual object map values in s:select in Struts2
The .{}
in Clause.{clause}
is called a projection in OGNL and it creates a list of properties of that object. That is why you get all values in option tag.
Usually you should put just property name in listValue
attribute but in your case you need to get value from map value then reference it like that value.property_of_object
<s:select list="Clause" listValue="value.clause" />
How to get values from hashmap in struts2 and compare it in s:if test?
You don't need to iterate through map to get a value from it. As you doing it now the type of your #map
variable is not a Map
but Map$Entry
.
So remove <s:iterator>
tag and use just <s:if>
- <s:else>
tags.
<s:if test="param['USE'].equals(\"1\")">
<s:checkbox label="Use" name="use" fieldValue="true" value="true"
labelposition="left"/>
</s:if>
<s:else>
<s:checkbox label="Use" name="useMta" fieldValue="false" value="false"
labelposition="left"/>
</s:else>
BTW naming your action field as param
is probably not the best idea, consider changing it to something less keyword-ish.
Related Topics
How to Do Arithmetic Operations on the Number Baseclass
Java: Reading a File into an Array
Does a Finally Block Always Run
Java: How to Do Dynamic Casting of a Variable from One Type to Another
How to Get the X and Y of a Program Window in Java
Regex to Split Camelcase or Titlecase (Advanced)
Java 8's Streams: Why Parallel Stream Is Slower
Add Bean Programmatically to Spring Web App Context
Reliable File.Renameto() Alternative on Windows
How to Create Table Using Ascii in a Console
How to Sort an Array of Ints Using a Custom Comparator
Convert Boolean to Int in Java
How to Calculate the Intersection of Two Sets
Specifying Java Version in Maven - Differences Between Properties and Compiler Plugin
Double Checked Locking in Singleton
Jaxb: How to Ignore Namespace During Unmarshalling Xml Document