Struts2 Tags
1. Generic Tags - Generic Tag Reference
1.1) Control Tags
if
elseIf
else
append
generator
iterator
merge
sort
subset
1.2) Data Tags
a
action
bean
date
debug
include
param
property
push
set
text
url
2. UI Tags - UI Tag Reference
2.1) Form Tags
checkbox
checkboxlist
combobox
doubleselect
head
file
form
hidden
label
optiontransferselect
optgroup
password
radio
reset
select
submit
textarea
textfield
token
updownselect
2.2) Non-Form UI Tags
actionerror
actionmessage
component
div
fielderror
3 Ajax Tags
a
autocompleter
bind
datetimepicker
div
head
submit
tabbedpanel
textarea
tree
treenode
---------------------------------------------------------------------------------------------------------------------------
If/Elseif/else Tag
- 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.
Ex:
<s:set name="Name" value="%{'Kites'}" />
<s:if test="%{#Name=='Softwares'}">
You Working with--<div>
<s:property value="%{#Name}" /></div>
<div>Your Name is Kites</div>
</s:if>
<s:elseif test="%{#Name=='Kites'}">
You Working with--<div>
<s:property value="%{#Name}" /></div>
<div>My Name is Kites</div>
</s:elseif>
<s:else>
for false condition <div>Your Name is Not
Specified</div>
</s:else>
---------------------------------------------------------------------------------------------------------------
Append Tag
- Append tag is used to Append an iterator based on the ?id? attribute provided in the page.
- Struts2 append tag is used to combine few Iterators (created by List or Map) into a single Iterator.
- Append Iterator Tag is used to append iterators to form an appended iterator
Ex:
<body>
<h1>Struts 2 Append tag example</h1>
Combine ArrayList1 and ArrayList2
into a single iterator.
<s:append id="AppendList">
<s:param value="%{list1}" />
<s:param value="%{list2}" />
</s:append>
<s:iterator value="%{#AppendList}">
<li><s:property /></li>
</s:iterator>
</body>
----------------------------------------------------------------------------------------
Generator Tag
- Struts 2 generator tag is used to generate an iterator based on the ?val? attribute provided in the page.
- The generator tag is a generic tag that is used to generate iterators based on different attributes passed.
Ex:
<body>
<s:generator separator=","
val="%{'User1,User2,User3,User4'}" >
<s:iterator >
<s:property/><br/>
</s:iterator>
</s:generator>
</body>
------------------------------------------------------------------------------------------------
Iterator tag
- Iterator tag is used to iterate over a value.
- An iterable value can be either of: java.util.Collection, java.util.Iterator.
Ex:
<body>
<h2>Iterator tag of Struts2</h2>
Elements of ArrayList Collection.....
<s:iterator value="obList">
<s:property />
<br />
</s:iterator>
</body>
---------------------------------------------------------------------------------------------
Merge Tag
- The merge tag is a generic tag that is used to merge iterators.
Ex:
<body>
<h1>Struts 2 Merge tag example</h1>
Merge List1 and List2 into a single iterator.
<s:merge id="MergeList">
<s:param value="%{list1}" />
<s:param value="%{list2}" />
</s:merge>
<s:iterator value="%{#MergeList}">
<li><s:property /></li>
</s:iterator>
</body>
----------------------------------------------------------------------------------
sort Tag
The sort tag is used to sort a List using a java.util.Comparator. List and Comparator both passed in as the tag attribute.
Ex:
<body>
The UnSorted Values.<br>
<ol> <s:iterator value="list">
<LI><s:property value="id" /> ,
<s:property value="name" /> ,
<s:property value="age" /></LI>
</s:iterator> </ol>
<table>
<tr>
<td bgcolor="#ffffcc">The Values Sorted by ID.<br>
<s:bean name="kites.IdComparator" var="MyId" />
<s:sort comparator="#MyId" source="list">
<ol> <s:iterator>
<LI><s:property value="id" /> ,
<s:property value="name" /> ,
<s:property value="age" />
</LI>
</s:iterator> </ol>
</s:sort></td>
<td></td>
<td bgcolor="pink">
The Values Sorted by Name.<br>
<s:bean name="kites.NameComparator"
var="MyName" />
<s:sort comparator="#MyName"
source="list">
<ol> <s:iterator>
<LI><s:property value="id" /> ,
<s:property value="name" /> ,
<s:property value="age" /></LI>
</s:iterator></ol>
</s:sort></td>
</tr> </table>
</body>
---------------------------------------------------------------------------------------------------------
subset Tag
- The subset tag is a generic tag is used to output a subset or portion of an iterator elements.
Ex:
<body>
The Subset of first 4 values :<br>
<s:subset source="list1" count="4">
<s:iterator><s:property /><br></s:iterator>
</s:subset>
<br>The Subset of 4 values starting from 5th index :<br>
<s:subset source="list1" start="4" count="4">
<s:iterator><s:property /><br></s:iterator>
</s:subset>
</body>
----------------------------------------------------------------------------------------------------------
Anchor Tag
- It provides a hyperlink from current page to another page.
- It works as anchor tag of html, but syntax is different .
- In HTML - <a>, struts - <s:a>.
Ex:
<body>
<h1>Struts2 a(Anchor)Tag Example</h1>
<ul>
<li>
<s:url var="StrutsExample"
value="http://www.kitesnet.com" />
<s:a href="%{StrutsExample}">
Struts Tags Examples........</s:a>
</li>
<li><s:url value="welcome.jsp"
var="welcome" />
<s:a href="%{welcome}" >
Go to welcome page </s:a>
</li>
</ul>
</body>
---------------------------------------------------------------------------------------------------------
Action Tag
- The Action tag is used to call action class directly from a JSP page.
- We can call action directly by specifying the action name and an optional namespace.
- The body content of the tag is used to render the results from the Action.
Ex:
<body>
<s:action name="ActionTag" >This is The Action.<br>
<b>Hello Dear!</b>
</s:action>
</body>
------------------------------------------------------------------------------------------------------
Bean Tag
The Bean tag is a generic tag that is used to instantiates a class that confirms to the JavaBeans specification.
Ex:
<body>
<h1>Value of paisa into rupee </h1>
<s:bean name="kites.javabean.ConvetPaisaIntoRupees">
<s:param name="paisa" value="100" />
100 Paisa = <s:property value="rupees" /> Rupees
</s:bean>
</body>
-----------------------------------------------------------------------------------------------------
Date Tag
- Date tag is used to output the date object into the required format.
Ex:
<body>
<h2>Struts2_Date_Tag_Example.</h2><hr>
Current Date :
<s:date name="currentDate" format="yyyy-MM-dd"></s:date>
</body>
-------------------------------------------------------------------------------------------------------
Debug Tag
- The debug tag is a very useful debugging tag to output the content of the 'Value Stack' and also the 'Stack Context' details in the web page.
- The <s:debug /> will generate a text link named 'debug', you need to click on the text link to expand the debugging details.
Ex:
<body>
<s:debug/>
</body>
-------------------------------------------------------------------------------------------------------
include tag
- It is a generic tag that is used to include the output of html or jsp pages directly into current page.
- We can include output of another web resource into current page.
Ex:
<body><h2>Struts2_Include_tag_Example</h2>
<hr><h3>Value of include.jsp page</h3>
<s:include value="/include.jsp"></s:include>
</body>
-----------------------------------------------------------------------------------------------------------
param Tag
- This tag can be used to parameterize other tags.
- The parameters can be added with or without a name as key.
- When we declare the param tag, the value can be defined in either a value attribute or as text between the start and end tag.
- This tag has the following two paramters.
name (String) - the name of the parameter
value (Object) - the value of the parameter
Ex:
<body>
<s:bean name="kites.ParamTag" var="paramTag">
<s:param name="name">Person1</s:param>
<s:param name="age">25</s:param>
<s:param name="status">Single</s:param>
<s:param name="birthday">1st March</s:param>
</s:bean>
<ol>
<li>Name :
<s:property value="#paramTag.name" /></li>
<li>Age :
<s:property value="#paramTag.age" /></li>
<li>Status :
<s:property value="#paramTag.status" /></li>
<li>Birthday :
<s:property value="#paramTag.birthday" />
</li>
</ol>
</body>
-----------------------------------------------------------------------------------------------------------------------------
property Tag
- The property tag is a generic tag that is used to get the property of a value, which will default to the top of the stack if none is specified.
Ex:
<body>
First Name: <s:property value="fname" />
<br>
Last Name: <s:property value="lname" />
</body>
-----------------------------------------------------------------------------------------------------------------------------
push tag
- The push tag push a value onto the top of stack.
- So it can access easily by using first-level of OGNL expression language.
Ex:
<body><h2>struts2_push_tag_Example</h2>
<hr/>
<b>Simple </b><br/><br/>
<s:bean name="kites.bean.EmployeeBean"
var="empBean">
Name :
<s:property value="#empBean.emp_name"/><br/>
Address : <s:property value="#empBean.address"/>
</s:bean>
<hr/><b>Using push tag</b><br/><br/>
<s:push value="#empBean">
Name : <s:property value="emp_name"/><br/>
Address : <s:property value="address"/>
</s:push>
</body>
--------------------------------------------------------------------------------------------------------------------
Set Tag
- The set tag is a generic (Data) tag that is used to assign a value of property to another name in a specified scope.
Ex:
<body>
<h2>Struts _ SetTag_Example</h2>
<hr><s:bean name="kites.action.StudentInfoBean" var="infoBean">
</s:bean>
<h4>Simple use of set tag </h4><br/>
<s:set name="name" value="#infoBean.name" />
<s:set name="age" value="#infoBean.age" />
Student Name :<s:property value="name"/>
<br/><br/>
Student Age :
<s:property value="age"/><br/><br/><hr/>
<h4>
Set value of property in session by set tag
</h4><br/>
<s:set name="name" value="#infoBean.name"
scope="session"/>
<s:set name="age" value="#infoBean.age"
scope="session" />
Student Name :<s:property
value="#session['name']"/>
<br/><br/>
Student Age :
<s:property value="#session['age']"/><br/><br/>
</body>
------------------------------------------------------------------------------------------------------------------------
url Tag
This tag is used.to to create an URL and output it as a text format
Ex:
<s:url action="URLtag.action" var="URLtag">
<s:param name="name">Kites Softwares</s:param>
</s:url>
<s:a href="%{URLtag}">URL Tag Action (via %)</s:a>
------------------------------------------------------------------------------------------------------------------------
UI Tags
Form Tags
checkbox tag
- The checkbox tag is a UI tag that is used to render an HTML input element of type checkbox.
Ex:
<s:checkbox name="hello" label="Hello" value="true"> </s:checkbox>
<s:checkbox name="hi" label="Hi"> </s:checkbox>
<s:checkbox name="gm" label="GoodMorning">
</s:checkbox>
--------------------------------------------------------------------------------------------------------------
checkboxlist tag
- The checkboxlist tag is a UI tag that creates a series of checkboxes from a list.
Ex:
<s:checkboxlist label="Select Cities" list="cities" name="mycity" />
-------------------------------------------------------------------------------------------------------------
combobox tag
- The combobox is basically an HTML INPUT of type text and HTML SELECT grouped together to give you a combo box functionality.
Ex:
<body><h1>Struts2_Combobox_Tag_Example</h1><hr/>
<b>Select Date of birth.... </b>
<s:form action="comboBoxResult.action" namespace="/">
<h4>
<s:combobox label="Date"
headerKey="-1" headerValue="--- Select ---"
list="dates"
name="yourDate" />
</h4>
<h4>
<s:combobox label="Month"
headerKey="-1" headerValue="--- Select ---"
list="months" name="yourMonth" />
</h4>
<h4>
<s:combobox label="Year"
headerKey="-1" headerValue="--- Select ---"
list="years" name="yourYear" />
</h4>
<s:submit value="submit" name="submit" />
</s:form>
</body>
-------------------------------------------------------------------------------------------------------------------------
doubleselect tag
- The <s:doubleselect> tag is used to create two HTML drop down boxes, once the first drop down list is selected, the second drop down list will be change accordingly.
Ex:
<s:doubleselect label="Qualification" name="list1"
doubleList="top == 'Degree' ? {'MCA', 'MBA','MTech','BTech','MSc(IT)'} :
{'PGDCA', 'PGDM','PGDBM','PGDSM'}"
list="{'Degree','Diploma'}" doubleName="list2" />
------------------------------------------------------------------------------------------------------------------------
head Tag
- The <s:head> tag is used to renders parts of the HEAD section for an HTML file.
Ex:
1)
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<s:head />
</head>
<body>
2)
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>My page</title>
<s:head theme="ajax" calendarcss="calendar-green"/>
</head>
<body>
------------------------------------------------------------------------------------------------------------------------
file upload
- We use a struts2 file tag for uploading a file.
Ex:
JSP Part
--------
<body>
<h1>Struts2_file_Tag_Example</h1><hr/>
<s:form action="fileUploadAction.action" method="post"
enctype="multipart/form-data" namespace="/">
<s:file name="Uploadfile" label="Upload file : "> </s:file>
<s:submit label="Submit"></s:submit></s:form>
</body>
struts.xml part
---------------
<action name="fileUploadAction" class="kites.action.FileUploadAction">
<interceptor-ref name="fileUpload">
<param name="allowedTypes">text/html</param>
<param name="maximumSize">200</param>
</interceptor-ref>
<interceptor-ref name="params">
<param name="excludeParams">
dojo\..*,^struts\..*</param>
</interceptor-ref>
<interceptor-ref name="validation" >
</interceptor-ref>
<interceptor-ref name="workflow">
<param name="excludeMethods">
input,back,cancel,browse
</param>
</interceptor-ref>
<result name="success">jsp/fileUploadSuccess.jsp
</result>
<result name="input">jsp/fileupload.jsp</result>
</action>
-------------------------------------------------------------------------------------
Form/Hidden/TextField Tags
Ex:
<s:form action="Information.action">
<s:label name="formName"
value="Student Registration Form........"/>
<s:textfield name="name" label="Name"></s:textfield>
<s:textarea name="address"
label="Address" cssClass="bh"></s:textarea>
<s:password label="Password" name="password">
</s:password>
<s:radio list="{'Male','Female'}"
name="gender" label="Gender" ></s:radio>
<s:select list="course"
name="courseName" headerKey="0"
headerValue="Select Course" label="Course Name">
</s:select>
<s:hidden name="message"
value="Hello friends....." >
</s:hidden>
<s:reset></s:reset> <br><s:submit></s:submit>
</s:form>
---------------------------------------------------------------------------------------------------
label Tag
- The label tag is used to renders an HTML LABEL that allow to output <s: label name=? ? value=? ? /> combination
Ex:
<body>
label1:<s:label value="Kites" /><br>
label2:<s:label value="Softwares" />
<br>
<s:textfield label="Kites"/><br>
<s:textfield label="Softwares"/>
</body>
-----------------------------------------------------------------------------------------------------
optiontransferselect tag
- The Optiontransferselect tag is a generic UI tag that creates an option transfer select component.
- There are two <select ...> tags with buttons in the middle of them.
- With the help of these button tag, you can transfer selected data form one select box to another select box.
Ex:
<body><h2>Struts2_Optiontransferselect_Example1
</h2><hr>
<s:form action="resultAction.action">
<s:optiontransferselect
leftTitle="List of indian cities.. "
headerKey="0"
headerValue="----Select Indian city-----"
name="IndiaCity"
list= "indianCityName"
doubleHeaderKey="0"
doubleHeaderValue="----Select American
city-----"
rightTitle="List of American cities... "
doubleList="americanCityName"
doubleName="americanCity">
</s:optiontransferselect>
<s:submit> </s:submit>
</s:form></body>
------------------------------------------------------------------------------------------------------
optgroup tag
- The optgroup tag is a generic Form_UI tag that creates an optgroup component which needs to reside within a select tag <s:select>.
Ex:
<body><h1>StrutsOptGroup_Tag_Example</h1><hr>
<s:form action="ResultOptGroup.action">
<s:select label="Select City"
name="Cites"
list="nameofCity">
<s:optgroup label="City_of_UP"
list=" upCitiesName" />
<s:optgroup label="City_Of_UttraKhand"
list="%{#{'Rudrapur':'Rudrapur','RamNagar':'RamNagar','Doon':'Doon'}}" />
</s:select>
<s:submit></s:submit>
</s:form>
</body>
------------------------------------------------------------------------------------------------------
password tag
- It is a UI tag in struts framework. It display a input tag of type password.
Ex:
<s:password name="password" key="Password:"></s:password>
----------------------------------------------------------------------------------------------
radio Tag
The radio tag is a UI tag that render a radio button input field.
Ex:
<body>
<s:form action="ResultRadioTag.action">
<h2>Fill Your Details.</h2>
<s:textfield name="username" key="Name">
</s:textfield>
<s:radio list="#{'Male':'Male','Female':'Female'}"
value="Gender" name="list1">
</s:radio>
<s:radio
list="#{'Single':'Single','Married':'Married'}"
value="Status"
name="list2"></s:radio>
<s:radio
list="#{'UnEmployed':'UnEmployed','Employed':'Employed',
'Self Employed':'Self Employed'}"
value="Employement" name="list3"></s:radio>
<s:submit></s:submit>
</s:form>
</body>
-----------------------------------------------------------------------------------------------
token tag
- The s:token tag merely places a hidden element that contains the unique token.
Ex:
<body><h1>Struts_Token_Example</h1><hr/>
<s:form action="tokenAction">
<s:textfield label="Name" name="name"></s:textfield>
<s:textfield name="age" label="Age"></s:textfield>
<s:token name="token"></s:token>
<s:submit></s:submit>
</s:form></body>
--------------------------------------------------------------------------------------------------------
updownselect Tag
- The updownselect tag is a UI tag that creates a HTML select component with buttons to move up and down the elements in the select component.
Ex:
<body>
UpdownSelect Tag Example
<s:form action="ResultUpDownSelect">
<s:updownselect
list="#{'Fun Cinema':'Fun Cinema','PVR':'PVR','WAVE':'WAVE'
,'INOX':'INOX','EROSE':'EROSE'}"
name="favMultiplex" headerKey="-1" headerValue="--- Please Select ---"
size="4" />
<s:submit></s:submit>
</s:form>
</body>
-------------------------------------------------------------------------------------------------------
actionerror and actionmessage tag
- It is a non-form UI tag in struts framework.
Ex:
<body><h1>Actionerror_Tag_Example</h1><hr>
<font color="Red" ><s:actionerror/></font>
<s:form action="LoginValidation">
<s:textfield name="loginID" label="LoginID">
</s:textfield>
<s:submit label="Login"></s:submit>
</s:form></body>
<td >
<font color="green"><s:actionmessage/></font></td>
Action Class
public String execute(){
if (getLoginID().equals("kites")) {
addActionMessage("You are a valid user.");
return SUCCESS;
}
else {
addActionError(
"Please enter valid login id");
return ERROR;
}
}
-------------------------------------------------------------------------------------------------------
fielderror Tag
- The fielderror tag is a UI tag that render field errors if they exists.
Ex:
<body>
<s:actionmessage />
<s:form action="ResultFieldErrorTag.action">
<s:textfield name="userName" key="Username">
</s:textfield>
<s:password name="password" key="Password:">
</s:password>
<s:submit></s:submit>
</s:form>
</body>
Action Class
public String execute() {
if (getUserName().equals("Kites")
&& getPassword().equals("Kites")) {
addActionMessage(
"You are a valid user.");
return SUCCESS;
}
if (!(getUserName().equals("Kites")))
addFieldError(
"userName", "Invalid username!");
if (!(getPassword().equals("Kites")))
addFieldError(
"password", "Invalid password!");
return ERROR;
}
--------------------------------------------------------------------------------------------------
1. Generic Tags - Generic Tag Reference
1.1) Control Tags
if
elseIf
else
append
generator
iterator
merge
sort
subset
1.2) Data Tags
a
action
bean
date
debug
include
param
property
push
set
text
url
2. UI Tags - UI Tag Reference
2.1) Form Tags
checkbox
checkboxlist
combobox
doubleselect
head
file
form
hidden
label
optiontransferselect
optgroup
password
radio
reset
select
submit
textarea
textfield
token
updownselect
2.2) Non-Form UI Tags
actionerror
actionmessage
component
div
fielderror
3 Ajax Tags
a
autocompleter
bind
datetimepicker
div
head
submit
tabbedpanel
textarea
tree
treenode
---------------------------------------------------------------------------------------------------------------------------
If/Elseif/else Tag
- 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.
Ex:
<s:set name="Name" value="%{'Kites'}" />
<s:if test="%{#Name=='Softwares'}">
You Working with--<div>
<s:property value="%{#Name}" /></div>
<div>Your Name is Kites</div>
</s:if>
<s:elseif test="%{#Name=='Kites'}">
You Working with--<div>
<s:property value="%{#Name}" /></div>
<div>My Name is Kites</div>
</s:elseif>
<s:else>
for false condition <div>Your Name is Not
Specified</div>
</s:else>
---------------------------------------------------------------------------------------------------------------
Append Tag
- Append tag is used to Append an iterator based on the ?id? attribute provided in the page.
- Struts2 append tag is used to combine few Iterators (created by List or Map) into a single Iterator.
- Append Iterator Tag is used to append iterators to form an appended iterator
Ex:
<body>
<h1>Struts 2 Append tag example</h1>
Combine ArrayList1 and ArrayList2
into a single iterator.
<s:append id="AppendList">
<s:param value="%{list1}" />
<s:param value="%{list2}" />
</s:append>
<s:iterator value="%{#AppendList}">
<li><s:property /></li>
</s:iterator>
</body>
----------------------------------------------------------------------------------------
Generator Tag
- Struts 2 generator tag is used to generate an iterator based on the ?val? attribute provided in the page.
- The generator tag is a generic tag that is used to generate iterators based on different attributes passed.
Ex:
<body>
<s:generator separator=","
val="%{'User1,User2,User3,User4'}" >
<s:iterator >
<s:property/><br/>
</s:iterator>
</s:generator>
</body>
------------------------------------------------------------------------------------------------
Iterator tag
- Iterator tag is used to iterate over a value.
- An iterable value can be either of: java.util.Collection, java.util.Iterator.
Ex:
<body>
<h2>Iterator tag of Struts2</h2>
Elements of ArrayList Collection.....
<s:iterator value="obList">
<s:property />
<br />
</s:iterator>
</body>
---------------------------------------------------------------------------------------------
Merge Tag
- The merge tag is a generic tag that is used to merge iterators.
Ex:
<body>
<h1>Struts 2 Merge tag example</h1>
Merge List1 and List2 into a single iterator.
<s:merge id="MergeList">
<s:param value="%{list1}" />
<s:param value="%{list2}" />
</s:merge>
<s:iterator value="%{#MergeList}">
<li><s:property /></li>
</s:iterator>
</body>
----------------------------------------------------------------------------------
sort Tag
The sort tag is used to sort a List using a java.util.Comparator. List and Comparator both passed in as the tag attribute.
Ex:
<body>
The UnSorted Values.<br>
<ol> <s:iterator value="list">
<LI><s:property value="id" /> ,
<s:property value="name" /> ,
<s:property value="age" /></LI>
</s:iterator> </ol>
<table>
<tr>
<td bgcolor="#ffffcc">The Values Sorted by ID.<br>
<s:bean name="kites.IdComparator" var="MyId" />
<s:sort comparator="#MyId" source="list">
<ol> <s:iterator>
<LI><s:property value="id" /> ,
<s:property value="name" /> ,
<s:property value="age" />
</LI>
</s:iterator> </ol>
</s:sort></td>
<td></td>
<td bgcolor="pink">
The Values Sorted by Name.<br>
<s:bean name="kites.NameComparator"
var="MyName" />
<s:sort comparator="#MyName"
source="list">
<ol> <s:iterator>
<LI><s:property value="id" /> ,
<s:property value="name" /> ,
<s:property value="age" /></LI>
</s:iterator></ol>
</s:sort></td>
</tr> </table>
</body>
---------------------------------------------------------------------------------------------------------
subset Tag
- The subset tag is a generic tag is used to output a subset or portion of an iterator elements.
Ex:
<body>
The Subset of first 4 values :<br>
<s:subset source="list1" count="4">
<s:iterator><s:property /><br></s:iterator>
</s:subset>
<br>The Subset of 4 values starting from 5th index :<br>
<s:subset source="list1" start="4" count="4">
<s:iterator><s:property /><br></s:iterator>
</s:subset>
</body>
----------------------------------------------------------------------------------------------------------
Anchor Tag
- It provides a hyperlink from current page to another page.
- It works as anchor tag of html, but syntax is different .
- In HTML - <a>, struts - <s:a>.
Ex:
<body>
<h1>Struts2 a(Anchor)Tag Example</h1>
<ul>
<li>
<s:url var="StrutsExample"
value="http://www.kitesnet.com" />
<s:a href="%{StrutsExample}">
Struts Tags Examples........</s:a>
</li>
<li><s:url value="welcome.jsp"
var="welcome" />
<s:a href="%{welcome}" >
Go to welcome page </s:a>
</li>
</ul>
</body>
---------------------------------------------------------------------------------------------------------
Action Tag
- The Action tag is used to call action class directly from a JSP page.
- We can call action directly by specifying the action name and an optional namespace.
- The body content of the tag is used to render the results from the Action.
Ex:
<body>
<s:action name="ActionTag" >This is The Action.<br>
<b>Hello Dear!</b>
</s:action>
</body>
------------------------------------------------------------------------------------------------------
Bean Tag
The Bean tag is a generic tag that is used to instantiates a class that confirms to the JavaBeans specification.
Ex:
<body>
<h1>Value of paisa into rupee </h1>
<s:bean name="kites.javabean.ConvetPaisaIntoRupees">
<s:param name="paisa" value="100" />
100 Paisa = <s:property value="rupees" /> Rupees
</s:bean>
</body>
-----------------------------------------------------------------------------------------------------
Date Tag
- Date tag is used to output the date object into the required format.
Ex:
<body>
<h2>Struts2_Date_Tag_Example.</h2><hr>
Current Date :
<s:date name="currentDate" format="yyyy-MM-dd"></s:date>
</body>
-------------------------------------------------------------------------------------------------------
Debug Tag
- The debug tag is a very useful debugging tag to output the content of the 'Value Stack' and also the 'Stack Context' details in the web page.
- The <s:debug /> will generate a text link named 'debug', you need to click on the text link to expand the debugging details.
Ex:
<body>
<s:debug/>
</body>
-------------------------------------------------------------------------------------------------------
include tag
- It is a generic tag that is used to include the output of html or jsp pages directly into current page.
- We can include output of another web resource into current page.
Ex:
<body><h2>Struts2_Include_tag_Example</h2>
<hr><h3>Value of include.jsp page</h3>
<s:include value="/include.jsp"></s:include>
</body>
-----------------------------------------------------------------------------------------------------------
param Tag
- This tag can be used to parameterize other tags.
- The parameters can be added with or without a name as key.
- When we declare the param tag, the value can be defined in either a value attribute or as text between the start and end tag.
- This tag has the following two paramters.
name (String) - the name of the parameter
value (Object) - the value of the parameter
Ex:
<body>
<s:bean name="kites.ParamTag" var="paramTag">
<s:param name="name">Person1</s:param>
<s:param name="age">25</s:param>
<s:param name="status">Single</s:param>
<s:param name="birthday">1st March</s:param>
</s:bean>
<ol>
<li>Name :
<s:property value="#paramTag.name" /></li>
<li>Age :
<s:property value="#paramTag.age" /></li>
<li>Status :
<s:property value="#paramTag.status" /></li>
<li>Birthday :
<s:property value="#paramTag.birthday" />
</li>
</ol>
</body>
-----------------------------------------------------------------------------------------------------------------------------
property Tag
- The property tag is a generic tag that is used to get the property of a value, which will default to the top of the stack if none is specified.
Ex:
<body>
First Name: <s:property value="fname" />
<br>
Last Name: <s:property value="lname" />
</body>
-----------------------------------------------------------------------------------------------------------------------------
push tag
- The push tag push a value onto the top of stack.
- So it can access easily by using first-level of OGNL expression language.
Ex:
<body><h2>struts2_push_tag_Example</h2>
<hr/>
<b>Simple </b><br/><br/>
<s:bean name="kites.bean.EmployeeBean"
var="empBean">
Name :
<s:property value="#empBean.emp_name"/><br/>
Address : <s:property value="#empBean.address"/>
</s:bean>
<hr/><b>Using push tag</b><br/><br/>
<s:push value="#empBean">
Name : <s:property value="emp_name"/><br/>
Address : <s:property value="address"/>
</s:push>
</body>
--------------------------------------------------------------------------------------------------------------------
Set Tag
- The set tag is a generic (Data) tag that is used to assign a value of property to another name in a specified scope.
Ex:
<body>
<h2>Struts _ SetTag_Example</h2>
<hr><s:bean name="kites.action.StudentInfoBean" var="infoBean">
</s:bean>
<h4>Simple use of set tag </h4><br/>
<s:set name="name" value="#infoBean.name" />
<s:set name="age" value="#infoBean.age" />
Student Name :<s:property value="name"/>
<br/><br/>
Student Age :
<s:property value="age"/><br/><br/><hr/>
<h4>
Set value of property in session by set tag
</h4><br/>
<s:set name="name" value="#infoBean.name"
scope="session"/>
<s:set name="age" value="#infoBean.age"
scope="session" />
Student Name :<s:property
value="#session['name']"/>
<br/><br/>
Student Age :
<s:property value="#session['age']"/><br/><br/>
</body>
------------------------------------------------------------------------------------------------------------------------
url Tag
This tag is used.to to create an URL and output it as a text format
Ex:
<s:url action="URLtag.action" var="URLtag">
<s:param name="name">Kites Softwares</s:param>
</s:url>
<s:a href="%{URLtag}">URL Tag Action (via %)</s:a>
------------------------------------------------------------------------------------------------------------------------
UI Tags
Form Tags
checkbox tag
- The checkbox tag is a UI tag that is used to render an HTML input element of type checkbox.
Ex:
<s:checkbox name="hello" label="Hello" value="true"> </s:checkbox>
<s:checkbox name="hi" label="Hi"> </s:checkbox>
<s:checkbox name="gm" label="GoodMorning">
</s:checkbox>
--------------------------------------------------------------------------------------------------------------
checkboxlist tag
- The checkboxlist tag is a UI tag that creates a series of checkboxes from a list.
Ex:
<s:checkboxlist label="Select Cities" list="cities" name="mycity" />
-------------------------------------------------------------------------------------------------------------
combobox tag
- The combobox is basically an HTML INPUT of type text and HTML SELECT grouped together to give you a combo box functionality.
Ex:
<body><h1>Struts2_Combobox_Tag_Example</h1><hr/>
<b>Select Date of birth.... </b>
<s:form action="comboBoxResult.action" namespace="/">
<h4>
<s:combobox label="Date"
headerKey="-1" headerValue="--- Select ---"
list="dates"
name="yourDate" />
</h4>
<h4>
<s:combobox label="Month"
headerKey="-1" headerValue="--- Select ---"
list="months" name="yourMonth" />
</h4>
<h4>
<s:combobox label="Year"
headerKey="-1" headerValue="--- Select ---"
list="years" name="yourYear" />
</h4>
<s:submit value="submit" name="submit" />
</s:form>
</body>
-------------------------------------------------------------------------------------------------------------------------
doubleselect tag
- The <s:doubleselect> tag is used to create two HTML drop down boxes, once the first drop down list is selected, the second drop down list will be change accordingly.
Ex:
<s:doubleselect label="Qualification" name="list1"
doubleList="top == 'Degree' ? {'MCA', 'MBA','MTech','BTech','MSc(IT)'} :
{'PGDCA', 'PGDM','PGDBM','PGDSM'}"
list="{'Degree','Diploma'}" doubleName="list2" />
------------------------------------------------------------------------------------------------------------------------
head Tag
- The <s:head> tag is used to renders parts of the HEAD section for an HTML file.
Ex:
1)
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<s:head />
</head>
<body>
2)
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>My page</title>
<s:head theme="ajax" calendarcss="calendar-green"/>
</head>
<body>
------------------------------------------------------------------------------------------------------------------------
file upload
- We use a struts2 file tag for uploading a file.
Ex:
JSP Part
--------
<body>
<h1>Struts2_file_Tag_Example</h1><hr/>
<s:form action="fileUploadAction.action" method="post"
enctype="multipart/form-data" namespace="/">
<s:file name="Uploadfile" label="Upload file : "> </s:file>
<s:submit label="Submit"></s:submit></s:form>
</body>
struts.xml part
---------------
<action name="fileUploadAction" class="kites.action.FileUploadAction">
<interceptor-ref name="fileUpload">
<param name="allowedTypes">text/html</param>
<param name="maximumSize">200</param>
</interceptor-ref>
<interceptor-ref name="params">
<param name="excludeParams">
dojo\..*,^struts\..*</param>
</interceptor-ref>
<interceptor-ref name="validation" >
</interceptor-ref>
<interceptor-ref name="workflow">
<param name="excludeMethods">
input,back,cancel,browse
</param>
</interceptor-ref>
<result name="success">jsp/fileUploadSuccess.jsp
</result>
<result name="input">jsp/fileupload.jsp</result>
</action>
-------------------------------------------------------------------------------------
Form/Hidden/TextField Tags
Ex:
<s:form action="Information.action">
<s:label name="formName"
value="Student Registration Form........"/>
<s:textfield name="name" label="Name"></s:textfield>
<s:textarea name="address"
label="Address" cssClass="bh"></s:textarea>
<s:password label="Password" name="password">
</s:password>
<s:radio list="{'Male','Female'}"
name="gender" label="Gender" ></s:radio>
<s:select list="course"
name="courseName" headerKey="0"
headerValue="Select Course" label="Course Name">
</s:select>
<s:hidden name="message"
value="Hello friends....." >
</s:hidden>
<s:reset></s:reset> <br><s:submit></s:submit>
</s:form>
---------------------------------------------------------------------------------------------------
label Tag
- The label tag is used to renders an HTML LABEL that allow to output <s: label name=? ? value=? ? /> combination
Ex:
<body>
label1:<s:label value="Kites" /><br>
label2:<s:label value="Softwares" />
<br>
<s:textfield label="Kites"/><br>
<s:textfield label="Softwares"/>
</body>
-----------------------------------------------------------------------------------------------------
optiontransferselect tag
- The Optiontransferselect tag is a generic UI tag that creates an option transfer select component.
- There are two <select ...> tags with buttons in the middle of them.
- With the help of these button tag, you can transfer selected data form one select box to another select box.
Ex:
<body><h2>Struts2_Optiontransferselect_Example1
</h2><hr>
<s:form action="resultAction.action">
<s:optiontransferselect
leftTitle="List of indian cities.. "
headerKey="0"
headerValue="----Select Indian city-----"
name="IndiaCity"
list= "indianCityName"
doubleHeaderKey="0"
doubleHeaderValue="----Select American
city-----"
rightTitle="List of American cities... "
doubleList="americanCityName"
doubleName="americanCity">
</s:optiontransferselect>
<s:submit> </s:submit>
</s:form></body>
------------------------------------------------------------------------------------------------------
optgroup tag
- The optgroup tag is a generic Form_UI tag that creates an optgroup component which needs to reside within a select tag <s:select>.
Ex:
<body><h1>StrutsOptGroup_Tag_Example</h1><hr>
<s:form action="ResultOptGroup.action">
<s:select label="Select City"
name="Cites"
list="nameofCity">
<s:optgroup label="City_of_UP"
list=" upCitiesName" />
<s:optgroup label="City_Of_UttraKhand"
list="%{#{'Rudrapur':'Rudrapur','RamNagar':'RamNagar','Doon':'Doon'}}" />
</s:select>
<s:submit></s:submit>
</s:form>
</body>
------------------------------------------------------------------------------------------------------
password tag
- It is a UI tag in struts framework. It display a input tag of type password.
Ex:
<s:password name="password" key="Password:"></s:password>
----------------------------------------------------------------------------------------------
radio Tag
The radio tag is a UI tag that render a radio button input field.
Ex:
<body>
<s:form action="ResultRadioTag.action">
<h2>Fill Your Details.</h2>
<s:textfield name="username" key="Name">
</s:textfield>
<s:radio list="#{'Male':'Male','Female':'Female'}"
value="Gender" name="list1">
</s:radio>
<s:radio
list="#{'Single':'Single','Married':'Married'}"
value="Status"
name="list2"></s:radio>
<s:radio
list="#{'UnEmployed':'UnEmployed','Employed':'Employed',
'Self Employed':'Self Employed'}"
value="Employement" name="list3"></s:radio>
<s:submit></s:submit>
</s:form>
</body>
-----------------------------------------------------------------------------------------------
token tag
- The s:token tag merely places a hidden element that contains the unique token.
Ex:
<body><h1>Struts_Token_Example</h1><hr/>
<s:form action="tokenAction">
<s:textfield label="Name" name="name"></s:textfield>
<s:textfield name="age" label="Age"></s:textfield>
<s:token name="token"></s:token>
<s:submit></s:submit>
</s:form></body>
--------------------------------------------------------------------------------------------------------
updownselect Tag
- The updownselect tag is a UI tag that creates a HTML select component with buttons to move up and down the elements in the select component.
Ex:
<body>
UpdownSelect Tag Example
<s:form action="ResultUpDownSelect">
<s:updownselect
list="#{'Fun Cinema':'Fun Cinema','PVR':'PVR','WAVE':'WAVE'
,'INOX':'INOX','EROSE':'EROSE'}"
name="favMultiplex" headerKey="-1" headerValue="--- Please Select ---"
size="4" />
<s:submit></s:submit>
</s:form>
</body>
-------------------------------------------------------------------------------------------------------
actionerror and actionmessage tag
- It is a non-form UI tag in struts framework.
Ex:
<body><h1>Actionerror_Tag_Example</h1><hr>
<font color="Red" ><s:actionerror/></font>
<s:form action="LoginValidation">
<s:textfield name="loginID" label="LoginID">
</s:textfield>
<s:submit label="Login"></s:submit>
</s:form></body>
<td >
<font color="green"><s:actionmessage/></font></td>
Action Class
public String execute(){
if (getLoginID().equals("kites")) {
addActionMessage("You are a valid user.");
return SUCCESS;
}
else {
addActionError(
"Please enter valid login id");
return ERROR;
}
}
-------------------------------------------------------------------------------------------------------
fielderror Tag
- The fielderror tag is a UI tag that render field errors if they exists.
Ex:
<body>
<s:actionmessage />
<s:form action="ResultFieldErrorTag.action">
<s:textfield name="userName" key="Username">
</s:textfield>
<s:password name="password" key="Password:">
</s:password>
<s:submit></s:submit>
</s:form>
</body>
Action Class
public String execute() {
if (getUserName().equals("Kites")
&& getPassword().equals("Kites")) {
addActionMessage(
"You are a valid user.");
return SUCCESS;
}
if (!(getUserName().equals("Kites")))
addFieldError(
"userName", "Invalid username!");
if (!(getPassword().equals("Kites")))
addFieldError(
"password", "Invalid password!");
return ERROR;
}
--------------------------------------------------------------------------------------------------
No comments:
Post a Comment