Spring MVC 之基本配置和請求流程

Spring MVC 基本配置項

SpringMVC是目前Web程序和前端數據交互的框架,包括文件上傳,靜態資源下載,api接口數據返回等,主要是配置一個spring-mvc.xml的配置文件,這個配置文件是給org.springframework.web.servlet.DispatcherServlet這個類使用的,初始化DispatcherServlet這個類須要傳入配置文件地址。前端

在web.xml中初始化DispatcherServletweb

<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:spring-mvc.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>

而後在src/main/resources中建立spring-mvc.xml文件,配置以下:spring

<?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-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/mvc 
      http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
  
   <!-- 自動掃面controller包 -->
	<context:component-scan base-package="com.content.spider.ru.controller" />

	<mvc:default-servlet-handler /> <!--默認handler-->
   <mvc:annotation-driven />       <!-- 使用默認註解解析器,@Controller註解生效前提 -->

  <!-- 配置@ResponseBody註釋後返回的數據格式,使用fastjson -->
	<mvc:annotation-driven>
		<mvc:message-converters register-defaults="true">
			<bean id="fastJsonHttpMessageConverter"
				class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
				<property name="supportedMediaTypes">
					<list>
						<value>application/json;charset=UTF-8</value>
					</list>
				</property>
			</bean>
		</mvc:message-converters>
	</mvc:annotation-driven>
  
  <!-- 配置@ResponseBody註釋後返回的數據格式,使用jackson -->
<!--
 <mvc:annotation-driven>
	<mvc:message-converters register-defaults="true">
		<bean id="jacksonMessageConverter"
			class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
				<property name="supportedMediaTypes">
					<list>
						<value>text/json;charset=UTF-8</value>
					</list>
				</property>
			</bean>
		</mvc:message-converters>
 </mvc:annotation-driven>
-->

    
    <!-- 配置modelview返回,自動加上前綴和後綴-->
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/view/" />
		<property name="suffix" value=".jsp" />
	</bean>
  
  <!-- 拋出異常,返回接口處理-->
    <bean id="exceptionResolver" class="com.content.spider.common.web.HandlerExceptionResolver"  />
   
    
   <!-- 文件上傳使用 --> 
    <bean id="multipartResolver"
		class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<property name="maxUploadSize" value="209715200" /><!-- 200M -->
	</bean>
    
  
    <!--
	     因爲DispatchServlet攔截了全部的請求,也包括靜態資源
    	 因此對於靜態資源的訪問,咱們須要在這裏進行配置
     -->
    <!-- 配置靜態資源映射,若是是maven項目,則是 src/main/webapp/webeditor目錄下的靜態資源 -->
	<mvc:resources mapping="/images/**" location="/WEB-INF/images/" />
	<mvc:resources mapping="/styles/**" location="/WEB-INF/styles/" />
	<mvc:resources mapping="/script/**" location="/WEB-INF/script/" />
	<mvc:resources mapping="/fonts/**" location="/WEB-INF/fonts/" />
    <!-- 資源重定向(僅做爲後臺使用不提供靜態資源) -->
	<mvc:resources mapping="upload/**" location="/WEB-INF/upload/" cache-period="300" />  

</beans>

 

Spring MVC請求流程

相關文章
相關標籤/搜索