Today i am sharing the demo example for struts2 control tags IF , else and elseIF.This demo example gives you the better
idea to understand to use the control tags in your JSP page.
There are the following senario in real world application to check the condition.
1. Check condition using boolean variable.
2. Check condition using String object.
3. Check condition using collection object / bean object
How can we use struts tags IF , else and elseif condition to fullfill our requirement?
Demo Example steps by step:
1. Filter Dispatcher Entry in web.xml file
2. struts.xml file
3. Struts2 Acton Class
4. Create a JSP file
Now Run the following below URL in your browser :
If you have any query so please free feel to ask.
There are the following senario in real world application to check the condition.
1. Check condition using boolean variable.
2. Check condition using String object.
3. Check condition using collection object / bean object
How can we use struts tags IF , else and elseif condition to fullfill our requirement?
Demo Example steps by step:
1. Filter Dispatcher Entry in web.xml file
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<
web-app
version
=
"2.4"
xmlns
=
"http://java.sun.com/xml/ns/j2ee"
xmlns:xsi
=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<
filter
>
<
filter-name
>struts2</
filter-name
>
<
filter-class
>
org.apache.struts2.dispatcher.FilterDispatcher
</
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
>
2. struts.xml file
<!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.devMode"
value
=
"true"
/>
<
package
name
=
"struts2"
extends
=
"struts-default"
namespace
=
"/"
>
<
action
name
=
"ifElseAction"
>
<
result
>/ifelse.jsp</
result
>
</
action
>
</
package
>
</
struts
>
3. Struts2 Acton Class
package
com.example.actions;
import
java.util.ArrayList;
import
java.util.List;
import
com.opensymphony.xwork2.ActionSupport;
public
class
IfElseConditionAction
extends
ActionSupport {
private
List<String> checkArrayList =
new
ArrayList<String>();
private
String myStringValue;
private
boolean
myBooleanValue;
public
String execute() {
checkArrayList.add(
"aoiblog1"
);
checkArrayList.add(
"aoiblog2"
);
checkArrayList.add(
"aoiblog3"
);
checkArrayList.add(
"aoiblog4"
);
checkArrayList.add(
"aoiblog5"
);
myStringValue =
"aoi blog"
;
myBooleanValue =
true
;
return
SUCCESS;
}
public
List<String> getCheckArrayList() {
return
checkArrayList;
}
public
void
setCheckArrayList(List<String> checkArrayList) {
this
.checkArrayList = checkArrayList;
}
public
String getMyStringValue() {
return
myStringValue;
}
public
void
setMyStringValue(String myStringValue) {
this
.myStringValue = myStringValue;
}
public
boolean
isMyBooleanValue() {
return
myBooleanValue;
}
public
void
setMyBooleanValue(
boolean
myBooleanValue) {
this
.myBooleanValue = myBooleanValue;
}
}
4. Create a JSP file
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<
html
>
<
head
>
<
title
>AOI Blog - If Else Condition</
title
>
</
head
>
<
body
>
<!-- For Boolean Value -->
<
s:if
test
=
"myBooleanValue"
>
I am returning TRUE.<
br
/>
</
s:if
>
<
s:else
>
I am returning FALSE.<
br
/>
</
s:else
>
<!-- For String Value -->
<
s:if
test
=
"%{myStringValue!=null}"
>
String is not null<
br
/>
</
s:if
>
<
s:elseif
test
=
"%{myStringValue==null}"
>
String is null<
br
/>
</
s:elseif
>
<
s:else
>
String is null<
br
/>
</
s:else
>
<!-- For Object Value -->
<
s:if
test
=
"%{checkArrayList.size()==0}"
>
Object Size is Zero<
br
/>
</
s:if
>
<
s:else
>
Object Size is not a Zero<
br
/>
</
s:else
>
</
body
>
</
html
>
Now Run the following below URL in your browser :
http://localhost:8080/yourprojectName/ifElseAction.action
If you have any query so please free feel to ask.
No comments:
Post a Comment