2013-07-21 37 views
0

我在Freemarker的viewResolver中遇到了問題 - 會話屬性沒有公開,因爲它在Spring的InternalResourceViewResolver中。未公開Spring MVC&Freemarker會話屬性

現在,重要的部分是:當我從Spring的解析器轉換到Freemarker的解析器時,會話屬性不會被傳遞,它是空的。當Spring的解析器正在工作時,會話已通過。

我的代碼:

調度-servlet.xml中:

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context-3.1.xsd 
     http://www.springframework.org/schema/mvc 
     http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"> 

    <bean id="userSession" class="com.revicostudio.web.session.UserSession" scope="session"> 
    </bean> 

    <context:component-scan base-package="com.revicostudio.web" /> 
    <mvc:annotation-driven /> 
    <!-- 
    <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"> 
     <property name="templateLoaderPath" value="/WEB-INF/ftl/"/> 
     <property name="freemarkerVariables"> 
      <map> 
      <entry key="xml_escape" value-ref="fmXmlEscape"/> 
      </map> 
     </property> 
    </bean> 

    <bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"> 
     <property name="cache" value="true"/> 
     <property name="prefix" value=""/> 
     <property name="suffix" value=".jsp"/> 

     <property name="exposeSpringMacroHelpers" value="true"/> 
     <property name="exposeRequestAttributes" value="true"/> 
     <property name="allowRequestOverride" value="false" /> 
     <property name="exposeSessionAttributes" value="true"/> 
     <property name="allowSessionOverride" value="false" /> 
     <property name="exposePathVariables" value="true"/> 
    </bean> 

    <bean id="fmXmlEscape" class="freemarker.template.utility.XmlEscape"/>--> 

    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="prefix" value="/WEB-INF/ftl/" /> 
     <property name="suffix" value=".jsp" /> 
     <property name="exposeContextBeansAsAttributes" value="true" /> 
    </bean> 
</beans> 

LoginController.java:

@Controller 
@RequestMapping("/login") 
@SessionAttributes("userSession") 
public class LoginController { 

    @Autowired 
    private UsersDatabaseService usersDatabaseService; 

    @RequestMapping(method = RequestMethod.POST) 
    public String login(
      @ModelAttribute UserLoginCredentials userLoginCredentials, 
      UserSession userSession, 
      final RedirectAttributes redirectAttributes) 
    { 
     int failedLoginAttempts = userSession.getFailedLoginAttempts(); 

     if (failedLoginAttempts < LoginConfig.maxLoginTries || 
       System.currentTimeMillis()-userSession.getFailedLoginsWaitTimeStart() > LoginConfig.failedLoginsWaitMinutes*60*1000) { 

      if (usersDatabaseService.isValidPassword(userLoginCredentials.getUsername(), userLoginCredentials.getPassword())) { 
       userSession.setUser(usersDatabaseService.getUser(userLoginCredentials.getUsername()));  
       userSession.setFailedLoginsWaitTimeStart(System.currentTimeMillis()); 
      } 
      else { 
       failedLoginAttempts++; 

       if (failedLoginAttempts == LoginConfig.maxLoginTries) { 
        redirectAttributes.addFlashAttribute("error", 
          "You've entered invalid username or password more than " 
          + LoginConfig.maxLoginTries + " times. Try again in " 
          + LoginConfig.failedLoginsWaitMinutes +" minutes."); 
       } 
       else { 
        redirectAttributes.addFlashAttribute("error", "Invalid username or password"); 
        userSession.setFailedLoginAttempts(failedLoginAttempts); 
        System.out.println(failedLoginAttempts); 
       } 
      } 
     } 
     else { 
      redirectAttributes.addFlashAttribute("error", 
        "You've entered invalid username or password more than " 
        + LoginConfig.maxLoginTries + " times. Try again in " 
        + LoginConfig.failedLoginsWaitMinutes +" minutes."); 
     } 

     return "redirect:/"; 
    } 
} 

的IndexController:

@Controller 
@RequestMapping("/index") 
public class IndexController { 
    @RequestMapping(method=RequestMethod.GET) 
    public String getIndex(Model model) { 
     return "index"; 
    } 

    @ModelAttribute("userRegisterCredentials") 
    public UserRegisterCredentials getUserRegisterCredentials() { 
     return new UserRegisterCredentials(); 
    } 

    @ModelAttribute("userLoginCredentials") 
    public UserLoginCredentials getUserLoginCredentials() { 
     return new UserLoginCredentials(); 
    } 
} 

的index.jsp:

<body> 
    <!-- Code for Freemarker --> 
    <#if error??> 
     ${error} 
    </#if> 

    <#if userSession??> 
     ${userSession} 
    </#if> 

    <#if !(userSession??)> 
     b 
    </#if> 

    <!-- Code for JSTL --> 
    ${userSession} 

回答

0

由於redirectAttributes.addFlashAttribute(),您有會話問題。這意味着addFlashAttribute actually stores the attributes in a flashmap (which is internally maintained in the users session and removed once the next redirected request gets fulfilled),

只是嘗試沒有redirectAttributes.addFlashAttribute()你會得到會話。

可能會幫到你