首页 > 代码库 > SpringMVC配置JSON、JSP、FreeMark多视图解析器配置

SpringMVC配置JSON、JSP、FreeMark多视图解析器配置

 1.web.xml内容: 

<?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内容: 

<?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=http://www.mamicode.com/"yyyy-MM-dd HH:mm:ss"/>> 


SpringMVC配置JSON、JSP、FreeMark多视图解析器配置