Dayananda
Dayananda

Reputation: 732

accessing the scriptlet variable in the struts2 <s:if> tag

i have created variable in scriptlet in the following way in my jsp

<% int count= 0; %>

Based on some conditions i am incrementing the variable value...... (think now 'count' value is 5 )

Now i have to do some process if the 'count' value is 0 by using struts2 tags

i have tried the following ways. But i failed................

<s:if test="#count == 0" > 
   --------
   -------- 
</s:if>  

<s:if test="%{#count == 0}" >  
</s:if>  

Thanks in advance

Upvotes: 1

Views: 6233

Answers (2)

Quaternion
Quaternion

Reputation: 10458

Everyone has told you it isn't a good idea... If you want a bad idea then:

Following must be at top of JSP:

<%@ page import="com.opensymphony.xwork2.ActionContext" %>
<%@ page import="com.opensymphony.xwork2.util.ValueStack" %>

Following someplace inside your JSP:

<%
    int i = 0;
    ValueStack stack = ActionContext.getContext().getValueStack();
    stack.getContext().put("varName", i);
    stack.setValue("#attr['varName']", i, false);
%>

then this should work:

<s:property value="#varName"/> <!-- prints 0 -->

I didn't take the time to test this but that is how the struts2 set tag does its job (if not a good idea it's still a bit educational ).

Upvotes: 0

MohanaRao SV
MohanaRao SV

Reputation: 1125

Don't use scriptlets

<s:bean name="org.apache.struts2.util.Counter" var="counter">
   <s:param name="first" value="0"/>
   <s:param name="last" value="5" />
</s:bean>

<s:iterator value="#counter">
  <li><s:property /></li>
</s:iterator>

It will print 0 to 5.

Upvotes: 1

Related Questions