SpringMVC配置JSON、JSP、FreeMark多視圖解析器配置

Spring MVC屬於SpringFrameWork的後續產品,已經融合在Spring Web Flow裏面。Spring 框架提供了構建 Web 應用程序的全功能 MVC 模塊。使用 Spring 可插入的 MVC 架構,可以選擇是使用內置的 Spring Web 框架還可以是 Struts 這樣的 Web 框架。通過策略接口,Spring 框架是高度可配置的,而且包含多種視圖技術,例如 JavaServer Pages(JSP)技術、Velocity、Tiles、iText 和POI。Spring MVC 框架並不知道使用的視圖,所以不會強迫您只使用 JSP 技術。Spring M...

 1.web.xml內容: 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">  
     
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:beans.xml</param-value>
    </context-param>    
     
    <listener>        
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!--處理因使用內省API而導致的內存泄漏問題-->
    <listener>
        <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
    </listener>
     
    <filter>
        <filter-name>CharacterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>  
            <param-name>encoding</param-name>  
            <param-value>utf-8</param-value>  
        </init-param
        <init-param>  
            <param-name>forceEncoding</param-name>  
            <param-value>true</param-value>  
        </init-param>  
    </filter>
     
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
     
    <servlet>
        <servlet-name>SpringMVC</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:Servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
     
    <servlet-mapping>
        <servlet-name>SpringMVC</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>   
     
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>     
    </welcome-file-list>
</web-app>

  2.Servlet.xml內容: 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?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>

轉自:http://my.oschina.net/u/1859292/blog/309812

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章