1、导入对应jar包

    <properties>
<spring.version>5.0.2.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>

2、配置web.xml

  1. 在以前使用Servlet时候:写好对应的请求处理类,需要在web.xml中配置映射,才能将请求对应到处理类。

  2. 因此要使用Spring MVC,需要配置web.xml将所有请求交给Spring MVC来处理,因此将所有请求映射到Spring MVC的org.springframework.web.servlet.DispatcherServlet类。

  3. 在web.xml中配置如下:

    <web-app>
    <display-name>Archetype Created Web Application</display-name> <servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    </servlet> <servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
    </servlet-mapping>
    </web-app>

3、增加springmvc配置文件

  1. 在resources文件夹下增加springmvc.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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!--开启注解扫描-->
    <context:component-scan base-package="com.lin"/> <!--配置视图解析器-->
    <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/pages/"/>
    <property name="suffix" value=".jsp"/>
    </bean> <!--开启springMVC框架注解的支持-->
    <mvc:annotation-driven/> </beans>
  2. springmvc配置文件(比如包扫描)需要被web加载,才能实现我们配置的功能,因此在web.xml中配置如下:

    <web-app>
    <display-name>Archetype Created Web Application</display-name> <servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!--加载spring配置文件-->
    <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:springmvc.xml</param-value>
    </init-param>
    <!--启动时候自动加载-->
    <load-on-startup>1</load-on-startup>
    </servlet> <servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
    </servlet-mapping>
    </web-app>

    特别要说明的是:<init-param>是用来初始化<servlet-class>类中的变量的,所以<param-name>写的变量名在类中是存在的。

4、<mvc:annotation-driven>说明

  1. 在SpringMVC的各个组件中,处理器映射器(HandleMapping)、处理器适配器(HandlerAdapteMr)、视图解析器称为SpringMVC的三大组件。

  2. 使用<mvc:annotation-driven>自动加载RequestMappingHandleMapping(处理映射器)和RequestMappingHandlerAdapteMr(处理适配器),可用在SpringMVC.xml配置文件中使用<mvc:annotation-driven>替代注解处理器和适配器的配置。

  3. 它就相当于在xml中配置了:

        <!--HandlerMapping-->
    <bean id="handlerMapping" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
    <!--HandlerAdapter-->
    <bean id="handlerAdapter" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
    <!--还有很多其它的bean....-->

最新文章

  1. SAP CRM 将组件整合至导航栏中
  2. SQL Server安全概念简析
  3. 品读吴军&quot;之&quot;系列
  4. composer环境配置
  5. bootstrap框架----像素
  6. 《深入浅出MFC:》
  7. 在VS2103环境中集成Doxygen工具
  8. motan源码分析四:客户端调用服务
  9. postfix防垃圾邮件
  10. HTML5 window/iframe跨域传递消息 API
  11. 实现promise
  12. PLSQL设置细节
  13. Hbase建表时遇到的问题This could be a sign that the server has too many connections
  14. 让eclipse调试和豌豆荚并存
  15. Android Studio中新建和引用assets文件
  16. iOS开发-消息通知机制(NSNotification和NSNotificationCenter)
  17. mysql日期问题
  18. Python初学者第十三天 三级菜单程序小作业
  19. bootstrap设计进度条和圆点
  20. jdk、jre、spring、java ee、java se

热门文章

  1. tensorflow(四)
  2. TPO2-3 Early Cinema
  3. Android之布局androidmanifest.xml 资源清单 概述
  4. Spring技术内幕
  5. java代码实现数据源切换(连接池简单粗暴)
  6. 基于 maven 的ssm 框架搭建
  7. ospf实验二
  8. Windows 10操作系统针对不同环境下的安装方法
  9. valgrind 的使用简介
  10. 使用junit测试springMVC项目提示ServletContext找不到定义错误