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



Control Tags-If / Else If / Else
Posted on: July 14, 2007 at 12:00 AM
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

     

Related Tags for Control Tags-If / Else If / Else:
cprocessiotagsluasedtagevalthisifconditionexampletoexamsseproceilitnotlsceinnobodyasmtrcaestruefalseprorateevaluatexaxampuatsdiscessatisllnotemplandarstrvascassrocrdthcondstaludiscardevaevapleplprocesprndonlyononl


More Tutorials from this section

Ask Questions?    Discuss: Control Tags-If / Else If / Else   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.