为什么SessionMap不会被实例化?(Why SessionMap is not instant

2019-10-20 04:42发布

第一次会员注册,会话需要进行设置,通常发生在登录,所以我想我会重用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");
}

Answer 1:

你需要有servletConfig的动作配置中引用拦截器。

其设定基于一个动作实现的接口操作属性的拦截。 例如,如果action实现ParameterAware那么行动方面的参数,将地图设置就可以了。

这个拦截器是专门为设置一个动作需要的所有属性,如果它是知道的servlet的参数,servlet上下文,会话等接口,它支持包括:

 ServletContextAware ServletRequestAware ServletResponseAware ParameterAware RequestAware SessionAware ApplicationAware PrincipalAware 

这个拦截器的能力注入的servlet的东西对象的动作豆。

请注意,这个拦截包括在defaultStack ,这是默认使用的,如果你不引用任何拦截。 如果替换的操作配置的拦截defaultStack消失。



Answer 2:

RegisterAction

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

在RegisterAction你通过会员的物品(如为会员<用户名,密码>)

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


}


文章来源: Why SessionMap is not instantiated?