Struts 2 If, ElseIf and Else Tag Example


On this page we are going to show how to check condition on JSP page using Struts 2 If, ElseIf and Else tag. These tags are working same like other programming languages If, ElseIf and Else tag.

If could be used by alone.

<s:if test="msg=='WebSparrow'">
	<p>I am in If condition</p>
</s:if>

or with ElseIf

<s:if test="msg=='WebSparrow'">
	<p>I am in If condition</p>
</s:if>
<s:elseif test="msg=='WebSparrow.org'">
	<p>I am in ElseIf condition.</p>
</s:elseif>

or with Else

<s:if test="msg=='WebSparrow'">
	<p>I am in If condition</p>
</s:if>
<s:else>
	<p>I am in Else condition</p>
</s:else>

It could be used with multiple blocks.

<s:if test="msg=='WebSparrow'">
	<p>I am in If condition</p>
</s:if>
<s:elseif test="msg=='WebSparrow.org'">
	<p>I am in ElseIf condition.</p>
</s:elseif>
<s:else>
	<p>I am in Else condition</p>
</s:else>

See the Complete Example

Add the struts 2 filters in web.xml

web.xml
<filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

Create the Action class to handle the request.

IfElseAction.java
package org.websparrow;

import com.opensymphony.xwork2.ActionSupport;

public class IfElseAction extends ActionSupport {
	private String msg = "";

	@Override
	public String execute() throws Exception {
		return "SUCCESS";
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}
}

Create the JSP page to show the responses.

index.jsp
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
</head>
<body>
	<h1>Struts 2 If, ElseIf and Else tag example</h1>

	<!-- start of if block -->
	<s:if test="msg=='WebSparrow'">
		<p>I am in If condition</p>
	</s:if>

	<!-- start of elseif block -->
	<s:elseif test="msg=='WebSparrow.org'">
		<p>I am in ElseIf condition.</p>
	</s:elseif>

	<!-- start of else block -->
	<s:else>
		<p>I am in Else condition</p>
	</s:else>

</body>
</html>

Map the action class and jsp page in struts.xml

struts.xml
<struts>
    <constant name="struts.devMode" value="true" />
    <package name="default" namespace="/" extends="struts-default">
        <action name="checkCondition" class="org.websparrow.IfElseAction">
            <result name="SUCCESS">/index.jsp</result>
        </action>		
    </package>
</struts>

Output:

To test the application check the below condition. If msg=WebSparrow it will go to If block, or if msg=WebSparrow.org it will go to ElseIf block, if msg is not in above it will goes to Else block.

localhost:8083/Struts2IfElseExample/checkCondition?msg=WebSparrow.org

Struts 2 If, ElseIf and Else Tag Example

Similar Posts

About the Author

Atul Rai
I love sharing my experiments and ideas with everyone by writing articles on the latest technological trends. Read all published posts by Atul Rai.