First steps with freemarker with spring mvc

不问归期 提交于 2019-12-10 10:36:55

问题


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

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

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


回答1:


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>



回答2:


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

    }

}


来源:https://stackoverflow.com/questions/3129167/first-steps-with-freemarker-with-spring-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!