I want to reuse a set of HTML field elements and have struts build 'name' attributes from a variable. I saw this How To Generate unique HTML id attributes within Struts 2 iterator tag and figured I could use something similar but can't get it to work. It seems I can build a name attribute from a struts variable only in an iterator loop. How can I do this with a simple variable instead?
Here is my code, first trying to use a variable, the second using an iterator :
<s:set var="type" value="Main" />
<s:textfield name="prefix%{#type}.Name"/>
<s:set var="AList" value="{'Main'}" />
<s:iterator value="AList" var="Ltype">
<s:textfield name="prefix%{#Ltype}.Name"/>
</s:iterator>
This generates 2 elements :
<input type="text" name="prefix.Name" value="">
<input type="text" name="prefixMain.Name" value="">
with the first not substituting the variable, and the iterator loop working.
Why doesn't it work and how can I do this?
You are assigning a value to the first variable, not a String literal,
and since there isn't an object called Main
in your ValueStack, you are assigning an empty String to the type
variable:
<s:set var="type" value="Main" />
while you are doing it (almost) great in the second example:
<s:set var="AList" value="{'Main'}" />
Just use
<s:set var="AList" value="%{'Main'}" />
or
<s:set var="AList" value="'Main'" />
everywhere and it will work.
{}
alone is useless and dangerous, because it has a special meaning in OGNL (list projection, list selection, etc.)