Java springmvc&;自由标记/速度

Java springmvc&;自由标记/速度,java,spring,session,velocity,freemarker,Java,Spring,Session,Velocity,Freemarker,我对Freemarker和Velocity的视图解析器(不同时运行)有一个问题——它们都看不到Spring的会话bean。Spring的InternalResourceViewResolver运行良好 一些代码: <context:component-scan base-package="com.revicostudio.web" /> <mvc:annotation-driven /> <bean id="userSession" class="com.revi

我对Freemarker和Velocity的视图解析器(不同时运行)有一个问题——它们都看不到Spring的会话bean。Spring的InternalResourceViewResolver运行良好

一些代码:

<context:component-scan base-package="com.revicostudio.web" />
<mvc:annotation-driven />

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

<bean id="velocityConfig" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
    <property name="resourceLoaderPath" value="/WEB-INF/jsp/" />
</bean>

<bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver">
    <property name="cache" value="true" />
    <property name="prefix" value="" />
    <property name="layoutUrl" value="layout.jsp"/>
    <property name="suffix" value=".jsp" />
    <property name="exposeSessionAttributes" value="true" />
    <property name="exposeRequestAttributes" value="true" />
    <property name="requestContextAttribute" value="rc" />
</bean>

1.您应该对控制器进行注释,指出在会话中应该公开哪个模型属性 2.在freemarker中,对会话属性的访问由freemarker会话包装器完成

下面的简短示例基于您的代码:

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

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

}
在ftl端:${Session.userRegisterCredentials.someStringField}

@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();
    }
}
@Controller
@SessionAttributes("userRegisterCredentials")
@RequestMapping("/index")
public class IndexController {
    @RequestMapping(method=RequestMethod.GET)
    public String getIndex(Model model) {
        return "index";
    }

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

}