java 使用带有 spring mvc 的 freemarker 的第一步

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3129167/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 00:29:27  来源:igfitidea点击:

First steps with freemarker with spring mvc

javaspringspring-mvcfreemarker

提问by Blankman

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

所以我的 HomeController.java 类中有索引操作。

I have my freemarker templates in:

我有我的 freemarker 模板:

/web-inf/ftl/test.ftl

How can I load the template?

如何加载模板?

I have this in my appname-servlet.xml:

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

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

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

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

回答by eon

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

我在 spring-mvc 演示 webapp (spring 3.0.5) 中使用了 freemarker。参见http://projectnotes.svn.sourceforge.net/viewvc/projectnotes/trunk/web 控制器看起来像这样,所以你的 index.ftl(你会放在 src/main/webapp/WEB-INF 下)将被呈现当对 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

我的 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.MappingHymansonJsonView">
                  <!-- 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>

回答by Jér?me Verstrynge

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

    }

}