struts2使用session 的三种方式

来源:互联网 发布:域名国外备案 编辑:程序博客网 时间:2024/05/19 14:53

1、通过上下文,从ActionContext中获取

ActionContext actionContext = ActionContext.getContext();
Map session = actionContext.getSession();

2、实现SessionAware接口

public class SessionTest1Action extends ActionSupport implements SessionAware {

private Map session;

public void setSession(Map session) {

this.session = session;

}

3、通过httpservletrequest获取

public class BaseAction extends ActionSupport{
    public HttpSession session;
    public HttpServletRequest request;
    public HttpSession getSession() {
        if(request==null){
            request=ServletActionContext.getRequest();
        }
        session=request.getSession();
        return session;
    }
    public HttpServletRequest getRequest() {
        request=ServletActionContext.getRequest();
        return request;
    }

}


0 0
原创粉丝点击