# 3Spring MVC环境配置

  1. Maven依赖spring-webmvc
  2. web.xml配置DispatcherServlet
  3. 配置applicationContext的mvc标记
  4. 开发Controller控制器

# 导航

回到Spring MVC导航页

# Spring MVC环境配置步骤

  1. Maven依赖spring-webmvc
  2. web.xml配置DispatcherServlet
  3. 配置applicationContext的mvc标记

Maven依赖spring-webmvc

<dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.1.9.RELEASE</version>
    </dependency>
1
2
3
4
5

web.xml配置DispatcherServlet

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <!--
        DispatcherServlet是Spring MVC最核心的对象
        DispatcherServlet用于拦截Http请求,并根据请求的URL调用与之对应的Controller方法,来完成Http请求的处理
        -->
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!--启动时的一些参数-->
        <init-param>
            <param-name>contextConfigLocationc</param-name>
            <param-value>classpath:applicationContext.xml</param-value>
        </init-param>
        <!--在Web应用启动时自动创建Spring IOC容器,并初始化DispatcherServlet-->
        <load-on-startup>0</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <!--/  代表拦截所有请求-->
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mv="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">

</beans>
1
2
3
4
5
6
7
8
9
10
11
12
13

# SpringMVC中DispatchServlet的路径配置

在SpringMVC中,需要配置DispatchServlet核心对象,而对应的url-pattern属性的路径有多种情况:

<servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <!--/  代表拦截所有请求-->
        <url-pattern>/</url-pattern>
    </servlet-mapping>
1
2
3
4
5
  1. <url-pattern>/</url-pattern>: /:会匹配到/login这样的路径型url,不会匹配到模式为*.jsp这样的后缀的url
  2. <url-pattern>/ * </url-pattern>: / * 会匹配到所有url(只匹配当前文件夹下文件,不匹配子文件夹下文件):路径型的和后缀型的url(包括/login,.jsp,.js和*.html等)
  3. <url-pattern>/**</url-pattern>: / * :会匹配所有的url(匹配当前文件夹下文件及子文件夹下文件):路径型的和后缀型的url(包括/login,.jsp,.js和.html等)
    一般情况下DispatcherServlet只需要处理我们向后台服务器的请求,不需要处理静态页面等内容,所以,建议使用 /。

# 书写controller请求测试代码:

测试文件目录

成功

<dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.1.9.RELEASE</version>
    </dependency>
1
2
3
4
5
package com.torey.springmvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Author http://torey611.gitee.io/li-tao-feng/
 * @Email torey6061@qq.com
 * @Date 2021/5/4 14:28
 * @描述:
 */
@Controller
public class TestController {
    @GetMapping("/t")
    @ResponseBody //直接向响应输出字符串数据,不跳转页面
    public String test(){
        return "hell word!!";
    }
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mv="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 标签的作用:
在Spring IOC初始化过程中,自动创建并管理com.torey.springmvc包及子包中拥有以下注解的对象:
@Repository  、@Service 、@Controller  、@Component
    -->
    <context:component-scan base-package="com.torey.springmvc"></context:component-scan>
    <!--启用Spring MVC的注解开发模式-->
    <mvc:annotation-driven/>
    <!--将图片/JS/CSS等静态资源排除在外,可提高执行效率-->
    <mvc:default-servlet-handler/>
</beans>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <!--
        DispatcherServlet是Spring MVC最核心的对象
        DispatcherServlet用于拦截Http请求,并根据请求的URL调用与之对应的Controller方法,来完成Http请求的处理
        -->
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!--启动时的一些参数-->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:applicationContext.xml</param-value>
        </init-param>
        <!--在Web应用启动时自动创建Spring IOC容器,并初始化DispatcherServlet-->
        <load-on-startup>0</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <!--/  代表拦截所有请求-->
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

修改Java文件后更新

# Spring MVC处理示例图

当在浏览器输入localhost/t网址之后,这时候请求路径就发送到服务器的tomcat上,如果请求路径能匹配DispatcherServlet中配置的URL-Pattern:/,那DispatcherServlet就在应用中寻找哪些路径方法映射了/t,找到后,就将字符串按照原路径返回

springMVC处理流程

# 测试文件目录如下

测试文件目录

# 导航,上一页,下一页

2idea环境下创建MavenWebApp
4springMVC数据绑定

# 支持我-微信扫一扫-加入微信公众号

Aseven公众号

# 赞赏作者

赞赏作者 ## 种一棵树,最好的时间是十年前,其次是现在

立志用功,如种树然。方其根芽,犹未有干;及其有干,尚未有枝;枝而后叶,叶而后花、实。初种根时,只管栽培灌溉,勿作枝想,勿作实想。悬想何益?何不忘栽培之功,怕没有枝叶花实?