The If tag is used for decision making on the basis of condition specified in the tag. If tag could be used by itself or with Else If ,Else, or multiple Else tags. only one tag is evaluated at a time based on the condition which one is evaluated. If the condition in the <s:if> tag is satisfied or evaluate true then only this tag is execute and other discarded. if <s:if> evaluate false then the else tag is evaluated.
The following Example will shows how to implement the If tag in the Struts2.2.1 --
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=='Singh'}">You Working with-- <div><s:property value="%{#Name}" /></div> <div>Your Name is Gyan</div> </s:if> <s:elseif test="%{#Name=='Gyan'}">You Working with-- <div><s:property value="%{#Name}" /></div> <div>My Name is Gyan</div> </s:elseif> <s:else>for false 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 <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 true and give the output as-
Output:-
Download HQL Select Source Code
Advertisements
Ads
Ads