带有spring mvc的freemarker的第一步

时间:2010-06-27 22:41:33

标签: java spring spring-mvc freemarker

所以我在HomeController.java类中有Index动作。

我的免费标记模板:

/web-inf/ftl/test.ftl

如何加载模板?

我在appname-servlet.xml中有这个:

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

    <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
        <property name="templateLoaderPath" value="/WEB-INF/freemarker/"/>
    </bean>

我的操作代码对于HomeController索引操作应该是什么样的?

我有/web-inf/freemarker/index.ftl

2 个答案:

答案 0 :(得分:3)

我在spring-mvc demo webapp(spring 3.0.5)中使用了freemarker。 见http://projectnotes.svn.sourceforge.net/viewvc/projectnotes/trunk/ Web控制器看起来像这样,因此当向index.html发出请求时,将呈现index.ftl(您将置于src / main / webapp / WEB-INF下)

@Controller
public class IndexController {

    @RequestMapping("/index.html")
    public String index(Map<String, Object> model) {

        // populate the model parameter if you need it in index.ftl   
        return "index";

    }
}

我的freemarker上下文文件如下所示

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"
       default-autowire="byName">

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

    <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
        <property name="templateLoaderPath">
            <value>/WEB-INF/freemarker/</value>
        </property>

        <property name="freemarkerVariables">
            <map>
                <entry key="xml_escape" value-ref="fmXmlEscape"/>
            </map>
        </property>

        <property name="freemarkerSettings">
            <props>
                <prop key="template_update_delay">3</prop>
            </props>
        </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=".ftl"/>
        <property name="exposeSpringMacroHelpers" value="true"/>
        <property name="exposeRequestAttributes" value="true"/>
        <property name="exposeSessionAttributes" value="true"/>
    </bean>

    <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
      <property name="mediaTypes">
          <map>
              <entry key="html" value="text/html"/>
              <entry key="ftl" value="text/html"/>
              <entry key="xml" value="application/xml"/>
              <entry key="json" value="application/json"/>
          </map>
      </property>
      <property name="favorPathExtension" value="true"/>
      <property name="defaultViews">
          <list>
              <bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
                  <!-- prevents code injection -->
                  <property name="prefixJson" value="true"/>
              </bean>
          </list>
      </property>
      <property name="viewResolvers">
          <list>
              <bean 
                  class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
                  <property name="cache" value="true" />
                  <property name="order" value="1"/>
                  <property name="prefix" value="/" />
                  <property name="suffix" value=".ftl" />
                  <property name="contentType" value="text/html;charset=UTF-8"/>
                  <property name="exposeSpringMacroHelpers" value="true" />
                  <property name="requestContextAttribute" value="rc" />
                  <property name="exposeSessionAttributes" value="true" />
              </bean>
          </list>
      </property>
    </bean>    
</beans>

答案 1 :(得分:0)

根据blog上提供的操作示例,您的控制器应如下所示:

@Controller
public class MyController {

    @RequestMapping(value = "/index")
    public String home(Model model) {

            // Populate the model as necessary
            model.addAttribute("MsTime", System.currentTimeMillis());

            return "Home";

    }

}