How to declare a variable and assign a value to it in Struts2?

Struts2Logo

By using Struts 2 Set  tag, we can  assign a value to a variable in a specified scope (application, session, request, page, or action), the action is the default scope. Well in struts2 we have concept of value stack and during request processing Struts2 framework will push the action on the top of value stack and its properties (Variable) will work as they are on the top of it.Here is the example:

setTag.jsp
<%@taglib uri="/struts-tags" prefix="s"%>

<div id="appendMsg" class="messageRow">
<s:if test="messageMap!=null">
<s:set var="counter" value="0"/>
<s:iterator value="messageMap">
<s:set var="counter" value="%{#counter+1}"/>
<div class="message">
<div>
<s:if test="%{#counter ==1}" >
<img src="images/add.jpg"/>
<img src="images/delete.png" style="display:none;" />
</s:if>
<s:if test="%{#counter ==messageMap.size}" >
<img src="images/add.jpg" style="display:inline;" />
<img src="images/delete.png" style="display:inline;" />
</s:if>
<s:if test="%{#counter !=1 && #counter !=messageMap.size}" >
<img src="images/add.jpg" style="display:none;" />
<img src="images/delete.png" style="display:inline;" />
</s:if>
</div>
</div>
</s:iterator>
</s:if>
</div>

Using  Set tag here we declare a variable named counter and initialize it with 0 (at first bold line).While iterating over iterator we are increasing counter value by 1 (at second bold line) and checking the condition in if statement.
Share on Google Plus

About JK STACK

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment