If, ElseIf, Else tag in struts2 framework

If, ElseIf, Else tag in struts2 framework

Previous Home Next

 

Struts2 framework provide If, ElseIf and Else components to select the condition then give the output on the browser.
<s:if></s:if>
<s:elseif></s:elseif> 
<s:else></s:else>

The struts2 framework provide following tag ,

<s:if test="#studentlistStatus.even == true"></s:if>,
<s:elseif test="#studentlistStatus.first == true"></s:elseif>
<s:else></s:else>

 
Directory Structure of <s:if> & <s:elseif> tag Example in Struts 2.0 Using MyEclipse IDE



index.jsp

<%@taglib uri="/struts-tags" prefix="s"%>

<h1>If, Else, ElseIf tag example in struts2 framework</h1>
 
<s:set name="studentset" value="student"/>
 
<s:if test="%{#studentset=='boys'}">
	Boys Name is Mukund Singh.
</s:if>
<s:elseif test="%{#studentset=='girls'}">
    Girls Name is Babita Singh.
</s:elseif>
<s:else>
    Both Students Name Mukund Singh and Babita Singh.
</s:else>

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>f1</filter-name>
  <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
  </filter>
  <filter-mapping>
  <filter-name>f1</filter-name>
  <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration
2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="demo" extends="struts-default">
<action name="" class="org.r4r.IfElseAction">
<result name="success" type="dispatcher">/index.jsp</result>
</action>
</package>
</struts>  

IfElseAction.java

package org.r4r;

public class IfElseAction {
	String student="avd";
	public String execute(){
		return "success";
	}

	public String getStudent() {
		return student;
	}

	public void setStudent(String student) {
		this.student = student;
	}
}

Output






Previous Home Next