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.