1. 程式人生 > >springmvc freemarker jsp多檢視配置

springmvc freemarker jsp多檢視配置

spring-mvc.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">


<!--基於validate的載入配置-->
   <mvc:annotation-driven validator="validator"/>

<bean id= "validator" class= "org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
            <property name= "providerClass"  value= "org.hibernate.validator.HibernateValidator"/>
            <!-- 如果不加 預設使用classpath下的 ValidationMessages.properties -->
            <property name= "validationMessageSource" ref= "messageSource"/>
    </bean>
    
<context:component-scan base-package="com.icwant.client.controller">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan>

<!-- 啟用AspectJ對Annotation的支援          -->
    <aop:aspectj-autoproxy proxy-target-class="true"/>
    <bean id="inputFilterAspect" class="com.icwant.client.aop.InputFilterAspect"></bean>
    
<!-- jsp檢視解析器 -->
<bean id="viewResolver"
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="order" value="1" />
</bean>


<!-- 引入freemarker配置檔案 -->
  <context:property-placeholder location="classpath:config/freemarker.properties"
ignore-unresolvable="true" />

<!-- 配置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" />
<!-- 解析器的優先級別。我們將FreeMarkerViewResolver的級別設為0,將InternalResourceViewResolver的級別設為1。 -->
<!-- 這樣,解析器就會優先使用 FreeMarkerViewResolver 進行解析,如果找不到相應的模板,就使用InternalResourceViewResolver進行解析 -->
<property name="order" value="0" />
<property name="requestContextAttribute" value="request"/>
</bean>




<!-- 配置freeMarker的模板路徑 -->
<bean id="freemarkerConfig"
class="com.icwant.client.freemarker.ShiroTagFreeMarkerConfigurer">
<property name="templateLoaderPath" value="${baseDir}" />
<property name="freemarkerVariables">
<map>
<entry key="xml_escape" value-ref="fmXmlEscape" />
</map>
</property>
<property name="defaultEncoding" value="${default_encoding}" />
<property name="freemarkerSettings">
<props>
<!-- 模版快取和模版問卷同步頻率 0表示不讀取快取,每次都讀取模版檔案 -->
<prop key="tagSyntax">${tag_syntax}</prop>
<prop key="template_update_delay" >${template_update_delay}</prop>
<prop key="auto_import">${auto_import}</prop>
<prop key="auto_include">${auto_include}</prop>
<prop key="outputEncoding">${output_encoding}</prop>
<prop key="numberFormat">${number_format}</prop>
<prop key="classicCompatible">${classic_compatible}</prop>
</props>
</property>
</bean>


<bean id="fmXmlEscape" class="freemarker.template.utility.XmlEscape" />


<!--檔案上傳 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="1000000000" />
</bean>


<!-- 異常處理類 -->
<bean id="exceptionResolver"
class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="defaultErrorView">
<value>error</value>
</property>
<property name="exceptionMappings">
<props>
<prop key="java.sql.SQLException">error</prop>
<prop key="java.lang.RuntimeException">error</prop>
</props>
</property>

</bean>

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

</beans>


freemarker.properties:

#ftl模版所在根路徑
baseDir=/WEB-INF/ftl/
#設定標籤型別:square_bracket:[]     auto_detect:[]<>    
tag_syntax=auto_detect    
#模版快取時間,單位:秒   
template_update_delay=0
default_encoding=UTF-8
output_encoding=UTF-8
locale=zh_CN
#設定數字格式,防止出現000.00 
number_format=\#    
#變數為空時,不會報錯 
classic_compatible=true   
#這個表示每個freemarker的檢視頁面都會自動引入這個ftl檔案。裡面定議的就是一些巨集,如text文字框,各種form元素   
auto_import="/base/spring.ftl" as sp
auto_include="/base/constant.ftl"