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

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

Rendyon | 7:24 AM | 0 comments

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


 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<%@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 line number 6).While iterating over iterator we are increasing counter value by 1 (at line number 11) and checking the condition in if statement.
Share this article :

0 comments:

 
Copyright © 2011. Find Updates - All Rights Reserved
Template Modify by Creating Website
Proudly powered by Blogger