---
title: ".spring3 mvc以freemarker作为视图层的详细配置和解释"
author: "acen.chen"
date: "2011-05-23T17:03:17Z"
tags: ["AOP", "Bean", "MVC", "Spring", "freemarker"]
categories: ["企业架构"]
slug: "spring3-mvc-freemarker"
draft: false
lastmod: "2011-05-23T17:03:17Z"
id: 1055484
url: "https://acen-chen.iteye.com/blog/1055484"
---

1、还是使用atomikos里面的那个项目，因为我使用的就是freemarker视图：

![Spring + Atomikos配置多个数据源，并且管理事务 - java_jian - java_jian的博客](http://img845.ph.126.net/6BuUqveLWPmZjwMhBsQ2Hw==/3003619476481289502.jpg)

包结构如上，其实这里面我们就只要看2个配置文件就行了：

第一个：freemarker.properties

#template\_update\_delay=1

datetime\_format=yyyy-MM-dd HH:mm:ss

date\_format=yyyy-MM-dd

time\_format=HH:mm:ss

number\_format=0.######;

boolean\_format=true,false

auto\_import="/common/index.ftl" as ui

whitespace\_stripping=true

default\_encoding=UTF-8

tag\_syntax=auto\_detect

url\_escaping\_charset=UTF-8

里面大部分都不解释了，就解释一个，

auto\_import="/common/index.ftl" as ui 这个表示每个freemarker的视图页面都会自动引入这个ftl文件。里面定义的就是一些宏，如text文本框，各种form元素，**此处不多做解释，会freemarker的都知道，这里只讲spring3mvc 配置freemarker视图。**

第一个配置文件：spring的mvc.xml在web-info下面的spring文件夹下：

&lt;?xml version="1.0" encoding="UTF-8"?&gt;

&lt;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:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc" 

xmlns:util="http://www.springframework.org/schema/util" xmlns:p="http://www.springframework.org/schema/p"

xsi:schemaLocation="

http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd

http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd

http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd

http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"&gt;

&lt;mvc:annotation-driven /&gt;

&lt;!-- 支持 @AspectJ 标记--&gt;

&lt;aop:aspectj-autoproxy proxy-target-class="true"/&gt;

&lt;context:component-scan base-package="code.main.bean.controller" /&gt;

&lt;!--**此处对应urlrewrite里面的配置，就是一些静态文件的存放目录，可以直接加载，原来spring rest里面会把这里也拦截掉，页面加载不了图片，js，样式表之类的 ，这个是3.0.4之后出来的标签**

**&lt;rule&gt; &lt;from&gt;/resources/\*\*&lt;/from&gt; &lt;to&gt;/resources/$1&lt;/to&gt; &lt;/rule&gt;**

**上面这些就是urlrewrite.xml的配置。**

 --&gt;

&lt;mvc:resources location="/resources/" mapping="/resources/\*\*" /&gt;

&lt;context:component-scan base-package="code.main.bean.aop"&gt;

&lt;context:include-filter type="annotation" expression="org.aspectj.lang.annotation.Aspect"/&gt;

&lt;/context:component-scan&gt;

&lt;!-- **使用注解映射** --&gt;

&lt;bean id="handlerMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"&gt;

&lt;/bean&gt;

&lt;!-- **让springmvc支持文件上传** --&gt;

&lt;bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"&gt;

     &lt;property name="maxInMemorySize" value="2048"&gt;&lt;/property&gt;

     &lt;property name="maxUploadSize" value="1000000000"/&gt;

     &lt;property name="uploadTempDir" value="tmoDir"&gt;&lt;/property&gt;

    &lt;/bean&gt;

&lt;!-- **让controller 返回json的配置** --&gt;

&lt;bean

class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"&gt;

&lt;property name="messageConverters"&gt;

&lt;util:list id="beanList"&gt;

&lt;ref bean="mappingJacksonHttpMessageConverter" /&gt;

&lt;/util:list&gt;

&lt;/property&gt;

&lt;/bean&gt;

&lt;!--**配置freemarker视图重点配置** --&gt;

&lt;bean id="freemarkerResolver"

class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"&gt;

&lt;property name="order" value="1" /&gt; 

&lt;property name="prefix" value="" /&gt; &lt;!--**前缀**--&gt;

&lt;property name="suffix" value=".html" /&gt;&lt;!--**后缀**--&gt;

&lt;property name="contentType" value="text/html;charset=utf-8" /&gt;&lt;!--**编码**--&gt;

&lt;property name="viewClass"&gt;

&lt;value&gt;

org.springframework.web.servlet.view.freemarker.FreeMarkerView

&lt;/value&gt;

&lt;/property&gt;

&lt;!-- **上下文，这里配置之后，fkt文件中可以使用*${rc.getContextPath()}* 来获取文件上下文，类似jsp的*request.getContextPath()*** --&gt;

&lt;property name="requestContextAttribute" value="rc"&gt;&lt;/property&gt;

&lt;!--

  如果freemarker自定义函数的话：有个属性 attributesMap这个属性，里面支持方一个map，key-value的方式来定义你的自定义函数。

类要实现freemarker提供的接口。

--&gt;

&lt;/bean&gt;

&lt;!-- **这里定义freemarker.properties文件的加载，和后面的对应。** --&gt;

&lt;bean id="**freemarkerConfiguration**" class="org.springframework.beans.factory.config.PropertiesFactoryBean"&gt;  

 &lt;property name="location" value="**classpath:freemarker.properties**"/&gt;  

&lt;/bean&gt;

&lt;bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"&gt;

&lt;property name="defaultEncoding" value="UTF-8" /&gt;

&lt;property name="templateLoaderPath" value="/WEB-INF/pages"/&gt; &lt;!-- **模板加载路径** --&gt;

&lt;property name="freemarkerSettings" ref="**freemarkerConfiguration**"/&gt;  

&lt;/bean&gt;

&lt;bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" /&gt;

&lt;/beans&gt;