Control Tag - if-elseif-else

The Struts2 framework have a collection of control tags. It is used to control the execution behavior of data in the page. Find below details of each control tags :

if : It is used to implements the normal conditional flow of execution in page. This tag checks the condition and execute code defined in the body block upon condition is true.

<s:if test="%{true}">
It is body of if block
</s:if>

if-else : It is use for alternate execution path with if tag. It will be executed if if condition is false. Sometimes, we want to execute some statements if if condition is false. In such scenario we use else tag with if tag.

<s:if test="%{true}">
  Will be executed if condition got true
</s:if>
<s:else>
  Will be executed if condition is false
</s:els

if-elseif : It is used with if tag for adding multiple if conditional statements. If evaluation of if / elseif condition is false then control goes to the elseif for next condition. Sometimes we need to check multiple conditions with if tag, then we use elseif tag. It is always be part of if tag.

<s:if test="%{true}">
    It will be executed if condition is true
</s:if>
<s:elseif test="%{true}">
    It  will be executed if first if is false and this if is true
</s:elseif>
<s:else>
    It will be executed if all the above conditions are flase
</s:else>

Let's demostrate the if-elseif-else tag. You will get a form with options: India,Nepal,Pakistan,China,USA. Select option and select form and message display based on the condition.
Step 1: Create a Dynamic Web Project in eclipse - "Struts2IfTag".

Step 2: Edit deployment descriptor web.xml. Click here If you are not familiar with Struts2 application environment setup in Eclipse.

Step 3: The Struts2 jar files required in WEB-INF\lib folder in your project. Please read Readme.txt file in the attached source code for more details.

Step 4: Create a View page index.jsp :
<%@taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
  <h2>If tag example</h2>
  <hr>
  <form action="IfTagAction" method="post">
    Select Country : <select name="country">
      <option value="India">India</option>
      <option value="Nepal">Nepal</option>
      <option value="Pakistan">Pakistan</option>
      <option value="China">China</option>
      <option value="USA">USA</option>
    </select><br> <input type="submit">
  </form>
  <hr>
  <s:if test="country!=null">
    <s:if test="country=='India'">
      <s:property value="country" /> is our Country.
    </s:if>
    <s:elseif test="country=='Pakistan'">
      <s:property value="country" /> is our neighbor country.
    </s:elseif>
    <s:elseif test="country=='China' or country=='Nepal'">
      <s:property value="country" /> is also our neighbor country.
    </s:elseif>
    <s:else>
      <s:property value="country" /> is not our neighbor country.
    </s:else>
  </s:if>
  <hr>
  <a href="index.jsp">Try Again</a>
</body>
</html>

Step 5: Create struts 2 configuration file src\struts.xml :
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
  <constant name="devMode" value="true" />
  <package name="default" extends="struts-default">
     <action name="IfTagAction" class="techmyguru.generictags.IfAction">
    <result>index.jsp</result>
     </action>
  </package>
</struts>

Step 6: Create action class IfAction.java
package techmyguru.generictags;
import com.opensymphony.xwork2.ActionSupport;
public class IfAction extends ActionSupport {
private String country;
public String getCountry() {
  return country;
}
public void setCountry(String country) {
  this.country = country;
}
@Override
public String execute() throws Exception {
  return SUCCESS;
}
}

Step 7: Run the application and select India and click submit : You will get the below output.
Struts2 If control tag

Select Pakistan and submit form : You will get below output.
Struts2 If control tag

Select China and submit form : You will get below output.
Struts2 If control tag

Select USA and submit form : You will get below output.
Struts2 If control tag


Previous Download Source Code Next


Write your Comment

Name :
Email Id : [ We will not publish your email id ]
Your Comment : [ Maximum 200 chars ]
Enter Security code