Struts 2 If Else Tag

Struts 2 If Else Tag with Examples

In this article, I am going to discuss Struts 2 If Else Tag with Examples. Please read our previous article where we discussed the basic concepts of Struts 2 Tag.

What is the If Else Tag in Struts 2?

The if and else tags in Struts2 are control tags that can be used to perform basic condition checking in the view. The if tag has a test attribute, the value of which must evaluate to be true or false. If true, the statements between the opening and closing s:if tags will be executed. If false, the statements between the opening and closing s:else tags will be executed. Note that s:else tags come after the closing s:if tag and that the s:else tags are not required.

How to Implement If Else Tag in Struts 2?

Step 1: Create a new maven project in Eclipse. We will use Maven to manage the dependencies of struts2. This way, we do not have to import JARs.

How to Implement If Else Tag in Struts 2?

Remember to set the packaging to war. This is because we shall be deploying a web application.

How to Implement If Else Tag in Struts 2?

Finally, click on the finish button to create the project. The following project directories need to be created:

How to Implement If Else Tag in Struts 2?

Step 2: Modify pom.xml to add the required dependencies and the build configuration:

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
https://maven.apache.org/xsd/maven-4.0.0.xsd">

  <modelVersion>4.0.0</modelVersion>
  <groupId>com.dotnet</groupId>
  <artifactId>strutex</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  
  <dependencies>
   <dependency>
    <groupId>org.apache.struts</groupId>
    <artifactId>struts2-core</artifactId>
    <version>6.1.2.1</version>
   </dependency>
   
   <dependency>
    <groupId>org.apache.struts</groupId>
    <artifactId>struts-taglib</artifactId>
    <version>1.3.10</version>
   </dependency>
   
   <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>servlet-api</artifactId>
    <version>2.3</version>
   </dependency>
   
   <dependency>
     <groupId>taglibs</groupId>
     <artifactId>standard</artifactId>
     <version>1.1.2</version>
   </dependency>
   
   <dependency>
     <groupId>javax.servlet.jsp.jstl</groupId>
     <artifactId>jstl-api</artifactId>
     <version>1.2</version>
  </dependency>
  
  <dependency>
     <groupId>javax.servlet.jsp</groupId>
     <artifactId>javax.servlet.jsp-api</artifactId>
     <version>2.3.3</version>
     <scope>provided</scope>
 </dependency>
 
 <dependency>
     <groupId>org.apache.struts</groupId>
     <artifactId>struts2-convention-plugin</artifactId>
     <version>6.2.0</version>
 </dependency>
  </dependencies>
  
  <build>
   <plugins>
    <plugin>
     <groupId>org.apache.maven.plugins</groupId>
    	  <artifactId>maven-compiler-plugin</artifactId>
    	  <version>3.11.0</version>
    	  <configuration>
     <source>17</source>
     <target>17</target>
    </configuration>
    </plugin>
   </plugins>
  </build>
</project>

Step 3: In the src/main/webapp directory, create a new file called index.jsp. This file asks the user for a name belonging to a student. Add the following content to the file:

<%@ 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>Hello World</title>
   </head>
   
   <body>
      <h1>Check marks</h1>
      
      <form action = "hello">
         <label for = "name">Please pick a student: </label><br/>
         <select name = "name">
            <option>Mike</option>
            <option>Jason</option>
            <option>Mark</option>
         </select>
         <input type = "submit" value = "Get Marks"/>
      </form>
      
   </body>
</html>

Step 4: In the src/main/webapp directory, create a new file called success.jsp. This file displays the results of the chosen student. Add the following content to the file:

<%@ 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>Student Marks</b><br/>
      
      <s:if test = "name=='Mike'">
         Name: Mike
         Marks: 99/100 
      </s:if>
      
      <s:elseif test = "name=='Jason'">
         Name: Jason
         Marks: 89/100 
      </s:elseif>
      
      <s:elseif test = "name=='Mark'">
         Name: Mark
         Marks: 79/100 
      </s:elseif>
      
      <s:else>
         Wrong choice!
      </s:else>
   </body>
</html>

Step 5: In the src/main/java directory, create a new file called StudentAction.java. The file contains the Java action code. Add the following content to the file:

package com.dotnet;

import com.opensymphony.xwork2.ActionSupport;

public class StudentAction extends ActionSupport
{
 private String name;
 
 public String execute()
 {
  return SUCCESS;
 }

 public String getName() {
  return name;
 }

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

Step 6: In the src/main/webapp directory, create a new file called struts.xml. This file is responsible for configuring struts2. Add the following content to the file:

<?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.devMode" value = "true" />
   <package name = "helloworld" extends = "struts-default">
     
      <action name = "StudentAction" 
         class = "com.dotnet.StudentAction" 
         method = "execute">
         <result name = "success">/success.jsp</result>
      </action>

   </package>
</struts>

Step 7: In the src/main/webapp directory, create a new directory called WEB-INF. In this directory, create a new file called web.xml. This file is responsible for implementing struts2 in the web application. Add the following content to the file:

<?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/web-app_3_0.xsd"
id="apache-struts-config-example"
version="3.0">

 <display-name>Struts Config XML</display-name>
 
 <welcome-file-list>
  <welcome-file>index.jsp</welcome-file>
 </welcome-file-list>
 
 <filter>
  <filter-name>struts2</filter-name>
  
  <filter-class>
   org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
  </filter-class>
 </filter>
 
 <filter-mapping>
  <filter-name>struts2</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>

</web-app>

Step 8: Compile and execute the program on the Tomcat server. The following page must open in your default web browser:

Struts 2 If Else Tag with Examples

Select the name of any student and click on the “Get Marks” button. The following page should be visible:

Struts 2 If Else Tag with Examples

Congratulations! You now know how to implement if else tags in struts2!

In the next article, I am going to discuss Struts 2 Iterator Tag with Examples. Here, in this article, I try to explain Struts 2 If Else Tag with Examples and I hope you enjoy this Struts 2 If Else Tag article.

Registration Open For New Online Training

Enhance Your Professional Journey with Our Upcoming Live Session. For complete information on Registration, Course Details, Syllabus, and to get the Zoom Credentials to attend the free live Demo Sessions, please click on the below links.

Leave a Reply

Your email address will not be published. Required fields are marked *