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.
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.
In Java Action class
In Struts2 Jsp page
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.
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="doLogin" class="action.Login">
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,
or need to implement the ServletRequestAware and ,
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....
In JSP page the code snippet will be