Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Session in Struts 2

Is this the correct way of storing a value in session in Struts2?

Map<String, Object> session = ActionContext.getContext().getSession();
session.put("user", "USERNAME");
like image 419
JR Galia Avatar asked Nov 14 '22 03:11

JR Galia


1 Answers

SessionAware interface in struts 2.x, our Action class needs to implement SessionAware interface in order to get HTTP Session behavior into our Action class.

If we implement from SessionAware interface we need to override the method setSession() by SessionAware in our action class. If we implement our action class from SessionAware interface then struts 2 controller doesn’t inject exactly session object, but it will injects a Map object with similar behavior.

 Map m;
 public void setSession(Map m)
    {
        this.m=m;
    }

 public String execute()
    {
        m.put("user", "USERNAME");


        return SUCCESS;
    }
like image 174
user2077648 Avatar answered Nov 16 '22 04:11

user2077648