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

来源:2-7 完成显示登录成功用户名和注销功能

慕斯卡5552298

2016-10-11 15:12

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回答

  • 慕函数0662299
    2016-10-12 18:44:46
    已采纳

    你的 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();

    }


    }


  • 骑士灬归来
    2017-05-25 10:11:13

    真给力,非常感谢!

使用Struts2+Hibernate开发学生信息管理功能

Strust2+Hibernate整合开发案例,Java Web开发技能更上一层楼

80701 学习 · 754 问题

查看课程

相似问题