Struts 2 – The if and else tags

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.
The ‘If‘ tag could be used by itself

<s:if test="%{#name=='Dinesh'}">
  This is Male Category
 </s:if>

or with ‘Else If‘ tag

<s:if test="%{#name=='Dinesh'}">
  This is Male Category
 </s:if>
 <s:elseif test="%{#name=='Sweety'}">
     This is Female Category
 </s:elseif>

and/or single/multiple ‘Else‘ tag.

<s:if test="%{#name=='Dinesh'}">
  This is Male Category
 </s:if>
 <s:elseif test="%{#name=='Sweety'}">
     This is Female Category
 </s:elseif>
 <s:else>
     Other Category
 </s:else>

Create action class:
ControlTagAction.java

package com.dineshonjava.struts2.action;

import com.opensymphony.xwork2.ActionSupport;

/**
 * @author Dinesh Rajput
 *
 */
public class ControlTagAction extends ActionSupport{

 private static final long serialVersionUID = 1L;
 
 private String userName;
 
 public String execute() {
  return SUCCESS;
 }
 
 public String getUserName() {
  return userName;
 }

 public void setUserName(String userName) {
  this.userName = userName;
 }
}

Create views-
Let us have index.jsp file as follows:

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>If ELSE Control Tag Struts2 | dineshonjava.com</title>
</head>
 
<body>
<h2>If ELSE Control Tag Struts2</h2>
<s:actionerror />
<s:form action="control">
    <s:textfield name="userName" key="label.username"/>
    <s:submit key="label.submit" align="center" />
</s:form>
</body>
</html>

Next let us have success.jsp to demonstrate the use of the if, else and elseif tags:

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>If ELSE Control Tag Struts2 | dineshonjava.com</title>
</head>
<body>
    <h2>If ELSE Control Tag Struts2</h2>
   <s:set name="name" value="userName"/>
 
 <s:if test="%{#name=='Dinesh'}">
  This is Male Category
 </s:if>
 <s:elseif test="%{#name=='Sweety'}">
     This is Female Category
 </s:elseif>
 <s:else>
     Other Category
 </s:else>
</body>
</html>

Here, the if tag returns true if the condition specified in the “test” attribute returns true. In our case we are comparing it against “Dinesh”. If the name is Dinesh, the tag returns true and we print the “This is male category”, otherwise the “elseif” block gets executed and if that is not satisfied, then the else block gets executed. This is no different from the conventional if, else if and else available in the Java language.
Here user name is male like “Dinesh’ then print “This is male category”, if user name like “Sweety” then print “This is female category”, if give any other value then print other value “Other category”.

Configuration Files
Your struts.xml should look like:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
 
<struts>
    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="false" />
    <constant name="struts.custom.i18n.resources" value="myapp" />
 
    <package name="default" extends="struts-default" namespace="/">
        <action name="control" class="com.dineshonjava.struts2.action.ControlTagAction">
            <result name="success">/success.jsp</result>
      <result name="input">/index.jsp</result>
        </action>
        
    </package>
 </struts>

Your web.xml should look like:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>Struts2ControlTags</display-name>
  <filter>
        <filter-name>struts2</filter-name>
        <filter-class>
            org.apache.struts2.dispatcher.FilterDispatcher
        </filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

Right click on the project name and click Export > WAR File to create a War file. Then deploy this WAR in the Tomcat’s webapps directory. Finally, start Tomcat server and try to access
URL http://localhost:8080/doj/index.jsp.
This will give you following screen:

Now insert “Dinesh” and submit then getting following.
http://localhost:8080/doj/control;jsessionid=CF742D962357EBE1ED73487C344793A8

Download SourceCode
Struts2IF_ELSE_Control.zip

 

<<Previous <<   || Index ||   >>Next >>
Previous
Next