Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Struts Struts2 Struts2controltags Control Tags-If / Else If / Else

Related Tutorials


 
 

Share on Google+Share on Google+

Control Tags-If / Else If / Else

Advertisement
In this section we are going to discuss the various control tags ( The Control Tags are used for flow control, such if, else and iterate.)

Control Tags-If / Else If / Else

     

In this section we are going to discuss the various control tags ( The Control Tags are used for flow control such as if, else and iterate.)

 'If' tag could be used by itself or with 'Else If' Tag and/or single/multiple 'Else' Tag.

Create a JSP page IfControlTag.jsp.

Set a property 'technologyName' with a value 'Java' as <s:set name="technologyName" value="%{'Java'}"/>

Among if, elseif and else tags only one tag evaluates at a time. Evaluation is based upon the conditions being processed. Evaluated conditions must be of Boolean type. This is illustrated in the following Jsp page.

[Note:

If the condition in <s:if > tag evaluates to 'true' then only this tag is evaluated and others are discarded. As illustrated in the example.

If the condition in <s:if > tag evaluates to 'false' and <s:elseif > tag evaluates to 'true' then the body of the <s:elseif > tag is processed.

If the condition in <s:if > tag  and <s:elseif > tags evaluates to 'false' then only the  <s:else > tag is processed. ]

IfControlTag.jsp

<%taglib prefix="s" uri="/struts-tags" %>

<html>
  <head> 
  <title>Struts Control Tag Example</title>
  </head>
  <body>
  <s:set name="technologyName" value="%{'Java'}"/>

 <s:if test="%{#technologyName=='Java'}">
  <div><s:property value="%{#technologyName}" /></div>
  </s:if>

  <s:elseif test="%{#technologyName=='Jav'}">
  <div><s:property value="%{#technologyName}" /></div>
  </s:elseif>
  
  <s:else>
  <div>Technology Value is not Java</div>
  </s:else>

  </body>
</html>

struts.xml: Add the following xml snippet in the struts.xml file.

<action name="doIf" >
  <result>/pages/genericTags/IfControlTag.jsp</result>
</action>

index.jsp : Add the following jsp snippet in the index.jsp file.

<ul>
   <li><a href="roseindia/doIf.action">IF Control Tag Example</a></li>
</ul>

In the IfControlTag.jsp only <s:if> tag evaluates to true 
  <s:
if test="%{#technologyName=='Java'}">
  <div><s:property value="%{#technologyName}" /></div>
  </s:if>

So we get the output equal to Java

Java

     

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: July 14, 2007

Related Tutorials

Discuss: Control Tags-If / Else If / Else   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
Yangzhongjie
June 30, 2011
<s:if test="@com.rjgc......................">

Hello: I want to kown .How to call the method of class in struts tags.Thank you!
DMCA.com