0

我在使用 Spring 的 Freemarker 的 viewResolver 时遇到问题 - 会话属性不会暴露给视图,就像在 Spring 的 InternalResourceViewResolver 中一样。

现在,重要的部分是:当我从 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>

登录控制器.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:/";
    }
}

索引控制器:

@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}
4

1 回答 1

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() 您将获得会话。

可能这会帮助你

于 2013-07-25T06:50:58.277 回答