上篇文章,我们围绕什么是Spring MVC进行讲述了,这篇,我们将在IDEA创建我们的第一个Spring MVC项目。

创建我们的第一个Spring MVC项目

创建项目模块

创建第一个Spring MVC项目_MVC


创建第一个Spring MVC项目_mvc_02


如果你出现上面小图这样的,代表你就成功创建成功了一个Web项目,当然方式有好几种。我偏向喜欢这种罢了。我就使用这种方式进行讲解啦。

配置依赖

接下来我们需要配置依赖了,在配置依赖之前,由于我们是通过上述自动创建的方式创建war项目,故而我们需要将<build>删除。:

<dependencies>
        <!-- SpringMVC -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.3.1</version>
        </dependency>
        <!-- 日志 -->
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.2.3</version>
        </dependency>
        <!-- ServletAPI -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
        <!-- Spring5和Thymeleaf整合包 -->
        <dependency>
            <groupId>org.thymeleaf</groupId>
            <artifactId>thymeleaf-spring5</artifactId>
            <version>3.0.12.RELEASE</version>
        </dependency>
    </dependencies>
  • 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.
  • 27.

注意依赖这步骤很重要,请各位亲们仔细一点,如果是复制粘贴的可以,就使用我的,如果是自己敲的,请各位注意一下,千万别敲错了。

注:由于 Maven 的传递性,我们不必将所有需要的包全部配置依赖,而是配置最顶端的依赖,其他靠传递性导入。

还有不是按照我的方式创建的亲们,这里是war.不是jar。

创建第一个Spring MVC项目_java_03

配置web.xml

注意,在配置这一步的时候,你需要注意的事情是,我们默认创建的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_4_0.xsd"
         version="4.0">
  <!-- 配置SpringMVC的前端控制器,对浏览器发送的请求统一进行处理 -->
  <servlet>
    <servlet-name>springMVC</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!--        我们不想将配置文件放web-inf中所要执行的操作-->
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:SpringMVC.xml</param-value>
    </init-param>
    <!--预防加载资源时间过长,将DispatcherServlet的初始化时间提前到服务器启动时间-->
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>springMVC</servlet-name>
    <!--
        设置springMVC的核心控制器所能处理的请求的请求路径
        /所匹配的请求可以是/login或.html或.js或.css方式的请求路径
        但是/不能匹配.jsp请求路径的请求
    -->
    <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.
  • 27.
  • 28.

可通过init-param标签设置SpringMVC配置文件的位置和名称,通过load-on-startup标签设置

<!-- 配置SpringMVC的前端控制器,对浏览器发送的请求统一进行处理 -->
<servlet>
   <servlet-name>springMVC</servlet-name>
   <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet.class>
   <!-- 通过初始化参数指定SpringMVC配置文件的位置和名称 -->
   <init-param>
       <!-- contextConfigLocation为固定值 -->
       <param-name>contextConfigLocation</param-name>
       <!-- 使用classpath:表示从类路径查找配置文件,例如maven工程中的src/main/resources -->
       <param-value>classpath:springMVC.xml</param-value>
   </init-param>
   <!--
       作为框架的核心组件,在启动过程中有大量的初始化操作要做
       而这些操作放在第一次请求时才执行会严重影响访问速度
       因此需要通过此标签将启动控制DispatcherServlet的初始化时间提前到服务器启动时
   -->
   <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
   <servlet-name>springMVC</servlet-name>
   <!--
       设置springMVC的核心控制器所能处理的请求的请求路径
       /所匹配的请求可以是/login或.html或.js或.css方式的请求路径
       但是/不能匹配.jsp请求路径的请求
   -->
   <url-pattern>/</url-pattern>
</servlet-mapping>
  • 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.
  • 27.

创建第一个Spring MVC项目_MVC_04


我们先将默认的index.jsp删除,我们将主要使用html进行,我们在WEB-INF目录下,创建一个Templates文件夹,用来存放我们的html文件。

创建一个index.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>首页</title>
</head>
<body>
<h1>首页</h1>
<a th:href="@{/hello}">HelloWorld</a><br/>
</body>
</html>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
创建java包和类

看上图,我们发现我们没法写代码,你发现我们现在缺了一个java文件夹来撰写我们的Java项目,现在我们来看看如何创建。

创建第一个Spring MVC项目_spring_05


选择main,然后右键选择上述图片中框红的位置,然后idea会显示Java,resources,点击一下Java就可以创建Java项目了。然后我们在Java文件夹下创建我们想要的包。

创建第一个Spring MVC项目_java_06

@Controller
public class HelloController {

    @RequestMapping("/")
    public String index() {
        //设置视图名称
        return "index";
    }
    @RequestMapping("/hello")
    public String HelloWorld() {
        return "target";
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
创建一个SpringMVC.xml文件

在上面创建了一个resources,接下来创建一个springMVC.xml文件

<?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 https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!-- 自动扫描包 -->
    <context:component-scan base-package="com.miaow.controller"/>
    <!-- 配置Thymeleaf视图解析器 -->
    <bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
        <property name="order" value="1"/>
        <property name="characterEncoding" value="UTF-8"/>
        <property name="templateEngine">
            <bean class="org.thymeleaf.spring5.SpringTemplateEngine">
                <property name="templateResolver">
                    <bean
                            class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
                        <!-- 视图前缀 -->
                        <property name="prefix" value="/WEB-INF/templates/"/>
                        <!-- 视图后缀 -->
                        <property name="suffix" value=".html"/>
                        <property name="templateMode" value="HTML5"/>
                        <property name="characterEncoding" value="UTF-8" />
                    </bean>
                </property>
            </bean>
        </property>
    </bean>
    <!--
        处理静态资源,例如html、js、css、jpg
        若只设置该标签,则只能访问静态资源,其他请求则无法访问
        此时必须设置<mvc:annotation-driven/>解决问题
    -->
    <mvc:default-servlet-handler/>
    <!-- 开启mvc注解驱动 -->
    <mvc:annotation-driven>
        <mvc:message-converters>
            <!-- 处理响应中文内容乱码 -->
            <bean
                    class="org.springframework.http.converter.StringHttpMessageConverter">
                <property name="defaultCharset" value="UTF-8" />
                <property name="supportedMediaTypes">
                    <list>
                        <value>text/html</value>
                        <value>application/json</value>
                    </list>
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>

</beans>
  • 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.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
配置Tomcat启动

接下来我们需要再Idea配置一个Tomcat,然后将项目部署到Tomcat中。

创建第一个Spring MVC项目_spring_07

创建第一个Spring MVC项目_spring_08

创建第一个Spring MVC项目_xml_09

创建第一个Spring MVC项目_mvc_10


注意,为了避免8080冲突,我将端口号改成了8008。亲,你的可以使用默认的。

启动项目

创建第一个Spring MVC项目_java_11


浏览器发送请求,若请求地址符合前端控制器的url-pattern,该请求就会被前端控制器DispatcherServlet处理。前端控制器会读取SpringMVC的核心配置文件,通过扫描组件找到控制器,将请求地址和控制器中@RequestMapping注解的value属性值进行匹配,若匹配成功,该注解所标识的控制器方法就是处理请求的方法。处理请求的方法需要返回一个字符串类型的视图名称,该视图名称会被视图解析器解析,加上前缀和后缀组成视图的路径,通过Thymeleaf对视图进行渲染,最终转发到视图所对应页面