开发者

First steps with freemarker with spring mvc

So I have Index action in my HomeController.java class.

I have my freemarker templates in:

/web-inf/ftl/test.ftl

How can I load the template?

I have this in my appname-servlet.xml:

 <bean id="viewResolver"
          class="org.s开发者_C百科pringframework.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>

What should my action code look like for HomeController index action?

I have /web-inf/freemarker/index.ftl


I have used freemarker in a spring-mvc demo webapp (spring 3.0.5). See http://projectnotes.svn.sourceforge.net/viewvc/projectnotes/trunk/ The web controller looks like this, so your index.ftl (which you would put under src/main/webapp/WEB-INF) would be rendered when a request is made to index.html

@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";

    }
}

My freemarker context file looks like this

<?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>


From an operational example available on my blog, your controller should look something like this:

@Controller
public class MyController {

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

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

            return "Home";

    }

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜