SpringMvc使用Freemarker时的配置文件

一、在使用Freemarker 时,需要在springmvc-servlet.xml 配置文件中作如下配置:

[html] view plain copy print ?
  1. xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"       
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         
  4.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xmlns:mvc="http://www.springframework.org/schema/mvc"  
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans                 
  7.            http://www.springframework.org/schema/beans/spring-beans-3.2.xsd        
  8.            http://www.springframework.org/schema/context        
  9.            http://www.springframework.org/schema/context/spring-context-3.2.xsd       
  10.            http://www.springframework.org/schema/mvc        
  11.            http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">  
  12.               
  13.              
  14.      <context:component-scan base-package="com.tliu.case2.web"/>   
  15.         
  16.        
  17.      <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">  
  18.        <property name="supportedMediaTypes">  
  19.            <list>  
  20.                <value>application/json;charset=UTF-8value>  
  21.            list>  
  22.        property>       
  23.        <property name="objectMapper">  
  24.            <bean class="org.codehaus.jackson.map.ObjectMapper">  
  25.                <property name="dateFormat">  
  26.                    <bean class="java.text.SimpleDateFormat">  
  27.                        <constructor-arg index="0" type="java.lang.String" value="yyyy-MM-dd HH:mm:ss"/>  
  28.                    bean>  
  29.                property>  
  30.            bean>  
  31.        property>  
  32.      bean>  
  33.      <bean id="stringHttpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter"/>  
  34.      <bean id="requestMappingHandlerAdapter" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">  
  35.        <property name="messageConverters">  
  36.            <list>  
  37.                <ref bean="mappingJacksonHttpMessageConverter"/>  
  38.                <ref bean="stringHttpMessageConverter"/>  
  39.            list>  
  40.        property>  
  41.      bean>  
  42.        
  43.        
  44.      <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  45.         <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>  
  46.         <property name="prefix" value="/WEB-INF/jsp/"/>  
  47.         <property name="suffix" value=".jsp"/>        
  48.         <property name="contentType" value="text/html;charset=UTF-8"/>  
  49.         <property name="order" value="1"/>  
  50.      bean>  
  51.         
  52.       
  53.     <bean id="freeMarkerViewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">  
  54.         <property name="contentType" value="text/html;charset=UTF-8"/>        
  55.         <property name="viewClass" value="org.springframework.web.servlet.view.freemarker.FreeMarkerView"/>  
  56.         <property name="suffix" value=".ftl"/>  
  57.         <property name="cache" value="true"/>  
  58.         <property name="exposeSessionAttributes" value="true"/>  
  59.         <property name="exposeRequestAttributes" value="true"/>       
  60.         <property name="exposeSpringMacroHelpers" value="true"/>  
  61.           
  62.         <property name="requestContextAttribute" value="rc"/>  
  63.         <property name="order" value="0"/>  
  64.     bean>  
  65.        
  66.     <bean id="freemarkConfig" class="org.springframework.beans.factory.config.PropertiesFactoryBean">  
  67.         <property name="location" value="classpath:freemark.properties"/>  
  68.     bean>  
  69.        
  70.     <bean id="fmXmlEscape" class="freemarker.template.utility.XmlEscape"/>  
  71.        
  72.     <bean id="FreeMarkerConfigurer" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">  
  73.         <property name="templateLoaderPath" value="/WEB-INF/ftl/"/>  
  74.         <property name="defaultEncoding" value="UTF-8"/>  
  75.         <property name="freemarkerSettings" ref="freemarkConfig"/>  
  76.         <property name="freemarkerVariables">  
  77.             <map>  
  78.                 <entry key="xml_escape" value-ref="fmXmlEscape"/>  
  79.             map>  
  80.         property>  
  81.     bean>  
  82.        
  83.       
  84.     <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  
  85.        <property name="maxUploadSize" value="500000"/>  
  86.     bean>  
  87.        
  88.       
  89.     <bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">  
  90.         <property name="exceptionMappings">  
  91.             <props>  
  92.                   
  93.                 <prop key="org.springframework.web.multipart.MaxUploadSizeExceededException">/error/upload_errorprop>  
  94.             props>  
  95.         property>  
  96.     bean>  
  97.        
  98.       
  99.     <mvc:resources mapping="/images/**" location="/images/" cache-period="31556926"/>  
  100.            
  101.     <mvc:resources mapping="/js/**" location="/js/" cache-period="31556926" />  
  102.            
  103.     <mvc:resources mapping="/css/**" location="/css/" cache-period="31556926" />        
  104.        
  105.     <mvc:resources mapping="/upload/**" location="/upload/" cache-period="31556926" />      
  106. beans>  

该文件中的freemarker.properties文件配置内容如下:

[html] view plain copy print ?
  1. #FreeMarker settings:  
  2.    
  3. #0 is for development only! Use higher value otherwise.  
  4. template_update_delay=0  
  5. locale=zh_CN  
  6. default_encoding=UTF-8  
  7. number_format=0.##########  
  8. date_format=yyyy-MM-dd  

 


二、在该文件中,freemarker文件的配置也可以采用如下形式:

[html] view plain copy print ?
  1. xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"   
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xmlns:p="http://www.springframework.org/schema/p"   
  5.     xmlns:context="http://www.springframework.org/schema/context"  
  6.     xmlns:util="http://www.springframework.org/schema/util"  
  7.     xmlns:mvc="http://www.springframework.org/schema/mvc"  
  8.     xsi:schemaLocation="  
  9.         http://www.springframework.org/schema/util  
  10.         http://www.springframework.org/schema/util/spring-util-3.1.xsd  
  11.         http://www.springframework.org/schema/beans   
  12.         http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
  13.         http://www.springframework.org/schema/context   
  14.         http://www.springframework.org/schema/context/spring-context-3.1.xsd  
  15.         http://www.springframework.org/schema/mvc  
  16.        http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd" >  
  17.        
  18.     <mvc:annotation-driven />  
  19.       
  20.     <context:component-scan base-package="com.misl.*" />  
  21.       
  22.       
  23.     <bean id="freeMarkerConfigurer" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">  
  24.         <property name="templateLoaderPath" value="" />  
  25.         <property name="freemarkerSettings">  
  26.             <props>  
  27.                 <prop key="template_update_delay">0prop>  
  28.                 <prop key="default_encoding">UTF-8prop>  
  29.                 <prop key="locale">zh_CNprop>  
  30.             props>  
  31.         property>  
  32.     bean>  
  33.       
  34.     <bean id="freeMarkerViewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">  
  35.         <property name="cache" value="false" />  
  36.         <property name="prefix" value="/WEB-INF/views/" />  
  37.         <property name="suffix" value=".ftl" />  
  38.         <property name="contentType" value="text/html;charset=UTF-8" />  
  39.     bean>  
  40.       
  41. beans>  


 

 

你可能感兴趣的:(spring)