Need to access a variable in jsp from action class
anilkumar potu
Greenhorn
Joined: Dec 11, 2008
Posts: 15
posted
0
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
posted
0
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
posted
0
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
posted
0
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
posted
0
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
posted
0
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
posted
0
I got my result. I used sessionaware and session get/set attribute.
Lorand Komaromi
Ranch Hand
Joined: Oct 08, 2009
Posts: 276
posted
0
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!
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://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.
subject: Need to access a variable in jsp from action class