创建SpringMVC项目过程

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....-->
    
    
~~但愿绝望和无奈远走高飞~~
原文地址:https://www.cnblogs.com/jinchengll/p/11789105.html