Pages Navigation Menu

Coding is much easier than you think

If, Else, ElseIf Conditional/Control tag example in Struts 2

 
Struts 2 If, ElseIf and Else tags are used to perform basic condition checking.

These are the following scenario in real world application to check the condition.

1. Condition checking using Boolean variable.
2. Condition checking using String object.
3. Condition checking using collection object / bean object
4. Condition checking using integer data type

 
** UPDATE: Struts 2 Complete tutorial now available here.
 
Now see the example on these scenario one by one.
 
1. Condition checking using Boolean variable.


   returning TRUE.


   returning FALSE.

 
2. Condition checking using String object.


   string is null


   string is not null

 
3. Condition checking using collection object / bean object


   object size is zero


   object size is greater than zero

 
4. Condition checking using integer data type


   integer is zero


   integer is greater than zero


   integer is lesser than zero

 
Recommended reading :

 
Jsp Page
 
On embedding all the above scenario in a complete Jsp page

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



ControlTag - If Else Condition


Control Tag

// For Boolean Value returning TRUE. returning FALSE. // For String Value string is null string is not null // For Integer Value integer is Zero integer is greater than zero integer is lesser than zero // For Object Value object size is zero object size is greater than zero

 
Do Read :

 

Acion Class
 

package com.simplecode.action;

import java.util.ArrayList;
import java.util.List;

import com.opensymphony.xwork2.Action;

public class ControlTag implements Action
{
	private List arrayList = new ArrayList();
	private String stringValue;
	private boolean booleanValue;
	private int integerValue;

	public String execute()
	{
		arrayList.add("Simple");
		arrayList.add("Code");
		arrayList.add("Stuffs");
		
		integerValue = -2;
		booleanValue = true;
		stringValue = "someValues";
		return SUCCESS;
	}

	public List getArrayList()
	{
		return arrayList;
	}

	public void setArrayList(List arrayList)
	{
		this.arrayList = arrayList;
	}

	public String getStringValue()
	{
		return stringValue;
	}

	public void setStringValue(String stringValue)
	{
		this.stringValue = stringValue;
	}

	public boolean isBooleanValue()
	{
		return booleanValue;
	}

	public void setBooleanValue(boolean booleanValue)
	{
		this.booleanValue = booleanValue;
	}

	public int getIntegerValue()
	{
		return integerValue;
	}

	public void setIntegerValue(int integerValue)
	{
		this.integerValue = integerValue;
	}
}

 
Demo
 
On runnig the application
 
ControlTag
 

One Comment

  1. I’m having multiple conditions, how to implemnt??

    for ex.,

    i’m getting following exception..

    org.apache.jasper.JasperException: Show.jsp(116,24) quote symbol expected