一、创建项目:

1、建立新的动态web项目:

2、为项目命名为:SpringMVC_01

3、添加tomcat运行时环境\依赖库  如果是MyEclipse的话创建web项目时就不需要此步骤

右键项目,点击Build Path->Add Librares:

添加完后会多出tomcat 的 Servlet包

4、最后添加Spring及SpringMVC所需要的jar,我添加以下jar到项目中

二、配置文件:

1、首先在web.xml中配置一个DispatcherServlet,并通过<servlet-mapping>指定需要拦截的url。 下面xml中配置一个拦截.html为后缀的url.

[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片

  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>  

先配置一个servlet 然后 加载SpringMVC的xml文件到Spring的上下文中。然后配置servlet-mapping,servlet-name为刚刚的servlet中的配置的name,然后指定要拦截的url为*.html

 

2、配置Spring的上下文监听器,并且指定Spring的xml配置文件的路径。

[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片

  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>  

这里指定的路径classpath为 项目编译后的classes文件中。

最终web.xml文件内容:

[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片

  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>  

3、创建SpringMVC所需要的xml文件和applicationContext的xml文件,这里由于第一步中配置的servlet中init-param所需要加载的格式为:mvc*.* 就是去寻找为mvc开头的文件所以创建SpringMVC的xml文件时必须要有mvc开头,我命名为:mvc-context.xml,并且按照context-param中的配置,将applicationContext文件命名为:root-context.xml;

4、配置mvc-context.xml:

首先通过import标签 导入root-context.xml,然后通过component-scan标签扫描指定包名,让该包下的所有java类的spring注解生效

然后配置SpringMVC的视图渲染解析器,让其前缀为/page/ 后缀为.jsp  这样能够SpringMVC 所需要渲染的路径能够在/page/返回值.jsp中寻找。

[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片

  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>  

最后mvc-context.xml和root-context.xml为:

mvc-context.xml:

[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片

  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. </beans:beans>  

root-context.xml:

[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片

  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>  

  12.