struts2 session问题?
Java code
public class CreateUserAction extends ActionSupport {
private static final long serialVersionUID = 1L;
HttpServletRequest request ;
public HttpServletRequest getRequest() {
return request;
}
public void setRequest(HttpServletRequest request) {
this.request = request;
}
@Override
public String execute() throws Exception {
System.out.println("-----CreateUserAction-----");
SysUserPOJO sysUserPOJO = (SysUserPOJO) request.getSession()
.getAttribute("INITCONF");//这行报错
String dir = this.getClass().getProtectionDomain().getCodeSource()
.getLocation().getPath();
String currdir = StringUtils.substring(dir, 0, StringUtils.indexOf(dir,
"WEB-INF"))
+ "page" + File.separator + "strutsData";
new InitSysDAO().createUser(sysUserPOJO);
int result = new InitSysDAO().impDBDate(currdir, sysUserPOJO);
ServletContext app = request.getSession().getServletContext();
app.setAttribute("ISBASESYSINIT", null);
if (result == 1) {
return "success";
}
return "DBerror";
}
}
严重:
Servlet.service() for servlet default threw exceptionjava.lang.NullPointerException at com.asideal.action.sysInit.CreateUserAction.execute(CreateUserAction.java:31)
at
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)==================================
这是另一个类的 session.set...
private HttpSession session;
。。。。
}else{
System.out.println("创建表空间......");
initSysDAO.createDataSpace(getsysUser(request));
session.setAttribute("INITCONF",sysUserPOJO);
}
struts2里的session出错。。。。
------解决方案--------------------request.getSession().getAttribute("INITCONF");
这个得到的是个集合!
你用 SysUserPOJO 类接收错了吧!
先把集合遍历出来 在用 SysUserPOJO 接收!
------解决方案--------------------
SysUserPOJO sysUserPOJO = (SysUserPOJO) request.getSession()不对吧,session类型不对吧。
struts2的session对象
这样写试试:
HttpSession session=ServletActionContext.getRequest().getSession()
------解决方案--------------------HttpServletRequest request ;
public HttpServletRequest getRequest() {
return request;
}
public void setRequest(HttpServletRequest request) {
this.request = request;
}
这样注入HttpServletRequest request,request当然==null 所以报
空指针了!
4楼正解
------解决方案--------------------public HttpServletRequest getRequest()
{
return ServletActionContext.getRequest();
}
------解决方案--------------------关于struts2获取session跟往里面存值我的方法是:
往session里放值ActionContext.getContext().getSession().put("name",value);
往session里取值ActionContext.getContext().getSession().get("name");
------解决方案--------------------HttpServletRequest request ;
public HttpServletRequest getRequest() {
return request;
}
request没有赋值吧