Struts 2 If, else if and else control tag

if-elseif-else tag:

The if-elseif-else tag is used for the conditional processing i.e. a block of statements executed based on some conditions.

Syntax: 

<s:if test=%{condition1}>
          //block of statements
</s:if>
<s:elseif test="%{condition2}">
         //block of statements
</s:elseif>
<s:else>
        //block of statements
</s:else>

Example:

test.jsp

<%@ taglib uri="/struts-tags" prefix="s"%>
<html>
	<head>
	    <title>
               Struts 2 If, else if and else control tag example
            </title>
	</head>
	<body>
		<h3>
                 This is a If, else if and else control tag example.
                </h3>
 
		<s:form action="Test">
			<s:select label="Select subject" 
			  headerKey="-1" headerValue="Select subject"
			  list="#{'java':'java', 'DBMS':'DBMS'}" 
			  name="selectedSubject" 
			  value="2" />
			<s:submit value="submit" align="center"/>
		</s:form>
 
	</body>
</html>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 
 
 	<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>
 
	<welcome-file-list>
	  <welcome-file>test.jsp</welcome-file>
	</welcome-file-list>
 
</web-app>

struts.xml

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
 
<struts>
 
	<package name="default" extends="struts-default">
		<action name="Test" class="com.w3spoint.action.Test">
			<result name="success">/welcome.jsp</result>
		</action>
	</package>
 
</struts>

Test.java

import com.opensymphony.xwork2.ActionSupport;
 
/**
 * This class is used as an action class.
 * @author w3spoint
 */
public class Test extends ActionSupport{
	//data members
	private String selectedSubject;
 
	//business logic
	public String execute(){
		return SUCCESS;	
	}
 
	//getter setters
	public String getSelectedSubject() {
		return selectedSubject;
	}
 
	public void setSelectedSubject(String selectedSubject) {
		this.selectedSubject = selectedSubject;
	}		
 
}

welcome.jsp

<%@ taglib uri="/struts-tags" prefix="s"%>
<html>
	<head>
	   <title>
            Struts 2 If, else if and else control tag example
           </title>
	</head>
	<body>
		<h3>
                 This is a If, else if and else control tag example.
                </h3>
 
		<s:set name="subject" value="selectedSubject"/>
 
		<s:if test="%{#subject=='java'}">
			Java is the selected subject.
		</s:if>
		<s:elseif test="%{#subject=='DBMS'}">
		    DBMS is the selected subject.
		</s:elseif>
		<s:else>
		    Unknown Subject.
		</s:else>
 
	</body>
</html>

Output:

struts 41 first   Select Subject. struts 41 second   Click on Submit button. struts 41 final   Download this example.   Next Topic: Struts 2 iterator control tag with example. Previous Topic: Struts 2 control tags with example.

 

Please follow and like us:
Content Protection by DMCA.com