*
The moose likes Struts and the fly likes Need to access a variable in jsp from action class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Need to access a variable in jsp from action class" Watch "Need to access a variable in jsp from action class" New topic
Author

Need to access a variable in jsp from action class

anilkumar potu
Greenhorn

Joined: Dec 11, 2008
Posts: 15
I have my result in action class and want to use those variables in jsp. I am using Struts2 which is different from struts 1.x in session variable's. Can some one let me know easy way to handle this?

Thanks
Anil
Lorand Komaromi
Ranch Hand

Joined: Oct 08, 2009
Posts: 276
anilkumar potu wrote:I have my result in action class and want to use those variables in jsp.


Just add a getter in your action class that returns the value you'd like to show on the page and use the property tag to output its value. If it's a Collection or array, you can loop through its elements with iterator.


OCJP 6 (93%)
anilkumar potu
Greenhorn

Joined: Dec 11, 2008
Posts: 15
Hi,

Thanks for your reply. If i am not wrong Property will be used as a static display. But i want my result in some variable and i need to use it in my jsp's.

Any suggestions?

anil
Lorand Komaromi
Ranch Hand

Joined: Oct 08, 2009
Posts: 276
anilkumar potu wrote: But i want my result in some variable and i need to use it in my jsp's.


You shouldn't have Java code in your JSP, it's against the principles of the MVC pattern, if you do that, you might as well throw out Struts and put all your code in JSPs!
anilkumar potu
Greenhorn

Joined: Dec 11, 2008
Posts: 15
Its not that i have my java code in jsp. I need to use the result variables in my jsp for further processing and displaying results to user accordingly.
Lorand Komaromi
Ranch Hand

Joined: Oct 08, 2009
Posts: 276
anilkumar potu wrote:Its not that i have my java code in jsp. I need to use the result variables in my jsp for further processing and displaying results to user accordingly.


What kind of "further processing" are you doing then? Can't it be done with struts tags..?

You could add your variables to the http servlet request, for instance, like this.
anilkumar potu
Greenhorn

Joined: Dec 11, 2008
Posts: 15
I got my result. I used sessionaware and session get/set attribute.

Lorand Komaromi
Ranch Hand

Joined: Oct 08, 2009
Posts: 276
anilkumar potu wrote:I got my result. I used sessionaware and session get/set attribute.



Make sure you remove them from session when they're no longer needed!
anilkumar potu
Greenhorn

Joined: Dec 11, 2008
Posts: 15
Sure Thanks
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

If you don't need them in the session, don't put them there in the first place: the normal way to expose data to a JSP in Struts 2 is to create an action property with a public getter and use the Struts 2 tags, or JSP EL, on the JSP side--which was the first answer you received.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need to access a variable in jsp from action class
 
Similar Threads
Creating Struts's Action
use of <bean:write> and <bean:message> ???
Session variable
How an action class read values sent from jsp
Data Transmission from a JSP Page to the Struts Action class