Is this the correct way of storing a value in session in Struts2?
Map<String, Object> session = ActionContext.getContext().getSession();
session.put("user", "USERNAME");
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With