为了账号安全,请及时绑定邮箱和手机立即绑定

为什么我的不能用session存,提示request为空,换成request存立刻就行了

public String login(){

UsersDAO udo = new UsersDAOImpl();

if (udo.usersLogin(user)) 

{

//在session中保存登录成功的用户名

// session.setAttribute("loginUserName", user.getUsername());

request.setAttribute("loginUserName", user.getUsername());

return "login_success";

} else {

return "login_failure";

}

}


正在回答

2 回答

你的 SuperAction 类是否没有初始化 session 啊,看下面,在 setServletRequest() 有给session 辅助哦:

package action;


import javax.servlet.ServletContext;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession;


import org.apache.struts2.interceptor.ServletRequestAware;

import org.apache.struts2.interceptor.ServletResponseAware;

import org.apache.struts2.util.ServletContextAware;


import com.opensymphony.xwork2.ActionSupport;


//所有Action动作的父类

public class SuperAction extends ActionSupport implements ServletRequestAware,ServletResponseAware,ServletContextAware {


/**

*/

private static final long serialVersionUID = 7727505312022359301L;

protected HttpServletRequest request;  //请求对象

protected HttpServletResponse response;  //响应对象

protected HttpSession session;  //会话对象

protected ServletContext application;  // 全局对象

@Override

public void setServletContext(ServletContext application) {

this.application = application;

}


@Override

public void setServletResponse(HttpServletResponse response) {

this.response = response;

}


@Override

public void setServletRequest(HttpServletRequest request) {

this.request = request;

this.session = this.request.getSession();

}


}


1 回复 有任何疑惑可以回复我~

真给力,非常感谢!

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

为什么我的不能用session存,提示request为空,换成request存立刻就行了

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信