Java Struts2侦听器会话在验证登录页时获取空值

Java Struts2侦听器会话在验证登录页时获取空值,java,session,struts2,struts2-interceptors,Java,Session,Struts2,Struts2 Interceptors,我是Struts2的初学者,我正试图在我自己的web项目上实现这个场景: 当用户访问登录页面时,服务器将通过使用拦截器访问会话来验证他/她是否以“管理员”/“用户”身份登录,如果用户在会话中没有权限数据(为空),则将其传递到登录页面 如果用户以“管理员”身份登录,用户将被重定向到“管理员”页面 如果用户以“用户”身份登录,用户将被重定向到“用户”页面 我在尝试这些代码,如果我不使用拦截器,我可以访问会话,但如果我使用拦截器,我得到的是,会话仍然为空,并给出错误500,而不是NPE。我不知道它

我是Struts2的初学者,我正试图在我自己的web项目上实现这个场景:

  • 当用户访问登录页面时,服务器将通过使用拦截器访问会话来验证他/她是否以“管理员”/“用户”身份登录,如果用户在会话中没有权限数据(为空),则将其传递到登录页面
  • 如果用户以“管理员”身份登录,用户将被重定向到“管理员”页面
  • 如果用户以“用户”身份登录,用户将被重定向到“用户”页面
我在尝试这些代码,如果我不使用拦截器,我可以访问会话,但如果我使用拦截器,我得到的是,会话仍然为空,并给出错误500,而不是NPE。我不知道它出了什么问题

谢谢你们中任何帮助我的人

struts.xml


struts pageauth.xml


strusts user.xml


/main/admin/admin.jsp
/main/user/user.jsp
/Login.jsp
/Login.jsp
/Login.jsp
UserAuthenticationLogin.java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package control.intercept;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.interceptor.Interceptor;
import java.util.Map;
import org.apache.struts2.interceptor.SessionAware;

public class UserAuthenticationLogin extends ActionSupport implements SessionAware, Interceptor {

    public void setSession(Map<String, Object> map) {
        this.sessionMap = map;
    }

    public void destroy() {
        System.out.println("UserAuthentication Interceptor destroy() called");
    }

    public void init() {
        System.out.println("UserAuthentication Interceptor init() called");
    }

    public String intercept(ActionInvocation ai) throws Exception {
        System.out.println("=========================DEBUG========================");
        System.out.println("UserAuthentication Interceptor intercept() called");
        System.out.println(getText("auth.privilage")); // I can access this properties
        System.out.println(this.sessionMap); // It gets NULL ??
//        System.out.println(this.sessionMap.get(getText("auth.privilage")));
        if(this.sessionMap.get(getText("auth.privilage"))==null) {
            return ai.invoke();
        }
        else if(this.sessionMap.get(getText("auth.privilage")).equals("admin")) {
            return "admin";
        }
        else if(this.sessionMap.get(getText("auth.privilage")).equals("user")) {
            return "user";
        }
        else {
            return "login";
        }
    }

    private String id;
    private String password;
    private String admin;
    private Map<String, Object> sessionMap;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getPassword() {
        return password;
    }

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

    public String getAdmin() {
        return admin;
    }

    public void setAdmin(String admin) {
        this.admin = admin;
    }

    public Map<String, Object> getSessionMap() {
        return sessionMap;
    }

    public void setSessionMap(Map<String, Object> sessionMap) {
        this.sessionMap = sessionMap;
    }
}
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package control.action;

import com.opensymphony.xwork2.ActionSupport;
import java.util.Map;
import org.apache.struts2.interceptor.SessionAware;

public class Login extends ActionSupport implements SessionAware {

    public void setSession(Map<String, Object> map) {
        this.sessionMap = map;
    }

    public String login() throws Exception {
        if(id.equals("admin") && password.equals("admin")) {
            this.sessionMap.put("id", "admin");
            this.sessionMap.put("priv", "admin");
            return "admin";
        }
        if(id.equals("user") && password.equals("user")) {
            this.sessionMap.put("id", "user");
            this.sessionMap.put("priv", "user");
            return "user";
        }
        else {
            setErr_msg(super.getText("error.login"));
            return super.ERROR;
        }
    }

    public String logout() throws Exception {
        this.sessionMap.remove("id");
        this.sessionMap.remove("priv");
        return super.SUCCESS;
    }

    private String id;
    private String password;
    private String err_msg;
    private String admin;
    private Map<String, Object> sessionMap;

    public Map<String, Object> getSessionMap() {
        return sessionMap;
    }

    public void setSessionMap(Map<String, Object> sessionMap) {
        this.sessionMap = sessionMap;
    }

    public String getAdmin() {
        return admin;
    }

    public void setAdmin(String admin) {
        this.admin = admin;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getPassword() {
        return password;
    }

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

    public String getErr_msg() {
        return err_msg;
    }

    public void setErr_msg(String err_msg) {
        this.err_msg = err_msg;
    }

}
/*
*要更改此模板,请选择工具|模板
*然后在编辑器中打开模板。
*/
包控制.intercept;
导入com.opensymphony.xwork2.ActionInvocation;
导入com.opensymphony.xwork2.ActionSupport;
导入com.opensymphony.xwork2.interceptor.interceptor;
导入java.util.Map;
导入org.apache.struts2.interceptor.SessionAware;
公共类UserAuthenticationLogin扩展ActionSupport实现SessionAware、拦截器{
公共无效设置会话(映射){
this.sessionMap=map;
}
公共空间销毁(){
System.out.println(“调用了UserAuthentication Interceptor destroy());
}
公共void init(){
System.out.println(“调用了UserAuthenticationInterceptor init());
}
公共字符串截获(ActionInvocation ai)引发异常{
System.out.println(“====================================================================================================”);
System.out.println(“调用了UserAuthentication Interceptor intercept());
System.out.println(getText(“auth.privilage”);//我可以访问此属性
System.out.println(this.sessionMap);//它得到NULL??
//System.out.println(this.sessionMap.get(getText(“auth.privilage”));
if(this.sessionMap.get(getText(“auth.privilage”))==null){
返回ai.invoke();
}
else if(this.sessionMap.get(getText(“auth.privilage”)).equals(“admin”)){
返回“admin”;
}
else if(this.sessionMap.get(getText(“auth.privilage”)).equals(“user”)){
返回“用户”;
}
否则{
返回“登录”;
}
}
私有字符串id;
私有字符串密码;
私有字符串管理;
私有地图会话地图;
公共字符串getId(){
返回id;
}
公共无效集合id(字符串id){
this.id=id;
}
公共字符串getPassword(){
返回密码;
}
public void setPassword(字符串密码){
this.password=密码;
}
公共字符串getAdmin(){
返回管理员;
}
公共void setAdmin(字符串管理){
this.admin=admin;
}
公共映射getSessionMap(){
返回sessionMap;
}
public void setSessionMap(映射sessionMap){
this.sessionMap=sessionMap;
}
}
Login.java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package control.intercept;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.interceptor.Interceptor;
import java.util.Map;
import org.apache.struts2.interceptor.SessionAware;

public class UserAuthenticationLogin extends ActionSupport implements SessionAware, Interceptor {

    public void setSession(Map<String, Object> map) {
        this.sessionMap = map;
    }

    public void destroy() {
        System.out.println("UserAuthentication Interceptor destroy() called");
    }

    public void init() {
        System.out.println("UserAuthentication Interceptor init() called");
    }

    public String intercept(ActionInvocation ai) throws Exception {
        System.out.println("=========================DEBUG========================");
        System.out.println("UserAuthentication Interceptor intercept() called");
        System.out.println(getText("auth.privilage")); // I can access this properties
        System.out.println(this.sessionMap); // It gets NULL ??
//        System.out.println(this.sessionMap.get(getText("auth.privilage")));
        if(this.sessionMap.get(getText("auth.privilage"))==null) {
            return ai.invoke();
        }
        else if(this.sessionMap.get(getText("auth.privilage")).equals("admin")) {
            return "admin";
        }
        else if(this.sessionMap.get(getText("auth.privilage")).equals("user")) {
            return "user";
        }
        else {
            return "login";
        }
    }

    private String id;
    private String password;
    private String admin;
    private Map<String, Object> sessionMap;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getPassword() {
        return password;
    }

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

    public String getAdmin() {
        return admin;
    }

    public void setAdmin(String admin) {
        this.admin = admin;
    }

    public Map<String, Object> getSessionMap() {
        return sessionMap;
    }

    public void setSessionMap(Map<String, Object> sessionMap) {
        this.sessionMap = sessionMap;
    }
}
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package control.action;

import com.opensymphony.xwork2.ActionSupport;
import java.util.Map;
import org.apache.struts2.interceptor.SessionAware;

public class Login extends ActionSupport implements SessionAware {

    public void setSession(Map<String, Object> map) {
        this.sessionMap = map;
    }

    public String login() throws Exception {
        if(id.equals("admin") && password.equals("admin")) {
            this.sessionMap.put("id", "admin");
            this.sessionMap.put("priv", "admin");
            return "admin";
        }
        if(id.equals("user") && password.equals("user")) {
            this.sessionMap.put("id", "user");
            this.sessionMap.put("priv", "user");
            return "user";
        }
        else {
            setErr_msg(super.getText("error.login"));
            return super.ERROR;
        }
    }

    public String logout() throws Exception {
        this.sessionMap.remove("id");
        this.sessionMap.remove("priv");
        return super.SUCCESS;
    }

    private String id;
    private String password;
    private String err_msg;
    private String admin;
    private Map<String, Object> sessionMap;

    public Map<String, Object> getSessionMap() {
        return sessionMap;
    }

    public void setSessionMap(Map<String, Object> sessionMap) {
        this.sessionMap = sessionMap;
    }

    public String getAdmin() {
        return admin;
    }

    public void setAdmin(String admin) {
        this.admin = admin;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getPassword() {
        return password;
    }

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

    public String getErr_msg() {
        return err_msg;
    }

    public void setErr_msg(String err_msg) {
        this.err_msg = err_msg;
    }

}
/*
*要更改此模板,请选择工具|模板
*然后在编辑器中打开模板。
*/
包装控制.行动;
导入com.opensymphony.xwork2.ActionSupport;
导入java.util.Map;
导入org.apache.struts2.interceptor.SessionAware;
公共类登录扩展ActionSupport实现SessionAware{
公共无效设置会话(映射){
this.sessionMap=map;
}
公共字符串login()引发异常{
if(id.equals(“admin”)和密码&password.equals(“admin”)){
this.sessionMap.put(“id”,“admin”);
this.sessionMap.put(“priv”、“admin”);
返回“admin”;
}
if(id.equals(“用户”)&&password.equals(“用户”)){
this.sessionMap.put(“id”,“user”);
this.sessionMap.put(“priv”、“user”);
返回“用户”;
}
否则{
setErr_msg(super.getText(“error.login”);
返回super.ERROR;
}
}
公共字符串logout()引发异常{
this.sessionMap.remove(“id”);
此.sessionMap.remove(“priv”);
回归超级成功;
}
私有字符串id;
私有字符串密码;
私有字符串err_msg;
私有字符串管理;
私有地图会话地图;
公共映射getSessionMap(){
返回sessionMap;
}
public void setSessionMap(映射sessionMap){
this.sessionMap=sessionMap;
}
公共字符串getAdmin(){
返回管理员;
}
公共void setAdmin(字符串管理){
this.admin=admin;
}
公共字符串getId(){
返回id;
}
公共无效集合id(字符串id){
this.id=id;
}
公共字符串getPassword(){
返回密码;
}
public void setPassword(字符串密码){
this.password=密码;
}
公共字符串getErr_msg(){
返回err_msg;
}
公共无效设置错误消息(字符串错误消息){
this.err\u msg=err\u msg;
}
}

sessionMap由实现SessionAware接口的操作注入,而不是由拦截器注入

在拦截器中获取会话映射的正确方法是:

Map<String, Object> session = ActionContext.getContext().getSession();
Map session=ActionContext.getContext().getSession();
注意:小心
Map<String, Object> session = ActionContext.getContext().getSession();