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" %>
|
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 |