1. 程式人生 > >spring MVC 4.x 完整配置

spring MVC 4.x 完整配置

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
	<context:annotation-config/> 
	<context:component-scan base-package="com.midea.jr.eams.core" />
	
	<mvc:annotation-driven>
	<mvc:message-converters register-defaults="true">
      <bean class="org.springframework.http.converter.StringHttpMessageConverter">
        <property name="supportedMediaTypes" value="text/html;charset=UTF-8"/>
      </bean>
    </mvc:message-converters>
	</mvc:annotation-driven>
	
	<mvc:default-servlet-handler/>
	
	<!--避免IE執行AJAX時,返回JSON出現下載檔案 -->
	<!-- <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value> </list> </property> </bean> -->
	<!-- 啟動SpringMVC的註解功能,完成請求和註解POJO的對映 -->
    <!-- 用於將物件轉換為 JSON  -->  
       <bean id="stringConverter"  
           class="org.springframework.http.converter.StringHttpMessageConverter">  
           <property name="supportedMediaTypes">  
               <list>  
                   <value>text/plain;charset=UTF-8</value>  
               </list>  
           </property>  
       </bean>  
       <bean id="jsonConverter"  
           class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"></bean>  
       <bean  
           class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">  
           <property name="messageConverters">  
               <list>  
                   <ref bean="stringConverter" />  
                   <ref bean="jsonConverter" />  
               </list>  
           </property>  
       </bean>
	 
	
	<bean id="freemarkerConfig"  
      class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">  
	    <property name="templateLoaderPaths">
	    	<list>
	    		<value>/</value>
	    	</list>
	    </property>
	    <property name="freemarkerSettings">  
	        <props>  
	            <prop key="template_update_delay">0</prop>  
	            <prop key="default_encoding">UTF-8</prop>  
	            <prop key="number_format">0.##########</prop>  
	            <prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>  
	            <prop key="classic_compatible">true</prop>  
	            <prop key="template_exception_handler">ignore</prop>  
	        </props>
	    </property>  
	</bean> 

	<!--通用檢視解析器-->    
    <bean id="viewResolverCommon" class="org.springframework.web.servlet.view.InternalResourceViewResolver">    
        <property name="prefix" value="/"/>      
        <property name="suffix" value=".jsp"/><!--可為空,方便實現自已的依據副檔名來選擇檢視解釋類的邏輯  -->    
        <property name="viewClass">    
            <value>org.springframework.web.servlet.view.InternalResourceView</value>    
        </property>    
        <property name="order" value="1"/>
    </bean>    
    
	<bean id="viewResolverFtl" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
		<property name="suffix">
			<value>.ftl</value>
		</property>
		<property name="contentType" value="text/html;charset=UTF-8"></property>
		<property name="requestContextAttribute" value="rc" />
	</bean>
	
</beans>

json/html:

//	@RequestMapping( value= "/queryAll" , method=RequestMethod.GET, produces = "text/html;charset=UTF-8")
	@RequestMapping( value= "/queryAll" , method=RequestMethod.GET, produces="application/json;charset=UTF-8")
	public @ResponseBody List<SGlAcctNo> queryAll(HttpServletRequest request,HttpServletResponse response){
		String pageSize = request.getParameter( "pageSize" );
		String pageNum = request.getParameter( "pageNum" );
		PageParam param = new PageParam(pageSize,pageNum);
		List<SGlAcctNo> sGlAcctNos= sGlAcctNoService.queryAll( param );
		return sGlAcctNos;
	}