【问题标题】:ActionContext.getContext().getSession() and NullPointerExceptionActionContext.getContext().getSession() 和 NullPointerException
【发布时间】:2014-07-15 07:41:54
【问题描述】:

我正在使用 Struts2、Spring、Spring security 和 Hibernate 开发一个应用程序。

我需要使用会话,我尝试用这段代码定义我的会话:

Map session =  ActionContext.getContext().getSession();//line 1
session.put("objet",objet);// line2

但它返回一个java.lang.NullPointerException (line1) 你对这个问题有什么想法吗?

LoginAction.java

public class LoginAction  extends ActionSupport implements SessionAware, Preparable, AuthenticationSuccessHandler {
private UserDetails userDetails;
private User userConnecte;
private Map session;
@Autowired
private UserBO userBO;
// DI via spring
public void setUserBO(UserBO userBO) {
    this.userBO = userBO;
}
/**
 * Getters and setters
 * @return
 */
public User getUserConnecte() {
    return userConnecte;
}

public void setUserConnecte(User userConnecte) {
    this.userConnecte = userConnecte;
}
public User getSession() {
    return session;
}

public void setSession(Map session) {
    this.session = session;
}

public void prepare() throws Exception {
    // TODO Auto-generated method stub

}
public void onAuthenticationSuccess(HttpServletRequest request,
        HttpServletResponse response, Authentication authentication)
throws IOException, ServletException {

    
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        if (!(auth instanceof AnonymousAuthenticationToken)) {
            this.userDetails =  (UserDetails) auth.getPrincipal();
        }
        this.userConnecte = this.userBO.retournerUserByEmail(this.userDetails.getUsername());
        this.session = ActionContext.getContext().getSession();
        session.put("userConnecte", this.userConnecte);
        

}

}

【问题讨论】:

  • 你在哪里使用这个代码?
  • 在我的 LoginAction 类中
  • 能否贴出与您的问题相关的源代码?
  • 完成!这是我的 LoginAction 类
  • 不要将 S2 操作用作AuthenticationSuccessHandler

标签: java spring session struts2 httpsession


【解决方案1】:

当一个动作由 struts2 过滤器启动时,您可以使用动作上下文。方法
onAuthenticationSuccess 与 struts2 和操作上下文无关。使用 servlet 请求对象获取会话。

HttpSession session = request.getSession(false);

【讨论】:

    【解决方案2】:

    试试这个,初始化你的 Servlet Action Context,

    public void onAuthenticationSuccess(HttpServletRequest request,HttpServletResponse response, Authentication authentication)
        throws IOException, ServletException {
    
               request=(HttpServletRequest)ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
               String userObject=request.getParameter("userConnecte");
                Authentication auth = SecurityContextHolder.getContext().getAuthentication();
               if (!(auth instanceof AnonymousAuthenticationToken)) {
                    this.userDetails =  (UserDetails) auth.getPrincipal();
               }
               this.userConnecte = this.userBO.retournerUserByEmail(this.userDetails.getUsername());
               this.session= ActionContext.getContext().getSession();
               if(this.userObject!=null){
               session.put("userConnecte", userObject);
               return "login_successful";
               }else{
               logger.info(" userObject is null."+userObject);
               retrun "re_login_page";
                }
     }
    

    【讨论】:

    • 已经有一个request 作为参数传递给这个方法。无需从动作上下文中检索。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-06
    • 1970-01-01
    • 2015-08-04
    • 1970-01-01
    • 1970-01-01
    • 2018-01-23
    • 2021-11-12
    相关资源
    最近更新 更多