溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

springMVC+freemarker

發(fā)布時間:2020-06-29 21:42:01 來源:網(wǎng)絡 閱讀:1342 作者:乾坤刀 欄目:開發(fā)技術(shù)

springMVC配置文件

<?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.xsd
		http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context.xsd
		http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
		">

	<!--自動掃描含有@Controller的文件,base-package可用逗號隔開設(shè)置多個包-->
	<context:component-scan base-package="com.demo" >
	   <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
	</context:component-scan>
	
	<!-- 默認的注解映射的支持 --> 
	<mvc:annotation-driven />
	
	<!-- 配置freeMarker視圖解析器 -->
    <bean id="viewResolverFtl" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.freemarker.FreeMarkerView"/>
        <property name="contentType" value="text/html; charset=UTF-8"/>
        <property name="cache" value="true" />
        <property name="suffix" value=".ftl" />
        <!-- 將請求和會話屬性作為變量暴露給FreeMarker模板使用。要做到這一點,可以設(shè)置exposeRequestAttributes或者exposeSessionAttributes為true -->
        <property name="exposeRequestAttributes" value="true" />
        <property name="exposeSessionAttributes" value="true" />
        <!-- 使用這些宏,必須設(shè)置FreeMarkerViewResolver的exposeMacroHelpers屬性為true -->
        <property name="exposeSpringMacroHelpers" value="true" />
        <!-- freemarker獲取上下文${request.contextPath} -->
        <property name="requestContextAttribute" value="request" />
    </bean>
    
	<bean id="freemarkerConfiguration" class="org.springframework.beans.factory.config.PropertiesFactoryBean">  
        <property name="location" value="classpath:freemarker.properties" />  
    </bean> 
    
	<!-- 配置freeMarker的模板路徑 -->
    <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
        <property name="templateLoaderPath" value="/WEB-INF/ftl/" />
        <property name="freemarkerVariables">
            <map>
                <entry key="xml_escape" value-ref="fmXmlEscape" />
            </map>
        </property>
        <property name="defaultEncoding" value="UTF-8"/>
        <property name="freemarkerSettings">
            <props>
                <!--  用來指定更新模版文件的間隔時間.0表示每次都重新加載,否則為多少毫秒檢查是否更新-->
                <prop key="template_update_delay">3600</prop>
                <!-- 設(shè)置標簽類型 兩種:[] 和 <> 。[] 這種標記解析要快些 -->
                <prop key="tag_syntax">auto_detect</prop>
                <prop key="default_encoding">UTF-8</prop>  
                <prop key="output_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="time_format">HH:mm:ss</prop>
                <prop key="number_format">#.##</prop>
                <!-- 可以滿足一般需要。默認情況變量為null則替換為空字符串,如果需要自定義,寫上${empty!"EmptyValue of fbysss"}的形式即可  -->
                <prop key="classic_compatible">true</prop>
                <!-- ignore,debug,html_debug,rethrow -->
                <prop key="template_exception_handler">html_debug</prop>
            </props>
        </property>
    </bean>
    
    <bean id="fmXmlEscape" class="freemarker.template.utility.XmlEscape"/>
	
	
	<!-- 攔截器:可分別針對不同的請求設(shè)置攔截器;也可設(shè)置近似全局攔截器 . 注:如果是rest風格靜態(tài)資源也會被攔截--> 
	<mvc:interceptors> 
	   <bean class="com.demo.inteceptor.MyInteceptor" /> 
	</mvc:interceptors>
	
	<!-- 對靜態(tài)資源文件的訪問 方案一:使用默認的servlet來響應靜態(tài)文件 -->
	<mvc:default-servlet-handler />

    <!-- 異常處理 -->
	<bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
		<property name="defaultErrorView">
			<value>/error/error</value>
		</property>
		<property name="defaultStatusCode">
			<value>500</value>
		</property>
		<property name="warnLogCategory" >
			<value>org.springframework.web.servlet.handler.SimpleMappingExceptionResolver</value>
		</property>
	</bean>
	
</beans>

屬性配置文件

tag_syntax=auto_detect  
template_update_delay=60  
default_encoding=UTF-8  
output_encoding=UTF-8  
locale=zh_CN  
date_format=yyyy-MM-dd  
time_format=HH:mm:ss  
datetime_format=yyyy-MM-dd HH:mm:ss  
classic_compatible=true  
template_exception_handler=ignore


說明:需要導入freemarker.jar和spring-context-support.jar







向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI