Struts2: test equality of <s:property/> to a variable from action - struts2

I am trying to test:
<s:if test="%{selectedLanguage == <s:property/>}">
Where selectedLanguage comes from the action, and s:property is the current object.
In a iterator like this:
<s:iterator value="languages">
<s:if test="%{selectedLanguage == <s:property/>}">
<option id="<s:property/>" selected><s:property/></option>
</s:if>
<s:else>
<option id="<s:property/>"><s:property/></option>
</s:else>
</s:iterator>
Obviously,it doesnt work. How can I solve?

Without fixing things, just use the var attribute to give the object of iteration a name:
<s:iterator value="languages" var="language">
<s:if test="%{selectedLanguage == #language}">
<option id="<s:property/>" selected><s:property/></option>
</s:if>
<s:else>
<option id="<s:property/>"><s:property/></option>
</s:else>
</s:iterator>
You could also just use #top to get the top of the stack, but I think naming it is more communicative.
I'd recommend setting a selected attribute based on #language or just use the select tag.

Related

how can i get last element iterator struts2

I'm looking for ideas to show only last element of iterator using struts 2
i have this code :
<s:iterator value="listuser" status="userStatus">
<tr class="<s:if test="#userStatus.odd == true ">odd</s:if><s:else>even</s:else>">
<td><s:property value="nameuser" /></td>
// and others
</s:iterator>
The Struts2 Iterator Status not only provide odd and even methods, it provides also a last and first method.
<s:iterator value="listuser" status="userStatus">
<s:if test="#userStatus.last == true ">
<td><s:property value="nameuser" /></td>
</s:if>
</s:iterator>
Something like this will do the trick :
<s:if test="#userStatus.index==listuser.size()-1">
//Show the last element here
</s:if>
[UPDATE]
As per Quaternion's comment, here's how to get it in one line :
<s:set name="lastUser" value="listuser[listuser.size()-1]"/>
Above we set the last element in lastUser and here's how to use it else-where in the same page.
<s:property value="#lastUser.name"/>

how to check for a condition in g select tag

I'm a newbie to Grails and GSPs.
I need to achieve the following code using g:select tag
<select name="applaiances" style="width: 200px" onchange="selectedTC(this); ">
<g:each in="${applaianceList}" var="appl">
<g:if test="${appl == "TELEVISION"}">
<option value="TELEVISION">TV</option>
</g:if>
<g:else>
<option value="${appl}">${appl}</option>
</g:else>
</g:each>
</select>
Any help would be appreciated.
Haven't tried this in an app but you could try something like:
<g:select name="applaiances" onchange="selectedTC(this);" from="${applaianceList}" optionKey="${{it=='TELEVISION'?'TELEVISION':it}}" optionValue="${{it=='TELEVISION'?'TV':it}}"></g:select>
Not sure about the optionKey but you can apply transformation via a closure on the optionValue.
Update:
This is documented here. Just search for the phrase "If you require even more control over how each"

Increment a variable in struts2

Is it possible to increment a variable in struts 2?
I have a group of check boxes, each checkbox comes under different group. And I name the check boxes based on the group id and its own id from the database
//namesHead : list contains all the group Ids and names
//subHead : list contains all the subgroups id,Name and the reference of main group
<s:iterator status="status" value="namesHead">
<input type="checkbox" onclick="selectSimilarSubGroup('<s:property value="%{id}" />')" />Select All
<s:set name="itrVar" value="1"></s:set>
<s:iterator status="status1" value="subHead" >
<s:set name="var1" value="%{refer_id}"></s:set>
<s:set name="var2" value="%{id}"></s:set>
<s:if test="%{#var1==#var2}">
<s:set name="itrVar" value="%{#status1.count}"></s:set>
<input type="checkbox" multiple id="chk_grp<s:property value="%{id}"/>_<s:property value="%{#status1.count}" />" name="chk_grp" value="<s:property value="%{id_grp}"/>" />
<s:property value="%{name_grp}"/>
</s:if>
</s:iterator>
<s:hidden name="grp_count_%{id}" value="%{#itrVar}" />
</s:iterator>
I think its because the iterator is skipping the initial counts in the second loop. How can i modify the code to get that expected output. Or is there any way to increment a variable inside the page itself?
Mmm... sub-iterator status should reset on every iteration of the parent iterator, with both "two separate lists" or "a list inside another list"...
Something is messing up the counter. Try removing the <s:set line inside the <s:if, just for debugging, and ignoring the <s:hidden for now:
<s:iterator status="status" value="namesHead">
<input type="checkbox" onclick="selectSimilarSubGroup('<s:property value="%{id}" />')" />Select All
<s:set name="itrVar" value="1"></s:set>
<s:iterator status="status1" value="subHead" >
<s:set name="var1" value="%{refer_id}"></s:set>
<s:set name="var2" value="%{id}"></s:set>
<s:if test="%{#var1==#var2}">
<!-- Nothing here -->
<input type="checkbox" multiple id="chk_grp<s:property value="%{id}"/>_<s:property value="%{#status1.count}" />" name="chk_grp" value="<s:property value="%{id_grp}"/>" />
<s:property value="%{name_grp}"/>
</s:if>
</s:iterator>
<!-- Nothing here -->
</s:iterator>
What do you see ?

Date comparision in struts2 using OGNL

enter code hereIn my JSP I have display a hyperlink depending upon the date field if it falls between certain range. I am using struts 2 and ONGL
<s:set name="cDate" value="accDate"/>
<s:if test="%{#cDate gt '4/4/11' && #cDate gt '4/4/12}">
<td class="viewCellLast"><s:url id="editURL" action="editActivities">
<s:param name="id" value="%{id}"></s:param>
</s:url> <s:a href="%{editURL}" onclick="return nanCompany();" >Edit</s:a></td>
</s:if>
its failing, can anybody suggest the me how this can achieved.
thanks
smitha.
I got it resolved; I missed # in front fsDate:
<s:set name="fsDate" value="finStartDate"/>
<s:if test="%{#acDate.before(#fsDate}">
<td class="viewCellLast"><s:url id="editURL" action="editActivities">
<s:param name="id" value="%{id}"></s:param>
</s:url> <s:a href="%{editURL}" >Edit</s:a> </td>
</s:if>

How to use property value as textarea value in struts 2

Instead of just displaying list values using <s:property/>, i need to display it in textarea.The reason why i am doing this is <s:property/> does not have "name" attribute in order to submit the values to the action class. Is there any way to do that.I wrote down piece fo code below to make it clear so that you can understand my requirement though it is wrong.Please help me.
<s:iterator value="map" status="stat">
<s:textarea value="<s:property/>" />
</s:iterator>
As Jagan said, assume that map is java.util.List
<s:iterator value="map" status="stat">
<s:textarea value="%{top}" />
</s:iterator>
or
<s:iterator value="map" status="stat">
<s:textarea>
<s:param name="value">
<s:property/>
</s:param>
</s:textarea>
</s:iterator>
or
<s:iterator value="map" status="stat">
<s:textarea>
<s:param name="value">
${top}
</s:param>
</s:textarea>
</s:iterator>
#Jagan : if it is map how to do this ?
Assume map is java.util.Map
<s:iterator value="map" status="stat">
<s:textarea value="%{key}" />
<!-- or -->
<s:textarea value="%{value}" />
</s:iterator>
IteratorComponent
if (value == null && begin == null && end == null) {
value = "top";
}
top is not a attribute of iterator and not a keyword of OGNL. (I may be wrong)
top : top of Stack / element of the current iteration
e.g.
<s:property value="top" />
or
<s:property value="[0].top" />
Iterator tag examples of Struts2 Cookbook have better explanation

Resources