ITeye博客

.spring3 mvc以freemarker作为视图层的详细配置和解释

acen.chen · 2011-05-23 17:03 · 企业架构 · Spring · freemarker · MVC · AOP · Bean · Markdown

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

包结构如上,其实这里面我们就只要看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文件夹下:

<?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: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">

<mvc:annotation-driven />

<!-- 支持 @AspectJ 标记-->

<aop:aspectj-autoproxy proxy-target-class="true"/>

<context:component-scan base-package="code.main.bean.controller" />

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

<rule> <from>/resources/**</from> <to>/resources/$1</to> </rule>

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

 -->

<mvc:resources location="/resources/" mapping="/resources/**" />

<context:component-scan base-package="code.main.bean.aop">

<context:include-filter type="annotation" expression="org.aspectj.lang.annotation.Aspect"/>

</context:component-scan>

<!-- 使用注解映射 -->

<bean id="handlerMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">

</bean>

<!-- 让springmvc支持文件上传 -->

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

     <property name="maxInMemorySize" value="2048"></property>

     <property name="maxUploadSize" value="1000000000"/>

     <property name="uploadTempDir" value="tmoDir"></property>

    </bean>

<!-- 让controller 返回json的配置 -->

<bean

class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">

<property name="messageConverters">

<util:list id="beanList">

<ref bean="mappingJacksonHttpMessageConverter" />

</util:list>

</property>

</bean>

<!--配置freemarker视图重点配置 -->

<bean id="freemarkerResolver"

class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">

<property name="order" value="1" /> 

<property name="prefix" value="" /> <!--前缀-->

<property name="suffix" value=".html" /><!--后缀-->

<property name="contentType" value="text/html;charset=utf-8" /><!--编码-->

<property name="viewClass">

<value>

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

</value>

</property>

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

<property name="requestContextAttribute" value="rc"></property>

<!--

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

类要实现freemarker提供的接口。

-->

</bean>

<!-- 这里定义freemarker.properties文件的加载,和后面的对应。 -->

<bean id="freemarkerConfiguration" class="org.springframework.beans.factory.config.PropertiesFactoryBean">  

 <property name="location" value="classpath:freemarker.properties"/>  

</bean>

<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">

<property name="defaultEncoding" value="UTF-8" />

<property name="templateLoaderPath" value="/WEB-INF/pages"/> <!-- 模板加载路径 -->

<property name="freemarkerSettings" ref="freemarkerConfiguration"/>  

</bean>

<bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />

</beans>

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。