Struts 2 If, Else Tag Example


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.


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"
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
<title>If and Else</title>
   <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>
      <input type="submit" value="Like it!!"/>

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

4. Struts.xml Configurations

A simple struts.xml configuration file.

<?xml version="1.0" encoding="UTF-8"?>
   "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
	<constant name="struts.devMode" value="true" />
	<package name="tags" extends="struts-default">
		<action name="conditionexample" class=""
			<result name="success">/ConditionExample.jsp</result>

5. Web.xml Configurations

Create a web.xml deployment descriptor.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi=""

   id="WebApp_ID" version="3.0">

   <display-name>Struts 2</display-name>

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



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.

Speak Your Mind


Please support the site
By clicking any of these buttons you help our site to get better