Table of contents
1.
Introduction
2.
Tag for Struts Control - If and Else Example
2.1.
Create action class
2.2.
Create Views
2.3.
Let's go on to HelloWorld.jsp to see how to use the if and else, and elseif tags.
2.4.
Files for Configuration
3.
Output
4.
Frequently Asked Questions
4.1.
What benefits do Struts offer?
4.2.
Struts are still in use.
4.3.
Shocks and struts the same?
4.4.
An assembly of front struts contains what?
4.5.
Why does spring beat out Struts?
5.
Conclusion
Last Updated: Mar 27, 2024
Medium

The If and Else Tags

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Hey, Ninja, let us begin with the introduction of If and Else Tags. So, The If Else tag is used in the struts web application to compare several situations. As a result, the code will be executed along with the appropriate condition.

Struts

Tag for Struts Control - If and Else Example

Create action class

package com.codingninjas.struts2;

public class NinjaAction {
  private String fullname;

  public String execute() throws Exception {
      return "success";
  }
  
  public String getNfullame() {
      return fullname;
  }

  public void setfullName(String fullname) {
      this.fullname = fullname;
  }
}
You can also try this code with Online Java Compiler
Run Code

java

Create Views

Let's look at the index.jsp file as shown below:

<%@ 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" 
  "https://www.codingninjas.com/">

<html>
  <head>
      <title>Hello World</title>
  </head>
  
  <body>
      <h1>Struts2</h1>
      <form action = "hello">
        <label for = "fullname">Pick a name</label><br/>
        <select fullname = "fullname">
            <option fullname = "Burge">Burge</option>
            <option fullname = "Sagar">Sagar</option>
            <option fullname = "Leon">Leon</option>
        </select>
        <input type = "submit" value = "Say Hi"/>
      </form>
  </body>
</html>

Let's go on to HelloWorld.jsp to see how to use the if and else, and elseif tags.

jsp

<%@ page contentType = "text/html; charset = UTF-8" %>
<%@ taglib prefix = "s" uri = "/struts-tags" %>
<html>
  <head>
      <title>If and Else Example</title>
  </head>
  <body>
      <b>If and Else Example</b><br/>
      <s:if test = "fullname=='Burge'">
        You have selected 'Burge'. 
      </s:if>
      <s:elseif test = "fullname=='sagar'">
        You have selected 'sagar'
      </s:elseif>
      <s:else>
        You have not selected 'Burge' or 'sagar'.
      </s:else>
  </body>
</html>


In this case, the "if" tag returns true if the "test" attribute's condition is satisfied. In this instance, we are contrasting it with "Mike." The tag returns true if the name is Mike, in which case the string is printed. If not, the "elseif" block is executed, and if that condition is not met, the else block is then carried out. This is identical to the standard if, else if, and else constructs found in the Java language.

Files for Configuration

Your struts.xml should appear as follows:

<?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 = "hello" 
        class = "com.codingninjas.HelloWorldAction" 
        method = "execute">
        <result name = "success">/HelloWorld.jsp</result>
      </action>

  </package>
</struts>


Your web.xml should appear as follows:

xml

<?xml version = "1.0" Encoding = "UTF-8"?>
<web-app xmlns:xsi = "https://www.codingninjas.com/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>Struts 2</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.FilterDispatcher
      </filter-class>
  </filter>

  <filter-mapping>
      <filter-name>struts2</filter-name>
      <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

To produce a War file, perform right-click on the project name and select Export > WAR File. After that, deploy this WAR to the webapps directory of Tomcat. Launch Tomcat server. This will result in the screen shown below:

Output

Output

Output

Frequently Asked Questions

What benefits do Struts offer?

Benefits of Struts. Java, JSP, and Action classes are well maintained and simple for developers to understand because Struts adheres to the MVC paradigm. Less time is needed for development, which benefits those who design applications. The MVC framework makes application maintenance simple.

Struts are still in use.

According to estimates, at least 65 per cent of the Fortune 100 firms rely on web applications created with the Apache Struts framework in 2017 despite the Apache Struts project being on the market for 18 years.

Shocks and struts the same?

A surprise is an independent component, but support combines a shock with additional properties to form a single structural unit—both aid in maintaining tire contact with the pavement and stabilizing the car. Your car would bounce down the road without shocks.

An assembly of front struts contains what?

The coil spring, which supports the vehicle's height, weight, and stability, and the shock absorber, which absorbs bumps and dampens vibrations brought on by abnormalities in the road, are the two main components of a strut assembly.

Why does spring beat out Struts?

Although struts are more effective than spring frameworks, they can occasionally add complexity, whereas struts keep things simple thanks to their straightforward and maintainable architecture. Compared to braces, the spring framework offers more capabilities. The significant reasons why Spring MVC is chosen are its increased performance and security.

Conclusion

In this article, we have learned about If and Else Tags. There is a test attribute on the Struts if and else tag. The test attribute's value must evaluate as true or false. If true, the sentences between the opening and ending tags will be carried out. 

Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in pygameCompetitive ProgrammingJavaScriptSystem Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! Nevertheless, you may consider our paid courses to give your career an edge over others!

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!

thank you
Live masterclass