Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need to access a variable in jsp from action class

 
anilkumar potu
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
anilkumar potu
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got my result. I used sessionaware and session get/set attribute.

 
Lorand Komaromi
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure Thanks
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic