Struts 2 If Else tag

In the struts, web application, the If Else tag is used to check among multiple conditions. The code along with the right condition will be executed as the result.

Struts Control Tag – If Else Example:

To get input from the user create the index.jsp file:

The index.jsp page takes the input from the user. The user will select the name. Then after clicking on the Submit button, it will redirect to the next resource. It contains one list and one button on the form with the link to action class

<%@ page language = "java" contentType = "text/html; charset = ISO-8859-1"
  pageEncoding = "ISO-8859-1"%>
<%@ taglib prefix = "s" uri = "/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
   "http://www.w3.org/TR/html4/loose.dtd">


<html>
   <head>
      <title> If Else Tag Example</title>
   </head>
   
   <body>
      <h1>If Else Control Tag in Struts</h1>
      <form action = "hello">
         <label for = "name">Please select username</label><br/>
         <select name = "name">
            <option name = "Sanket">Sanket</option>
            <option name = "Ram">Ram</option>
            <option name = "Shyam">Shyam</option>
         </select>
         <input type = "submit" value = "Submit"/>
      </form>
   </body>
</html>

Create welcome.jsp file to denote success:

The welcome.jsp page displays Welcome message and selected name by the user by using the If, elseif, else property. It checks the name parameter with the values and according to that display welcome message for different users.

<%@ page contentType = "text/html; charset = UTF-8" %>
<%@ taglib prefix = "s" uri = "/struts-tags" %>


<html>
   <head>
      <title>Example of If and Else</title>
   </head>
   
   <body>
      <b>Example of If and Else Control tag</b><br/>
      
      <s:if test = "name=='Sanket'">
         Welcome user 'Sanket'. 
      </s:if>
      
      <s:elseif test = "name=='Ram'">
        Welcome user 'Ram'.
      </s:elseif>
      
      <s:else>
          Welcome user 'Shyam'.
      </s:else>
   </body>
</html>

Create error.jsp file to denote failure:

The error.jsp page displays Sorry wrong Name if any exception or error is thrown by the application.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Error page</title>
</head>
<body>
Sorry wrong name
</body>
</html>

Create the action class HelloWorldAction.java:

The Action class HelloWorldAction.java contains only one field name with its getter and setters. It contains one execute method which will return a success string on successful execution.

public class HelloWorldAction 
{
   private String name;


   public String execute() throws Exception 
   {
      return "success";
   }
   
   public String getName()
   {
      return name;
   }


   public void setName(String name) 
   {
      this.name = name;
   }
}

web.xml file is created inside WEB-INF folder in WebContent folder:

The web.xml file defines how elements are processed. The entry of FilterDispatcher is done in the web.xml file. This file is created in WebContent->WEB-INF folder. /* specifies all urls will be parsed. This task is done by struts filter.

<?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" 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>StrutsIfElse</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <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>
</web-app>

Construct struts.xml file:

In the struts.xml file, make the entry of the action class HelloWorldAction and link for it and result pages. The result determines what browser will display after the execution of the action. Results have optional names like success and error.

<?xml version="1.0" encoding="UTF-8" ?>  
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts  
Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">  
<struts>
   <constant name = "struts.devMode" value = "true" />
   <package name = "helloworld" extends = "struts-default">
     
      <action name = "hello" 
         class = "HelloWorldAction" 
         method = "execute">
         <result name = "success">/welcome.jsp</result>
                  <result name = "error">/error.jsp</result>
         
      </action>


   </package>
</struts>

Output:

In order to run the application, right-click on the project -> Click on the option Run As -> then select Run on Server. It will show one label to select the name and the list with 3 names and one submit button.

Struts Control Tag - If Else

Then select the first name Sanket and click on the submit button.

Struts Control Tag - If Else

As the name, Sanket is selected it will be checked in welcome.jsp page and welcome message for user Sanket is displayed.

Struts Control Tag - If Else

Again run the application and select the second name Ram and click on the submit button.

Struts Control Tag - If Else

This time Ram is selected so elseif gets executed in welcome.jsp and welcome message is printed for user Ram.

Struts Control Tag - If Else

Again run the application and this time select the last option Shyam and click on the submit button.

Struts Control Tag - If Else

As the name Shyam is selected else will get executed and a welcome message for the user Shyam is displayed.

Struts Control Tag - If Else