In this tutorial, you will see the use of struts2.2.1 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. It is useful when you wish to assign a variable to a complex expression and then simply reference that variable each time rather than the complex expression.
Directory structure of set tag example.![]() |
1- index.jsp
|
<%@taglib uri="/struts-tags" prefix="s" %> <html> <head> <title>Struts2.2.1_SetTag_Example</title> </head> <body> <h2>Struts2.2.1_SetTag_Example</h2> <hr> <s:a href="setTagAction.action">setTagAction</s:a> </body> </html> |
2-SetTagAction .java
|
package roseindia.action; import com.opensymphony.xwork2.ActionSupport; public class SetTagAction extends ActionSupport{ public String execute() throws Exception { return super.execute();} } |
3-StudentInfoBean .java
|
package roseindia.action; public class StudentInfoBean { private String name="bharat"; private String age="24"; public String getName() { return name;} public void setName(String name) { this.name = name;} public String getAge() { return age;} public void setAge(String age) { this.age = age;} } |
4_struts.xml
|
<struts> <package name="roseindia" extends="struts-default" namespace="/"><action name="setTagAction" class="roseindia.action.SetTagAction"> <result name="success">setTagView.jsp</result> </action></package> </struts> |
5_setTagView.jsp
|
<%@taglib uri="/struts-tags" prefix="s" %> <html> <head><title>Struts2.2.1_SetTag_Example</title></head> <body> <h2>Struts2.2.1_SetTag_Example</h2> <hr><s:bean name="roseindia.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> </html> |
index.gif

pushResult.gif
