Java 未在Spring中设置会话变量
我有一个Spring控制器,它在会话中放入一个变量:Java 未在Spring中设置会话变量,java,spring-mvc,session-variables,Java,Spring Mvc,Session Variables,我有一个Spring控制器,它在会话中放入一个变量: public class LoginFormController extends SimpleFormController { public ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command) throws Exception { reques
public class LoginFormController extends SimpleFormController {
public ModelAndView onSubmit(HttpServletRequest request,
HttpServletResponse response, Object command) throws Exception {
request.getSession().setAttribute("authenticated_user", "authenticated_user");
}
}
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;
}
然后我有一个手柄拦截器。在预处理方法中,我检查会话中的变量:
public class LoginFormController extends SimpleFormController {
public ModelAndView onSubmit(HttpServletRequest request,
HttpServletResponse response, Object command) throws Exception {
request.getSession().setAttribute("authenticated_user", "authenticated_user");
}
}
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.getAttributeauthenticated_用户总是解析为null
有什么想法吗
谢谢,
Krt_Malta尝试抛出新模型和视图定义例外新模型和视图登录页面,而不是返回false
在logonPage中,在form标记中显式包含操作,如下所示:
<form:form method="post" commandName="login" action="logonPage.htm">
尝试抛出新模型和视图定义例外新建模型和视图登录页面,而不是返回false
在logonPage中,在form标记中显式包含操作,如下所示:
<form:form method="post" commandName="login" action="logonPage.htm">
您还可以注入ServletRequestAttributes。 包括:
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
我在一个抽象的超级控制器中实现了这一点,这样我的所有控制器都可以感知servlet请求
要提取您使用的请求对象,可以使用以下命令
protected ServletRequestAttributes getServletRequest(){
return (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
}
您还可以注入ServletRequestAttributes。 包括:
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
我在一个抽象的超级控制器中实现了这一点,这样我的所有控制器都可以感知servlet请求
要提取您使用的请求对象,可以使用以下命令
protected ServletRequestAttributes getServletRequest(){
return (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
}
Bdw…isAuthenticationRequired方法运行良好,我对其进行了测试。问题实际上是onSubmit方法没有被调用…Bdw…isAuthenticationRequired方法运行良好,我对其进行了测试。问题实际上是onSubmit方法没有被调用。。。