FreeMarker

来源:互联网 发布:淘宝客微信淘口令 编辑:程序博客网 时间:2024/06/05 00:13

使用RESTful风格,故需要额外注意DispatcherServlet的url-pattern和applicationContext.xml中的<mvc:default-servlet-handler/>

【1】xml配置

applicationContext.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:tx="http://www.springframework.org/schema/tx"    xmlns:mvc="http://www.springframework.org/schema/mvc"     xmlns:aop="http://www.springframework.org/schema/aop"    xsi:schemaLocation="http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans.xsd                             http://www.springframework.org/schema/context   http://www.springframework.org/schema/context/spring-context.xsd                             http://www.springframework.org/schema/tx   http://www.springframework.org/schema/tx/spring-tx.xsd                            http://www.springframework.org/schema/mvc   http://www.springframework.org/schema/mvc/spring-mvc.xsd                            http://www.springframework.org/schema/aop   http://www.springframework.org/schema/aop/spring-aop.xsd">    <context:component-scan base-package="com" />     <mvc:annotation-driven />     <mvc:default-servlet-handler/>     <!-- 配置spring mvc -->    <bean id="freemarkerConfigurer"          class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">           <property name="templateLoaderPath" value="/WEB-INF/views/" />         <property name="freemarkerSettings">              <props>                  <!-- 禁用freemarker缓存 -->                <prop key="template_update_delay">0</prop>                   <prop key="default_encoding">UTF-8</prop>                  <prop key="locale">zh_CN</prop>                  <prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>                  <prop key="date_format">yyyy-MM-dd</prop>                  <prop key="number_format">#.##</prop>              </props>          </property>        <property name="freemarkerVariables">              <map>                  <entry key="xml_escape" value-ref="fmXmlEscape" />              </map>        </property>      </bean>    <bean id="fmXmlEscape" class="freemarker.template.utility.XmlEscape" />     <!-- 如果属性配置在properties文件中,可使用如下代码(上面的freemarkerSettings可配置在properties文件中) --><!--     <bean id="freemarkerStaticModelsProps"  --><!--         class="org.springframework.beans.factory.config.PropertiesFactoryBean">  --><!--         <property name="location" value="classpath:freemarker_static.properties" />  --><!--     </bean> -->    <bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">        <!-- 禁用SpringMVC缓存 -->        <property name="cache" value="false" />        <property name="viewClass" value="org.springframework.web.servlet.view.freemarker.FreeMarkerView"/>        <!-- 扫描路径內所有以ftl結尾的文件 viewNames 和suffix 只能存在一个 -->  <!--         <property name="viewNames">   --><!--             <array>   --><!--                 <value>*.ftl</value>   --><!--             </array>   --><!--         </property> -->        <property name="suffix" value=".ftl" />        <property name="contentType" value="text/html;charset=UTF-8"/>        <property name="requestContextAttribute" value="request" />        <property name="exposeSpringMacroHelpers" value="true" />        <property name="exposeRequestAttributes" value="true" />        <property name="exposeSessionAttributes" value="true" />    </bean></beans>

web.xml配置

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee"    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"    version="3.0">    <filter>        <filter-name>encoding</filter-name>        <filter-class>org.springframework.web.filter.CharacterEncodingFilter        </filter-class>        <init-param>            <param-name>encoding</param-name>            <param-value>UTF-8</param-value>        </init-param>    </filter>    <filter-mapping>        <filter-name>encoding</filter-name>        <url-pattern>/*</url-pattern>    </filter-mapping>    <servlet>        <servlet-name>hh-mvc</servlet-name>        <servlet-class>org.springframework.web.servlet.DispatcherServlet        </servlet-class>        <init-param>            <param-name>namespace</param-name>            <param-value>hh-mvc</param-value>        </init-param>        <init-param>            <param-name>contextConfigLocation</param-name>            <param-value>classpath*:applicationContext.xml</param-value>        </init-param>        <init-param>            <param-name>publishContext</param-name>            <param-value>true</param-value>        </init-param>        <load-on-startup>1</load-on-startup>    </servlet>    <servlet-mapping>        <servlet-name>hh-mvc</servlet-name>        <!-- 注意这里 -->        <url-pattern>/</url-pattern>    </servlet-mapping>    <welcome-file-list>        <welcome-file>index.jsp</welcome-file>    </welcome-file-list></web-app>

【2】后台代码测试

package com.web.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.servlet.ModelAndView;@RequestMapping("/data")@Controllerpublic class DataController {    @RequestMapping("/hello")    public ModelAndView hello(){        ModelAndView mv = new ModelAndView();        mv.addObject("msg", "hello world !");        mv.setViewName("hello");        return mv;    }}

【3】FTL模板

<#assign base=request.contextPath/><!DOCTYPE html><html><head>    <base id="base" href="${base}">    <meta charset="UTF-8">    <title>hello ftl</title><script type="text/javascript">    var base = document.getElementById("base").href;    console.log(base);</script></head><body>    ${msg}</body></html>
原创粉丝点击