Struts 2 if elseif tag sample code
If you want to perform basic condition on JSP page in struts application you could use “if” tag or with “elseif” or “else” tag. Please see working example below:
- Final project structure:
- Struts2SampleCodeAction.java:
package com.javahonk.action; import java.util.ArrayList; import java.util.List; import org.apache.struts2.convention.annotation.Action; import org.apache.struts2.convention.annotation.Result; import org.apache.struts2.convention.annotation.Results; import com.opensymphony.xwork2.ActionSupport; @Results({ @Result(name = "error", location = "/Struts2Example.jsp"), @Result(name = "input", location = "/Struts2Example.jsp")}) public class Struts2SampleCodeAction extends ActionSupport { private static final long serialVersionUID = 1L; private List<Person> personList = new ArrayList<Person>(); private String personName; @Override @Action(value = "/struts2SampleCodeAction") public String execute() throws Exception { personList.add(new Person("Java", "Honk", "NY")); personList.add(new Person("Ramesh", "Arrepu", "NY")); personList.add(new Person("Igor", "Vornovitsky", "NY")); personList.add(new Person("Bob", "Sidebottom", "NY")); personList.add(new Person("Sunil", "Garg", "NY")); personList.add(new Person("Mike", "Lee", "NY")); setPersonName("Java Honk"); return ActionSupport.INPUT; } public List<Person> getPersonList() { return personList; } public void setPersonList(List<Person> personList) { this.personList = personList; } public String getPersonName() { return personName; } public void setPersonName(String personName) { this.personName = personName; } }
- Person.java:
package com.javahonk.action; import java.io.Serializable; public class Person implements Serializable{ private static final long serialVersionUID = 1L; private String firstName; private String lastName; private String location; public Person(String firstName, String lastName, String location) { super(); this.firstName = firstName; this.lastName = lastName; this.location = location; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getLocation() { return location; } public void setLocation(String location) { this.location = location; } }
- Struts2Example.jsp:
page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib prefix="s" uri="/struts-tags"%> <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>Struts 2 if elseif else tag sample code</title> <s:head /> </head> <body> <h2 style="color: green"> <s:text name="label.welcome" /> </h2> <s:if test="personName == 'Java Honk'"> <ul style="list-style-type:disc"><li><s:text name="I am Java Honk" /></li></ul> </s:if> <s:elseif test="personName == 'Java Monk'"> <ul style="list-style-type:disc"><li><s:text name="I am Java Monk" /></li></ul> </s:elseif> <s:else> <ul style="list-style-type:disc"><li><s:text name="My name is not set" /></li></ul> </s:else> <h3 style="color: green"> <s:text name="label.iterator" /> </h3> <table cellspacing="0" cellpadding="0" border="1" width="500"> <tr style="font-weight: bold"> <td><s:text name="label.firstName" /></td> <td><s:text name="label.lastName" /></td> <td><s:text name="label.location" /></td> </tr> <s:iterator value="personList"> <tr> <s:if test="firstName == 'Bob' or firstName == 'Java'"> <td><s:property value="firstName" /></td> <td><s:property value="lastName" /></td> <td><s:property value="location" /></td> </s:if> </tr> </s:iterator> </table> </body> </html>
- Web page output:
For more information please read this tutorial
Download Project: Struts2ExampleCode