Action中读写Session
这在struts2的官网上有说明,让Action实现SessionAware 接口即可,然后直接操作成员变量userSession对象即可。
例如,用Map的put函数放入一个属性,则会影响当前HttpSession。
http://struts.apache.org/2.x/docs/http-session.html
import java.util.Map;
import org.apache.struts2.interceptor.SessionAware;
import com.opensymphony.xwork2.ActionSupport;
public class HelloWorld extends ActionSupport implements SessionAware
{
private Map<String, Object> userSession ;
public void setSession(Map<String, Object> session)
{
userSession = session ;
}
}
public String execute()
{
// TODO Auto-generated method stub
System.out.println("username = " + this.username);
userSession.put("username", username);
whatName1 = "thankasfsdfs";
this.hei = "lsdflafdk";
return SUCCESS;
}
【注】struts2自身会把该Map的值放到HttpSession的键值对里,机制未知,有可能该Map就是直接指向了HttpSession的Map