Struts2 框架下 session 读出来为null
2016-06-11 11:24
302 查看
我用的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); 有没有人解释下 为什么
相关文章推荐
- mysql/sql server和java之间的数据类型对应关系
- JavaWeb学习笔记之Log4j使用教程
- Struts.xml中Action的method与路径的三种匹配方法
- eclipse连接MySQL数据库
- Java中的动态代理
- 孩子们的游戏(圆圈中最后剩下的数)--约瑟夫环问题
- java学习笔记之集合框架
- Java 文档注释
- java学习笔记之内存模型
- RxJava操作符 -创建型
- 和为S的连续正数序列
- IDEA 和 Eclipse 使用对比
- java RandomAccessFile
- java8的lambda表达式
- java学习笔记之jdbc连接数据库
- 【ZZ】Java : 一个帝国的诞生 & 假如时光能够倒流, 我会这么学习Java
- Retrofit与RXJava整合
- java中将数组转换为集合,不能进行增删操作解惑
- windows环境下强制JVM和Sass使用utf-8编译的方法
- java学习笔记之class类实例获取方法