问题描述
成员第一次注册时,需要设置会话,这通常发生在登录时,所以我想我会重用 LoginAction
而不路由到它.但是 sessionmap
没有被实例化.
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
注册操作
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;
}
}
登录操作
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");
}
推荐答案
你需要有 servletConfig
拦截器在动作配置中引用.
You need to have servletConfig
interceptor referenced in the action configuration.
基于接口设置动作属性的拦截器一个动作执行.例如,如果动作实现ParameterAware
然后将设置动作上下文的参数映射
这个拦截器旨在设置一个动作需要的所有属性,如果它知道 servlet 参数、servlet 上下文、会话、等等,它支持的接口有:
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
此拦截器将 servlet 填充对象注入操作 bean 的能力.
The capability of this interceptor to inject the servlet stuff objects to the action bean.
请注意,此拦截器包含在 defaultStack
中,如果您不引用任何拦截器,则默认使用该拦截器.如果您在操作配置中覆盖拦截器,defaultStack
就会消失.
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.
这篇关于为什么 SessionMap 没有实例化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!