SpringMvc使用Freemarker時的配置檔案
阿新 • • 發佈:2019-02-13
一、在使用Freemarker 時,需要在springmvc-servlet.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:mvc="http://www.springframework.org/schema/mvc" 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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> <!-- 元件自動掃描 --> <context:component-scan base-package="com.tliu.case2.web"/> <!--主要作用於@Controller啟用該模式下面是一種簡寫形式 它會自動註冊DefaultAnnotationHandlerMapping與AnnotationMethodHandlerAdapter, 是spring MVC為@Controllers分發請求所必須的 --> <mvc:annotation-driven/> <!-- 配置JSON檢視 --> <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>application/json;charset=UTF-8</value> </list> </property> <property name="objectMapper"> <bean class="org.codehaus.jackson.map.ObjectMapper"> <property name="dateFormat"> <bean class="java.text.SimpleDateFormat"> <constructor-arg index="0" type="java.lang.String" value="yyyy-MM-dd HH:mm:ss"/> </bean> </property> </bean> </property> </bean> <bean id="stringHttpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter"/> <bean id="requestMappingHandlerAdapter" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> <property name="messageConverters"> <list> <ref bean="mappingJacksonHttpMessageConverter"/> <ref bean="stringHttpMessageConverter"/> </list> </property> </bean> <!-- 配置JSP檢視 --> <bean id="internalResourceViewResolver" 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"/> <property name="contentType" value="text/html;charset=UTF-8"/> <property name="order" value="1"/> </bean> <!-- 配置FreeMark檢視 --> <bean id="freeMarkerViewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"> <property name="contentType" value="text/html;charset=UTF-8"/> <property name="viewClass" value="org.springframework.web.servlet.view.freemarker.FreeMarkerView"/> <property name="suffix" value=".ftl"/> <property name="cache" value="true"/> <property name="exposeSessionAttributes" value="true"/> <property name="exposeRequestAttributes" value="true"/> <property name="exposeSpringMacroHelpers" value="true"/> <!-- 在頁面中使用${rc.contextPath}就可獲得contextPath --> <property name="requestContextAttribute" value="rc"/> <property name="order" value="0"/> </bean> <bean id="freemarkConfig" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> <property name="location" value="classpath:freemark.properties"/> </bean> <bean id="fmXmlEscape" class="freemarker.template.utility.XmlEscape"/> <bean id="FreeMarkerConfigurer" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"> <property name="templateLoaderPath" value="/WEB-INF/ftl/"/> <property name="defaultEncoding" value="UTF-8"/> <property name="freemarkerSettings" ref="freemarkConfig"/> <property name="freemarkerVariables"> <map> <entry key="xml_escape" value-ref="fmXmlEscape"/> </map> </property> </bean> <!-- 檔案上傳配置注意:這裡申明的id必須為multipartResolver --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="500000"/> </bean> <!-- 簡單的異常處理 --> <bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> <property name="exceptionMappings"> <props> <!-- 對映目錄為/WEB-INF/jsp/error/upload_error.jsp --> <prop key="org.springframework.web.multipart.MaxUploadSizeExceededException">/error/upload_error</prop> </props> </property> </bean> <!-- 對靜態資原始檔的訪問 --> <mvc:resources mapping="/images/**" location="/images/" cache-period="31556926"/> <mvc:resources mapping="/js/**" location="/js/" cache-period="31556926" /> <mvc:resources mapping="/css/**" location="/css/" cache-period="31556926" /> <mvc:resources mapping="/upload/**" location="/upload/" cache-period="31556926" /> </beans>
該檔案中的freemarker.properties檔案配置內容如下:
#FreeMarker settings:
#0 is for development only! Use higher value otherwise.
template_update_delay=0
locale=zh_CN
default_encoding=UTF-8
number_format=0.##########
date_format=yyyy-MM-dd
二、在該檔案中,freemarker檔案的配置也可以採用如下形式:
<?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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd" > <!-- 開啟註解 --> <mvc:annotation-driven /> <!-- 掃描包 --> <context:component-scan base-package="com.misl.*" /> <!-- FreeMarker settings --> <bean id="freeMarkerConfigurer" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"> <property name="templateLoaderPath" value="" /> <property name="freemarkerSettings"> <props> <prop key="template_update_delay">0</prop> <prop key="default_encoding">UTF-8</prop> <prop key="locale">zh_CN</prop> </props> </property> </bean> <!-- FreeMarker view Reslover --> <bean id="freeMarkerViewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"> <property name="cache" value="false" /> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".ftl" /> <property name="contentType" value="text/html;charset=UTF-8" /> </bean> </beans>