Struts2 框架下 session 读出来为null

我用的strust2框架,开始的时候这么写的:

在 登陆函数中(注释部分):

public String dealerLogin(){
        EntityInfo entityinfo=dealerservice.dealerLogin(super.account,super.password);
           if(entityinfo!=null){
/*             ActionContext ac = ActionContext.getContext();我开始用的这种方式写入到session中
               ac.getSession().put(KeyConstant.MANAGER,entityinfo); */
               ServletActionContext.getRequest().getSession().setAttribute(KeyConstant.MANAGER,entityinfo);
               return "dealertlogin";
           }
           else{
               return "false";
           }
    }

用的时候的函数:

public String addAllMachCode(){
        String[] machinearrs=machinearr.split(";");
        int dealerId=0;
/*        HttpServletRequest request=ServletActionContext.getRequest();
        HttpSession session=request.getSession();
        EntityInfo entityinfo=(EntityInfo) session.getAttribute(KeyConstant.MANAGER);*/这里读出来  entityinfo是null
        EntityInfo entityinfo= (EntityInfo) ActionContext.getContext().getSession().get(KeyConstant.MANAGER);
        Dealer dealer= entityinfo.getDealer();
        dealerId=dealer.getDEALER_ID();
        for (String machinecode :machinearrs) {
            
            addAMachCode(machinecode,dealerId);
        }
        return SUCCESS;
    }

这到底是为什么了:
提取出来 第一种: 写入 ActionContext ac = ActionContext.getContext();
               ac.getSession().put(KeyConstant.MANAGER,entityinfo)
          读取: HttpServletRequest request=ServletActionContext.getRequest();
               HttpSession session=request.getSession();
              EntityInfo entityinfo=(EntityInfo) session.getAttribute(KeyConstant.MANAGER);
      第二种:写入   ServletActionContext.getRequest().getSession().setAttribute(KeyConstant.MANAGER,entityinfo);
          读取: EntityInfo entityinfo= (EntityInfo) ActionContext.getContext().getSession().get(KeyConstant.MANAGER);
有没有人解释下 为什么  

 

转载于:https://www.cnblogs.com/softwarewebdesign/p/5574955.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值