ActionContext.getContext().getSession().get()空指针问题
在一个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";
}