Spring MVC 開發 配置

一、首先在web.xml中配置一個DispatcherServlet,並經過<servlet-mapping>指定須要攔截的url。 下面xml中配置一個攔截.html爲後綴的url.html

 

  1. <!-- 配置Spring MVC DispatcherServlet -->  
  2.     <servlet>  
  3.         <servlet-name>MVC</servlet-name>  
  4.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  5.         <!-- 初始化參數 -->  
  6.         <init-param>  
  7.             <!-- 加載SpringMVC的xml到 spring的上下文容器中 -->  
  8.             <param-name>contextConfigLocation</param-name>  
  9.             <param-value>  
  10.                 /WEB-INF/classes/mvc*.*  
  11.             </param-value>  
  12.         </init-param>  
  13.         <load-on-startup>1</load-on-startup>  
  14.     </servlet>  
  15.   
  16.     <!-- 配置DispatcherServlet所須要攔截的 url -->  
  17.     <servlet-mapping>  
  18.         <servlet-name>MVC</servlet-name>  
  19.         <url-pattern>*.html</url-pattern>  
  20.     </servlet-mapping>  
<!-- 配置Spring MVC DispatcherServlet -->
	<servlet>
		<servlet-name>MVC</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<!-- 初始化參數 -->
		<init-param>
			<!-- 加載SpringMVC的xml到 spring的上下文容器中 -->
			<param-name>contextConfigLocation</param-name>
			<param-value>
				/WEB-INF/classes/mvc*.*
			</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<!-- 配置DispatcherServlet所須要攔截的 url -->
	<servlet-mapping>
		<servlet-name>MVC</servlet-name>
		<url-pattern>*.html</url-pattern>
	</servlet-mapping>

 

先配置一個servlet 而後 加載SpringMVC的xml文件到Spring的上下文中。而後配置servlet-mapping,servlet-name爲剛剛的servlet中的配置的name,而後指定要攔截的url爲*.htmljava

 

 

二、配置Spring的上下文監聽器,而且指定Spring的xml配置文件的路徑。web

 

  1. <!-- 監聽spring上下文容器 -->  
  2. <listener>  
  3.     <listener-class>  
  4.         org.springframework.web.context.ContextLoaderListener  
  5.     </listener-class>  
  6. </listener>  
  7.   
  8. <!-- 加載spring的xml配置文件到 spring的上下文容器中 -->  
  9. <context-param>  
  10.     <param-name>contextConfigLocation</param-name>  
  11.     <param-value>classpath:root-context.xml</param-value>  
  12. </context-param>  
	<!-- 監聽spring上下文容器 -->
	<listener>
		<listener-class>
			org.springframework.web.context.ContextLoaderListener
		</listener-class>
	</listener>
	
	<!-- 加載spring的xml配置文件到 spring的上下文容器中 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:root-context.xml</param-value>
	</context-param>

 

這裏指定的路徑classpath爲 項目編譯後的classes文件中。spring

 

最終web.xml文件內容:spring-mvc

 

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  5.     http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">  
  6.     <display-name></display-name>  
  7.       
  8.       
  9.     <!-- 監聽spring上下文容器 -->  
  10.     <listener>  
  11.         <listener-class>  
  12.             org.springframework.web.context.ContextLoaderListener  
  13.         </listener-class>  
  14.     </listener>  
  15.       
  16.     <!-- 加載spring的xml配置文件到 spring的上下文容器中 -->  
  17.     <context-param>  
  18.         <param-name>contextConfigLocation</param-name>  
  19.         <param-value>classpath:root-context.xml</param-value>  
  20.     </context-param>  
  21.       
  22.     <!-- 配置Spring MVC DispatcherServlet -->  
  23.     <servlet>  
  24.         <servlet-name>MVC</servlet-name>  
  25.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  26.         <!-- 初始化參數 -->  
  27.         <init-param>  
  28.             <!-- 加載SpringMVC的xml到 spring的上下文容器中 -->  
  29.             <param-name>contextConfigLocation</param-name>  
  30.             <param-value>  
  31.                 /WEB-INF/classes/mvc*.*  
  32.             </param-value>  
  33.         </init-param>  
  34.         <load-on-startup>1</load-on-startup>  
  35.     </servlet>  
  36.   
  37.     <!-- 配置DispatcherServlet所須要攔截的 url -->  
  38.     <servlet-mapping>  
  39.         <servlet-name>MVC</servlet-name>  
  40.         <url-pattern>*.html</url-pattern>  
  41.     </servlet-mapping>  
  42.   
  43.     <welcome-file-list>  
  44.         <welcome-file>index.html</welcome-file>  
  45.     </welcome-file-list>  
  46.   
  47.   
  48. </web-app>  
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
	<display-name></display-name>
	
	
	<!-- 監聽spring上下文容器 -->
	<listener>
		<listener-class>
			org.springframework.web.context.ContextLoaderListener
		</listener-class>
	</listener>
	
	<!-- 加載spring的xml配置文件到 spring的上下文容器中 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:root-context.xml</param-value>
	</context-param>
	
	<!-- 配置Spring MVC DispatcherServlet -->
	<servlet>
		<servlet-name>MVC</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<!-- 初始化參數 -->
		<init-param>
			<!-- 加載SpringMVC的xml到 spring的上下文容器中 -->
			<param-name>contextConfigLocation</param-name>
			<param-value>
				/WEB-INF/classes/mvc*.*
			</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<!-- 配置DispatcherServlet所須要攔截的 url -->
	<servlet-mapping>
		<servlet-name>MVC</servlet-name>
		<url-pattern>*.html</url-pattern>
	</servlet-mapping>

	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
	</welcome-file-list>


</web-app>



三、建立SpringMVC所須要的xml文件和applicationContext的xml文件,這裏因爲第一步中配置的servlet中init-param所須要加載的格式爲:mvc*.* 就是去尋找爲mvc開頭的文件因此建立SpringMVC的xml文件時必需要有mvc開頭,我命名爲:mvc-context.xml,而且按照context-param中的配置,將applicationContext文件命名爲:root-context.xml;mvc

 


 

 

 

四、配置mvc-context.xml:app

 

首先經過import標籤 導入root-context.xml,而後經過component-scan標籤掃描指定包名,讓該包下的全部java類的spring註解生效jsp

 

而後配置SpringMVC的視圖渲染解析器,讓其前綴爲/page/ 後綴爲.jsp  這樣可以SpringMVC 所須要渲染的路徑可以在/page/返回值.jsp中尋找。url

 

 

 

  1. <!-- 加載Spring的全局配置文件 -->  
  2.     <beans:import resource="root-context.xml" />  
  3.       
  4.     <!-- SpringMVC配置 -->  
  5.       
  6.     <!-- 經過component-scan 讓Spring掃描org.swinglife.controller下的全部的類,讓Spring的代碼註解生效 -->  
  7.     <context:component-scan base-package="org.swinglife.controller"></context:component-scan>  
  8.       
  9.     <!-- 配置SpringMVC的視圖渲染器, 讓其前綴爲:/page/ 後綴爲.jsp  將視圖渲染到/page/<method返回值>.jsp中 -->  
  10.     <beans:bean  
  11.         class="org.springframework.web.servlet.view.InternalResourceViewResolver"  
  12.         p:prefix="/page/" p:suffix=".jsp">  
  13.         </beans:bean>  

 

<!-- 加載Spring的全局配置文件 -->
	<beans:import resource="root-context.xml" />
	
	<!-- SpringMVC配置 -->
	
	<!-- 經過component-scan 讓Spring掃描org.swinglife.controller下的全部的類,讓Spring的代碼註解生效 -->
	<context:component-scan base-package="org.swinglife.controller"></context:component-scan>
	
	<!-- 配置SpringMVC的視圖渲染器, 讓其前綴爲:/page/ 後綴爲.jsp  將視圖渲染到/page/<method返回值>.jsp中 -->
	<beans:bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver"
		p:prefix="/page/" p:suffix=".jsp">
		</beans:bean>

 

 

 

 

 

最後mvc-context.xml和root-context.xml爲:spa

 

mav-context.xml:

 

 

 

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans:beans xmlns="http://www.springframework.org/schema/mvc"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"  
  4.     xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop"  
  5.     xmlns:context="http://www.springframework.org/schema/context"  
  6.     xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd  
  7.                 http://www.springframework.org/schema/aop  
  8.                 http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
  9.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">  
  10.     <!-- 加載Spring的全局配置文件 -->  
  11.     <beans:import resource="root-context.xml" />  
  12.       
  13.     <!-- SpringMVC配置 -->  
  14.       
  15.     <!-- 經過component-scan 讓Spring掃描org.swinglife.controller下的全部的類,讓Spring的代碼註解生效 -->  
  16.     <context:component-scan base-package="org.swinglife.controller"></context:component-scan>  
  17.       
  18.     <!-- 配置SpringMVC的視圖渲染器, 讓其前綴爲:/ 後綴爲.jsp  將視圖渲染到/page/<method返回值>.jsp中 -->  
  19.     <beans:bean  
  20.         class="org.springframework.web.servlet.view.InternalResourceViewResolver"  
  21.         p:prefix="/page/" p:suffix=".jsp">  
  22.         </beans:bean>  
  23.   
  24.   
  25. </beans:beans>  

 

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
	xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
				http://www.springframework.org/schema/aop
				http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
	<!-- 加載Spring的全局配置文件 -->
	<beans:import resource="root-context.xml" />
	
	<!-- SpringMVC配置 -->
	
	<!-- 經過component-scan 讓Spring掃描org.swinglife.controller下的全部的類,讓Spring的代碼註解生效 -->
	<context:component-scan base-package="org.swinglife.controller"></context:component-scan>
	
	<!-- 配置SpringMVC的視圖渲染器, 讓其前綴爲:/ 後綴爲.jsp  將視圖渲染到/page/<method返回值>.jsp中 -->
	<beans:bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver"
		p:prefix="/page/" p:suffix=".jsp">
		</beans:bean>


</beans:beans>


root-context.xml:

 

 

 

 

    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <beans xmlns="http://www.springframework.org/schema/beans"  
    3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"  
    4.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  
    5.                 http://www.springframework.org/schema/context  
    6.                  http://www.springframework.org/schema/context/spring-context-3.2.xsd  
    7.                 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">  
    8.     <!-- Root Context: defines shared resources visible to all other web components -->  
    9.   
    10.        
    11. </beans>  
相關文章
相關標籤/搜索