2010-10-20 93 views
2

没有设置我有一个春天控制器,它是把一个变量在会话:Session变量春

public class LoginFormController extends SimpleFormController { 

    public ModelAndView onSubmit(HttpServletRequest request, 
      HttpServletResponse response, Object command) throws Exception { 

      request.getSession().setAttribute("authenticated_user", "authenticated_user"); 
    } 
} 

然后我有一个HandlerInterceptor接口。在preHandle方法,我检查变量会话:

public boolean preHandle(HttpServletRequest request, 
             HttpServletResponse response, 
             Object handler) throws Exception { 

     /* first, check if the requested view even required authentication */ 
     if (isAuthenticationRequired(request)) { 

      /* check to see that user is logged in */ 
      if (null == request.getSession().getAttribute("authenticated_user")) { 
       forwardToLogonPage(request, response); 
       return false; 
      } 
      /* all is ok - pass the request on */ 
      return true; 
     } 
     /* all is ok - pass the request on */ 
     return true; 
    } 

问题是,它似乎是会话变量没有被自request.getSession()设置的getAttribute(“AUTHENTICATED_USER”))始终。解决为空。

任何想法plz?

感谢, Krt_Malta

+0

BDW Request对象...的isAuthenticationRequired方法效果很好,我测试了它。 – 2010-10-20 06:41:26

+0

问题实际上是onSubmit()方法没有被调用... – 2010-10-20 07:15:23

回答

0

尝试throw new ModelAndViewDefiningException(new ModelAndView("logonPage")),而不是return false

而在你logonPage,明确包括在表单标签的动作,这样的事情:

<form:form method="post" commandName="login" action="logonPage.htm"> 
0

您也可以注入ServletRequestAttributes。 通过包括:

import org.springframework.web.context.request.ServletRequestAttributes; 
import org.springframework.web.context.request.RequestContextHolder; 

我在一个抽象的超控制器这样做,所以我的所有控制器都将servlet请求知道。

要提取您使用可以使用以下

protected ServletRequestAttributes getServletRequest(){ 
    return (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes(); 
    }