为什么SessionMap没有实例化?

时间:2014-05-24 01:39:50

标签: java session struts2 struts2-interceptors

会员第一次注册时,需要设置会话,这通常发生在登录中,所以我想我会重用LoginAction而不会路由到它。 但sessionmap并未实例化。

member logging in is: model.hibernate.Member@549c8a8c
session map not instantiated

RegisterAction

public class RegisterAction extends ActionSupport implements SessionAware{
    private String username, password, email;

    SessionMap<String,Object> sessionmap;  
     MemberDAO mdao = new MemberDAO();
     UsersDAO udao = new UsersDAO();
     Users user = new Users();
     Member member = new Member();



    public RegisterAction() {
        this.email = "";
        this.password = "";
        this.username = "";
    }

   // ...setters/getters...



    public String execute() {



            udao.addUserToDatabase(newUser);

            Member newMember = new Member(username, password);
            mdao.addMemberToDatabase(newMember);
            member = newMember;

            // perform first login (to set session member and role).
            // this could be done by sending to login action. 
            // but then would have to track that it was first login.  this is quick fix. 
            LoginAction firstLogin = new LoginAction(member);   
            firstLogin.setSession(sessionmap);
            String firstLoginAttempt = firstLogin.execute();
            String resultString = "";
            if(firstLoginAttempt.equals(SUCCESS)){resultString = SUCCESS;}
            else{            
                    addActionError("First login attempt didnt work");
                    resultString = ERROR;
            }


            return resultString; // send user to quiz or show error
         }else {
            // cant add user
             addActionError("Username already taken. Please choose another.");
             return ERROR;
         }
    }

    public void setSession(Map<String, Object> map) {
        sessionmap=(SessionMap) map;  
    }
}

的LoginAction

public class LoginAction extends ActionSupport implements SessionAware{
    private String username, password;

     MemberDAO mdao = new MemberDAO();
     Member member = new Member();
     SessionMap<String,Object> sessionmap;  


    public LoginAction() {
        this.password = "";
        this.username = "";
    }

    public LoginAction(Member m) {
        this.member = m;
        this.password = member.getPassword();
        this.username = member.getUsername();

    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getUsername() {
        return username;
    }

    public String getPassword() {
        return password;
    }

    public String execute() {

        // checks that credentials match db


        setLoggedInMember(member);
        setLoggedInRole(member);

    }


    public String logout(){  
            if(sessionmap!=null){  
                sessionmap.invalidate();  
            }  
        return "success";  
    }  

    public void setSession(Map<String, Object> map) {

        sessionmap=(SessionMap) map;  

    }
protected void setLoggedInMember(Member m){
    System.out.println("member logging in is: "+m.toString());
    try{
        if(sessionmap!=null){
    sessionmap.put("member",m.toString());
        }
        else{System.out.println("session map not instantiated");}
    }catch(Exception e){System.out.println(e);}

}

public Member getLoggedInMember(){
    return (Member) sessionmap.get("member");
}


protected void setLoggedInRole(Member member) {
    if(member.getAdmin() != null)
        sessionmap.put("role", "admin");
    else if(member.getAgent()!=null)
        sessionmap.put("role", "agent");
    else if(member.getUsers()!=null)
        sessionmap.put("role", "user");
    else
        addActionError("Unknown member role");
}

public String  getLoggedInRole(){
    return (String) sessionmap.get("role");
}

2 个答案:

答案 0 :(得分:0)

您需要在操作配置中引用servletConfig拦截器。

  

基于接口设置动作属性的拦截器   一个动作实现。例如,如果操作实现   ParameterAware然后将设置操作上下文的参数映射   它

     

此拦截器旨在设置动作所需的所有属性   它知道servlet参数,servlet上下文,会话,   它支持的接口是:

ServletContextAware

ServletRequestAware

ServletResponseAware

ParameterAware

RequestAware

SessionAware

ApplicationAware

PrincipalAware

此拦截器将servlet stuff对象注入操作bean的能力。

注意,此拦截器包含在defaultStack中,如果您不引用任何拦截器,则默认使用此拦截器。如果您在操作配置中覆盖拦截器,defaultStack将消失。

答案 1 :(得分:-1)

RegisterAction

Member newMember = new Member(username, password);//newMember having username,password object
Member member = new Member();
LoginAction firstLogin = new LoginAction(member);
firstLogin.execute();
在RegisterAction中

你传递了会员对象(如会员&lt;用户名,密码&gt;)

LoginAction.class

 public String execute() {
    //if you received member object here.
    //retrieve your username,password here like this
     //Your mentioned model.hibernate.Member@549c8a8c -->Object Reference value

     member.getUsername();
     member.getPassword();



    setLoggedInMember(member);
    setLoggedInRole(member);

}
protected void setLoggedInMember(Member m){
    System.out.println("member logging in is: "+m.getUsername());


}