Hello World 之 Freemarker

FreeMarker 是一款 模板引擎: 即一种基于模板和要改变的数据, 并用来生成输出文本(HTML网页,电子邮件,配置文件,源代码等)的通用工具。 它不是面向最终用户的,而是一个Java类库,是一款程序员可以嵌入他们所开发产品的组件。

1.maven引入所需jar包

    <!-- freemarker jar -->
     <dependency>
         <groupId>org.freemarker</groupId>
         <artifactId>freemarker</artifactId>
        <version>2.3.20</version>
     </dependency>

2.spring-freemarker.xml 相关配置含义

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
    xmlns:jee="http://www.springframework.org/schema/jee" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">

     <!-- freemarker的配置属性 -->
    <bean id="freemarkerConfig"
          class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
    <!-- 此处为模板存放路径 -->
        <property name="templateLoaderPath" value="/WEB-INF/ftl/" />
        <property name="freemarkerSettings">
            <props>
                <prop key="template_update_delay">0</prop>
                <prop key="default_encoding">UTF-8</prop>
                <prop key="number_format">0.##########</prop>
                <prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>
                <prop key="classic_compatible">true</prop>
                <prop key="template_exception_handler">ignore</prop>
            </props>
        </property>
    </bean>

        <!-- View resolvers can also be configured with ResourceBundles or XML files. 
        If you need different view resolving based on Locale, you have to use the 
        resource bundle resolver. -->


    <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
        <property name="ignoreAcceptHeader" value="true" />
        <property name="defaultContentType" value="text/html" />
        <property name="mediaTypes">
            <map>
                <entry key="json" value="application/json" />
            </map>
        </property>
        <property name="favorParameter" value="false" />
        <property name="viewResolvers">
            <list>
                <!-- 配置freeMarker视图解析器 -->
                <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="contentType" value="text/html;charset=UTF-8" />
                    <property name="requestContextAttribute" value="request" />
                    <property name="exposeRequestAttributes" value="true" />
                    <property name="exposeSessionAttributes" value="true" />
                    <property name="exposeSpringMacroHelpers" value="true" />
                </bean>
                <!-- <bean
                    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                    <property name="viewClass"
                        value="org.springframework.web.servlet.view.JstlView" />
                    <property name="prefix" value="/WEB-INF/jsp/" />
                    <property name="suffix" value=".jsp" />
                </bean> -->
            </list>
        </property>
    </bean>
</beans>

3.web.xml 包含spring-freemarker.xml

  <servlet>
    <description></description>
    <display-name>DispatcherServlet</display-name>
    <servlet-name>DispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <description></description>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:conf/spring-*.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>

4.创建模板文件 hello.ftl 5.java -controller类 return的字符串对应模板名

      //测试freemarker
        @RequestMapping("hello")
        public String getDetail(Model model){
            model.addAttribute("name","freemarker");
            return "hello";
        }

6.测试结果

7.[freemarker官方中文手册地址]

--END--


 点赞  打赏
  • 暂无评论