• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

Struts 2 If, Else Tag Example

December 13, 2013 //  by Krishna Srinivasan//  Leave a Comment

This tag is useful for executing the simple if or else condition by taking the values from a variable. It is very similar to the one used in every language. You can use single else block or the multiple else block by using the else if condition. Let’s see an example to show the use of Struts 2 ‘If, ElseIf and Else‘ tag.

1. Create Struts 2 Action

This action class passes and stores the language value submitted by the user.

package javabeat.net.struts2;

public class Struts2ExampleAction{
   private String lang;

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

   public String getLang() {
      return lang;
   }

   public void setLang(String lang) {
      this.lang = lang;
   }
}

2. Create Input JSP

This JSP file lists the languages and send the value to action class once clicked on the submit button. Action value conditionexample is defined in the action mapping configurations.

<%@ 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 and Else</title>
</head>
<body>
   <B>If and Else Example From Struts2</B>
   <form action="conditionexample">
      <label for="lang">Please Like a Language</label><br/>
      <select name="lang">
         <option name="Java">Java</option>
         <option name="Groovy">Groovy</option>
         <option name="Scala">Scala</option>
      </select>
      <input type="submit" value="Like it!!"/>
   </form>
</body>
</html>

3. Struts 2 – If and Else Condition in JSP

If the user selects a value in the above screen and then submits it, this JSP takes that value and then uses the If, elseif, else condition to check and print the appropriate value.

<%@ 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 Tags in Struts 2</b>
	<br />
	<s:if test="lang=='Java'">
   You liked the language Java
</s:if>
	<s:elseif test="lang=='Groovy'">
   You lliked the language Groovy
</s:elseif>
<s:else>
   You don't like any of the above options.
</s:else>
</body>
</html>

4. Struts.xml Configurations

A simple struts.xml configuration 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="tags" extends="struts-default">
		<action name="conditionexample" class="javabeat.net.struts2.Struts2ExampleAction"
			method="execute">
			<result name="success">/ConditionExample.jsp</result>
		</action>
	</package>
</struts>

5. Web.xml Configurations

Create a web.xml deployment descriptor.

<?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>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.ng.filter.StrutsPrepareAndExecuteFilter
      </filter-class>
   </filter>
   <filter-mapping>
      <filter-name>struts2</filter-name>
      <url-pattern>/*</url-pattern>
   </filter-mapping>
</web-app>

6. Run The Application

If you access the application http://localhost:8080/Struts2App/index.jsp. You would see the following output in your screen.

Struts 2 if and else example input screen

Struts 2 if and else example output screen

Category: StrutsTag: Struts 2 Tutorials

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Previous Post: « Struts 2 Configuration Files Path
Next Post: Struts 2 Iterator Tag Example »

Reader Interactions

Leave a Reply Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact