手把手教你创建 Spring MVC 实例

时间:2022-07-22
本文章向大家介绍手把手教你创建 Spring MVC 实例,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

目录

  • 1. 前言
  • 2. 创建过程
  • 3. 注意事项

1. 前言

今天来手把手创建一个 Spring MVC 的实例,看看 Spring MVC 到底应该怎么用。

2. 创建过程

  1. 首先创建一个普通的 Maven 项目;

好了,完成上面两步之后,我们的普通 Maven 项目就创建 OK 了。

  1. 既然是 Web 项目,那肯定得加入 Web 框架的支持,选中项目后右键,选择 Add Framework Support,然后选中 Web Application 添加即可;
  1. 创建 Maven 项目,并添加 Web 框架之后之后,我们的项目结构如下图:
  1. 接下来,在 pom.xml 中添加相关依赖,一般需要 Junit、Servlet、Spring MVC 等框架的依赖;
<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.13</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.2.7.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>javax.servlet.jsp-api</artifactId>
        <version>2.3.3</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>

</dependencies>

除此之外,为了避免后面有可能出现的静态资源导出问题,我们也可以在 pom.xml 添加如下配置:

<build>
    <resources>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
            <filtering>false</filtering>
        </resource>
        <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
            <filtering>false</filtering>
        </resource>
    </resources>
</build>
  1. 接下来,在 web.xml 中注册 DispatcherServlet
<servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

    <!--    绑定 Spring 配置    -->
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:springmvc-servlet.xml</param-value>
    </init-param>
    <!--    加载顺序    -->
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

同时在 /src/main/resources 目录下新建一个 Spring MVC 配置文件 springmvc-servlet.xml (文件名可以自定义,可以根据自己喜好自定义);

  1. 配置 Spring MVC 的配置文件,即上一步中所创建的 springmvc-servlet.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">
    <!--  自动扫描,由 IOC 统一管理  -->
    <context:component-scan base-package="com.cunyu.controller"/>
    <!--Spring MVC 不处理静态资源-->
    <mvc:default-servlet-handler/>
    <!--  支持 MVC 注解驱动  -->
    <mvc:annotation-driven/>

    <!--  视图解析器,此处的为 Spring 自带,也可以使用其他解析器  -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
        <!--    前后缀    -->
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>
  1. 创建控制类 Controller
package com.cunyu.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.Scanner;

/**
 * @author : cunyu
 * @version : 1.0
 * @className : HelloController
 * @date : 2020/7/21 8:19
 * @description : 控制器
 */

@Controller
public class HelloController {
    // 浏览器访问 ip:port/项目名/hello
    @GetMapping("/hello")
    public String sayHello(Model model) {
        String name = "村雨遥";
        model.addAttribute("name", name);
        // 访问 /WEB-INF/jsp/hello.jsp
        return "hello";
    }
}
  1. 创建视图层,在 WEB-INF/jsp 目录下新建 hello.jsp(没有的目录和文件需要自己创建),然后就可以取出 ControllerModel 所存放的值或者对象;
<%--
  Created by IntelliJ IDEA.
  User: cunyu
  Date: 2020/7/21
  Time: 8:23
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>村雨遥的博客</title>
</head>
<body>
你好,${name},欢迎来到我的博客!
</body>
</html>
  1. 以上步骤都配置好之后,接下来就去配置 Tomcat;

填写相关配置,同时点击 Fix

  1. 点击启动 Tomcat,然后在浏览器中访问 localhost:8080/hello

3. 注意事项

如果出现访问不了的情况,可以按照如下步骤进行设置:

  1. 依次进入 Project Structure -> Artifacts
  1. WEB-INF 目录新建 lib 目录,然后将所有 Library Files 添加到 lib 目录,然后重启 Tomcat 即可;