Saturday, September 13, 2008

Accessing Scope Variables in struts2 from Action class

Here in this post, I am going to show how to access variables in different scope , in struts2. In struts 2 we can set objects in the following scopes, say request, session, page and application.
To share the object between Java and Jsp expressions, we need to set the java object in any of the above said scopes and can access it using the struts tag in Jsp page.

For Example....
If I have a variable named foodCollection which contains a collection of different food items, I can set this in different scopes and can access it.
1. Session
In Java Action class
ActionContext.getContext().getSession().setAttribute("foodCollection",foodCollection);
In Struts2 Jsp page
#session.foodCollection
2.Request
request.setAttribute("foodCollection",foodCollection);
In Struts2Jsp
#request.foodCollection
3.Page
pageContext.setAttribute("foodCollection",foodCollection);
In Struts2Jsp
#attr.foodCollection

Here is a simple example of using struts2.
First add the following entry in your web.xml, so that all your requests pass through the filterdispatcher filter instead Action servlet in struts1.2.
<filter
filter-namestruts2filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcherfilter-class>
filter>

<filter-mapping>
<filter-name>struts2filter-name>
<url-pattern>*.dourl-pattern>
filter-mapping>

The default URL pattern is .action which can be changed by using the struts.properties file.

Next write your struts.xml file and place it in the package where you want.
Struts.xml contains the mapping between the request and the corresponding action class.
<package name="struts2" extends="struts-default">
<action name="showLogin">
<result>/jsp/master/login.jsp</result>
action>

action name="doLogin" class="action.Login">
<result name="input">/jsp/master/login.jspresult>
<result name="error">/jsp/master/login.jspresult>
<result>/jsp/master/loginsuccess.jspresult>
action>

package>

Write a java class which extends ActionSupport class of struts2, you can either have a default execute method or any method name according to the requirements.

In the action class you cant access the request and session directly, you need to either use ActionContext class like this,

ActionContext.getContext().getSession().put("TestLoop1",testingVos);

or need to implement the ServletRequestAware and ,ServletResponseAware and override its setter method, also you define your getter method which returns the request and response and you can access like the one shown below

getServletRequest().getSession().setAttribute("testSessionVar", "From Action class set in session");
getServletRequest().setAttribute("TestVariable", "From Action class set in request");

Suppose if you want to access a collection in the action class in JSP page you dont have to set the collection in session or request, you can access it in the JSP page by just having the name of the collection and a getter method for that collection in the StrutsAction class like this
I have a collection named testingVos which is a collection of testing vo
In Action class, the code snippet will be like the one....
public ArrayList getTestingVos(){
return testingVos;
}


In JSP page the code snippet will be

<s:iterator value="testingVos">
<s:property value="code"/><s:property value="type"/>
s:iterator>

No comments: