问题
The first time a member registers, the session needs to be set, which normally happens in login, so I thought I'd reuse the LoginAction
without routing to it.
But the sessionmap
isn't getting instantiated.
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");
}
回答1:
You need to have servletConfig interceptor referenced in the action configuration.
An interceptor which sets action properties based on the interfaces an action implements. For example, if the action implements
ParameterAware
then the action context's parameter map will be set on it.This interceptor is designed to set all properties an action needs if it's aware of servlet parameters, the servlet context, the session, etc. Interfaces that it supports are:
ServletContextAware ServletRequestAware ServletResponseAware ParameterAware RequestAware SessionAware ApplicationAware PrincipalAware
The capability of this interceptor to inject the servlet stuff objects to the action bean.
Note, this interceptor is included in the defaultStack
, which is used by default if you don't reference any of interceptors. If you override the interceptors in the action config the defaultStack
disappears.
回答2:
RegisterAction
Member newMember = new Member(username, password);//newMember having username,password object
Member member = new Member();
LoginAction firstLogin = new LoginAction(member);
firstLogin.execute();
in RegisterAction you passed Member Object(like as Member < username , password > )
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());
}
来源:https://stackoverflow.com/questions/23840483/why-sessionmap-is-not-instantiated