ActionContext.getContext().getSession().get()空指针问题

cszhmrz 2014-01-13 09:47:55
在一个ssh项目中,我在一个Action里将一个对象使用ActionContext.getContext().getSession().put()方法存起来,然后在另一个action里使用ActionContext.getContext().getSession().get()方法取出,却报出空指针异常?请各位大神帮忙解决,谢谢
代码如下:
public String checkLogin() throws Exception {
Employee emp = null;
try {
String sql = "from Employee where userId='" + userId
+ "'and loginPwd='" + loginPwd + "'";
emp = empService.getpagedata(Employee.class, sql, -1, -1).get(0);

ActionContext.getContext().getSession().put("currentEmp", emp);
/*在这个方法里能够获得该对象*/
Employee emp1 = (Employee) ActionContext.getContext().getSession()
.get("currentEmp");
System.out.println(emp1.getName());
return "success";
} catch (Exception ex) {
HttpServletResponse response = ServletActionContext.getResponse();
System.out.println(ex);
PrintWriter writer = response.getWriter();
writer.print("false");
return null;
}
}

另一个Action类:
public String addSales() {
String sql = "from Employee where roleId = 2";
List<Employee> empList = salesService.getpagedata(Employee.class, sql,
-1, -1);
Employee emp = (Employee) ActionContext.getContext().getSession().get("currentEmp");//emp为空???
if(emp!=null)
System.out.println(emp.getName());
else
System.out.println("fuck");
ActionContext.getContext().put("user", emp.getName());
ActionContext.getContext().put("empList", empList);
return "success";
}
...全文
192 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
逍遥兮容与 2014-01-13
  • 打赏
  • 举报
回复
理论上应该没问题,看看session失效时间,调试下吧。 还有估计你也是初学,代码最好要做分层,提高复用。---我也工作没多久,一点建议

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧