struts2--session

session和request的用法,仅作参考:
定义一个BasicAction继承ActionSupport实现SessionAware,ServletRequestAware这两个接口,然后再定义两个成员变量protected Map<String, Object> session;protected HttpServletRequest request;最后定义两个方法:



public void setSession(Map<String, Object> session) {
    this.session = session;
}
 
public void setServletRequest(HttpServletRequest request) {
    this.request = request;
}

其他action可以继承BasicAction,需要用session或request时直接request.setAttribute("cid", cid);
页面获取属性都是一样的方式



一个项目中用到的实例
public class BaseAction extends ActionSupport implements SessionAware{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 7284375843476500697L;
	
	private Map session;
	
	public void setSession(Map arg0) {
		this.session = arg0;
	}
	
	public void addInfoToSession(String sessKey, Object value)
	{
		this.session.put(sessKey, value);
	}
	//sessKey  是单点登录中用的
	public Object getSessionInfoByKey(String sessKey)
	{
		return this.session.get(sessKey);
	}
}
 

你可能感兴趣的:(struts2)