Home Struts Struts Struts2.2.1 Tags Struts2.2.1 ElseIf Tag Example

Ask Questions?

View Latest Questions


 
 

Struts2.2.1 ElseIf Tag Example
Posted on: December 30, 2010 at 12:00 AM
Advertisement
The elseIf tag is used for decision making on the basis of condition specified in the If tag. elseIf tag always used with If tags.

Struts2.2.1 ElseIf Tag Example

The elseIf tag is used for decision making on the basis of condition specified in the If tag. elseIf tag always used with If tags. If the condition in the <s:if> tag is not satisfied or evaluate false then elseIf tag is execute and evalute the condition defined in the <s:elseIf > tag and then the evalution of the elseIf or Else tag place. if  <s:if>  tag evaluate false then the ElseIf tag is evaluated.if the <s:elseIf > tag is evaluate true then ElseIf executed othervise Else is executed.

The following Example will shows how to implement the ElseIf tag in the Struts2 --

First we create a JSP file named IfControlTag.jsp as follows.

<%@ page language="java" contentType="text/html; charset=UTF-8"

pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

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

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Insert title here</title>

</head>

<body>

<s:set name="Name" value="%{'Gyan'}" />

 

<s:if test="%{#Name=='S'}">You Working with--

<div><s:property value="%{#Name}" /></div>

<div>Your Name is Gyan</div>

</s:if>Evaluation of elseif condition

<s:elseif test="%{#Name=='Gyan'}">You Working with--

<div><s:property value="%{#Name}" /></div>

<div>My Name is Gyan</div>

</s:elseif>

<s:else>Evaluation of else condition

<div>Your Name is Not Specified</div>

</s:else>

</body>

</html>

The index.jsp file is as follows- This file only contains the hiperlink only.

<%@ page language="java" contentType="text/html; charset=UTF-8"

pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  

"http://www.w3.org/TR/html4/loose.dtd">

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

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>If Control Tag</title>

</head>

<body>

<ul>

<li><a href="IfControlTag.jsp">IF Control Tag Example</a></li>

</ul>

</body>

</html>

The Struts mapping file Struts.xml is as follows-

<?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.enable.DynamicMethodInvocation"

value="false" />

<constant name="struts.devMode" value="false" />

<constant name="struts.custom.i18n.resources"

value="ApplicationResources" />

<package name="gyan" extends="struts-default" namespace="/">

<action name="Result" class="gyan.Result" >

<result name="SUCCESS">/IfControlTag.jsp</result>

</action>

</package>

</struts>

The web config file web.xml is as follows-

<?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_2_5.xsd"
id="WebApp_ID" version="2.5">

<display-name>IfTagExample</display-name>

<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>/index.jsp</welcome-file>

</welcome-file-list>

</web-app>

The action class Result.java is as follows.

package gyan;

import com.opensymphony.xwork2.ActionSupport;

public class Result extends ActionSupport{

public String execute() throws Exception {

return SUCCESS;

}

This Program produces output on the basis of the <s:if> tag evaluation, This evaluate false and then elseif is evaluated and give the output as.

Output:-

Download Select Source Code


Related Tags for Struts2.2.1 ElseIf Tag Example:


DMCA.com