get session into action class in struts 2.x

Nov 25th, 2011No Comments

JSPs contain implicit session object to use in the jsp pages, when an action in invoked in struts we need to get ‘session ‘ into the action class, we can do this by implementing “SessionAware” interface. Eclipse  will ask you to implement an unimplemented  method click on that meesage and eclipse will create the method for you called “public void setSession(Map<String, Object> arg0){//TODO}”

  1. declare a private variable as “Map<String, Object> session” in the action class.
  2. In the setSession method created as mentioned above write “this.session=arg0;” in place of “//TODO”.

Struts will bring in the session into your session object. you can use the session object anywhere in the code.

example code:

public class ExampleClass extends ActionSupport implements SessionAware{

private Map<String, Object> session;

@Override

public void setSession(Map<String, Object> arg0) {

this.session=arg0;

}

VN:F [1.9.10_1130]
Rating: 8.3/10 (4 votes cast)
VN:F [1.9.10_1130]
Rating: 0 (from 0 votes)

get session into action class in struts 2.x, 8.3 out of 10 based on 4 ratings

About author:

All entries by

Leave a Reply