• Java真的不难(四十五)SpringMVC的入门


    SpringMVC

    什么是MVC?

    我想大家对MVC应该都不陌生了,在之前也应该都接触过,也就是分层开发

    MVC: 全名是 Model view Controller,是模型(Model)、视图(View)、控制层(controller)的缩写
    而SpringMVC是基于Spring对web层的支持,实现了对WebMVC设计模式的请求驱动类型的轻量级Web框架,使用了MVC的架构设计思想,将Web层进行解耦,并管理其生命周期,为简化日常开发提供了很大的便利

    SSM:Spring、SpringMVC、Mybatis
    在之后的开发中,可以用SpringMVC来代替Servlet做控制层,Spring用来管理对象的,Mybatis用来代替JDBC做持久层

    说白了SpringMVC就是对Servlet的封装,也就是Servlet可以做什么,SpringMVC也一样不差,甚至可以做的更多乃至更简单方便。


    SpringMVC的执行流程:
    在这里插入图片描述


    一、SpringMVC的导入

    还是采用maven来管理项目,直接在pom.xml文件内写入依赖即可:

            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>5.3.21</version>
            </dependency>
    
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-web</artifactId>
                <version>5.3.21</version>
            </dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    二、实现Controller 方法使用MVC

    接着编写我们要操作业务Controller ,要么实现Controller接口,要么增加注解;需要返回一个ModelAndView,装数据

    HelloMVC:

    public class HelloMVC implements Controller {
    
        @Override
        public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
            ModelAndView mv = new ModelAndView();
    
            //封装对象,放在ModelAndView中
            mv.addObject("msg","你好,SpringMVC");
    
            //相当于拼接成/WEB-INF/jsp/hello.jsp
            mv.setViewName("hello");
            return mv;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    接着配置一下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">
    
          <!--注册一个servlet-->
        <servlet>
            <servlet-name>springMVC</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <!--初始化加载SpringMVC的配置文件-->
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:springMvc-Servlet.xml</param-value>
            </init-param>
            <!-- 启动项目的时候就给你加载DispatcherServlet这个Servlet -->
            <load-on-startup>1</load-on-startup>
        </servlet>
        
          <!--servlet的映射文件-->
        <servlet-mapping>
            <servlet-name>springMVC</servlet-name>
            <!--/表示拦截所有的请求并交给SpringMVC控制器(DispatcherServlet)处理-->
            <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

    编写SpringMVC的配置文件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"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd">
    
        <context:component-scan base-package="zte.controller"></context:component-scan>
    
        <!--添加处理映射器-->
        <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
        
        <!--添加视图解析器-->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
            <!--前缀-->
            <property name="prefix" value="/WEB-INF/jsp/"></property>
            <!--后缀-->
            <property name="suffix" value=".jsp"></property>
        </bean>
    
    </beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    接着写要跳转的jsp页面,显示ModelandView存放的数据,因为我们在Controller 里面setViewName是hello,所以我们在
    /WEB-INF/jsp/下创建一个hello.jsp

    当在地址栏输入/hello的时候,就会调用handleRequest方法,返回一个/WEB-INF/jsp/hello.jsp的页面

    hello.jsp:

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>MVC</title>
    </head>
    <body>
    ${msg}
    </body>
    </html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    配置TomCat:
    在这里插入图片描述
    遇到404解决办法:

    1. 查看控制台输出,是否缺少jar包
    2. 在IDEA的项目发布中,添加lib依赖
    3. 重启即可解决

    这就是实现Controller 接口的方法,但我们实际业务中并不会这样去写,因为太复杂了,要写的太多,所以我们来看一个注解版的SpringMVC。


    三、使用注解实现SpringMVC

    首先还是在Maven的pom.xml文件内添加依赖,主要是Spring框架核心库、Spring MVC、servlet , JSTL。

    <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>5.3.21</version>
            </dependency>
    
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-web</artifactId>
                <version>5.3.21</version>
            </dependency>
    
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.11</version>
            </dependency>
            
            <!--Servlet-->
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>servlet-api</artifactId>
                <version>2.5</version>
            </dependency>
           
            <!-- tomcat 9.x jstl依赖  换这个依赖到tomcat10的时候需要删除编译缓存,并且重新编译有效-->
            <dependency>
                <groupId>javax.servlet.jsp.jstl</groupId>
                <artifactId>jstl-api</artifactId>
                <version>1.2</version>
            </dependency>
    
            <!-- JSTL标签标准库jar包 -->
            <dependency>
                <groupId>org.apache.taglibs</groupId>
                <artifactId>taglibs-standard-spec</artifactId>
                <version>1.2.5</version>
            </dependency>
            <!-- 上一个坐标所需的依赖,两个一起的 -->
            <dependency>
                <groupId>org.apache.taglibs</groupId>
                <artifactId>taglibs-standard-impl</artifactId>
                <version>1.2.5</version>
            </dependency>
            <dependency>
                <groupId>org.example</groupId>
                <artifactId>demo01</artifactId>
                <version>1.0-SNAPSHOT</version>
            </dependency>
    
    • 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

    然后还是先配置配置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">
        <!--1.注册DispatcherServlet-->
        <servlet>
            <servlet-name>springMVC</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <!--初始化加载SpringMVC的配置文件-->
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:springMvc-Servlet.xml</param-value>
            </init-param>
            <!-- 启动项目的时候就给你加载DispatcherServlet这个Servlet -->
            <load-on-startup>1</load-on-startup>
        </servlet>
    
        <servlet-mapping>
            <servlet-name>springMVC</servlet-name>
            <!--/表示拦截所有的请求并交给SpringMVC控制器(DispatcherServlet)处理-->
            <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

    然后添加Spring MVC配置文件:

    <?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"
           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">
        
        <!-- 自动扫描包,让指定包下的注解生效,由IOC容器统一管理 -->
        <context:component-scan base-package="zte.controller"/>
        
        <!--添加视图解析器-->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <!--前缀-->
            <property name="prefix" value="/WEB-INF/jsp/"></property>
            <!--后缀-->
            <property name="suffix" value=".jsp"></property>
        </bean>
    
    </beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    HelloMVC控制层:

    @Controller
    @RequestMapping("/user")
    public class HelloMVC {
    
        @RequestMapping("/hello")
        public String helloMVC(Model model){
            model.addAttribute("msg","你好mvc,我是通过注解方式来访问你的!");
    
            //这里也是相当于返回 //web-inf/jsp/hello.jsp
            return "hello";
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    这里关于一些注解作用的解释:

    1. @Controller 为了让Spring IOC容器初始化时自动扫描到
    2. @RequestMapping 为了映射请求路径,这里因为类与方法上都有映射所以访问时应该是/user/hello
    3. 方法中声明Model类型的参数是为了把Action中的数据带到视图中
    4. 还有很多很多注解,大家可以继续学习其他的注解

    视图层就不写了,还是用上面的hello.jsp即可

    配置TomCat,输入URL测试:

    在这里插入图片描述

    如果遇到404问题还是可以参考上面写的解决方法,依次排除即可。

    通过以上的步骤,用两种方式实现了简单的SpringMVC的搭建,SpringMVC代码清新简洁,大幅度提升工作效率,内部组件化程度高,可插拔式组件即插即用,想要什么功能提供配置文件即可性能卓越,尤其适合现代大型或超大型项目。


    注意:

    使用springMVC必须配置的三大块:

    处理器映射器:HandlerMapping
    处理器适配器:HandlerAdapter
    视图解析器:View Resolver


    四、总结

    最后,总结一下使用SpringMVC的步骤:

    1. 新建一个web项目(Maven)
    2. 在pom.xml里面导入需要的依赖
    3. 配置web.xml , 注册DispatcherServlet
    4. 配置SpringMVC配置文件,文件名要与web.xml里param-value里一致
    5. 创建Controller类,推荐使用注解法
    6. 根据控制类方法的返回编写视图,放到/WEB-INF/jsp下(根据自己的情况创建)
    7. 配置TomCat,输入URL测试

    在这里插入图片描述

  • 相关阅读:
    三、虚拟机的迁移和删除
    robots.txt漏洞
    JS逆向之TouTiao signature
    第五章:最新版零基础学习 PYTHON 教程—Python 字符串操作指南(第七节 - Python 中的字符串模板类)
    MYSQL 事务、事务隔离级别和MVCC,幻读
    npm常见相关命令
    SQL 的执行流程是什么样的
    华为云云耀云服务器L实例评测|基于L实例使用Docker部署MySQL服务并连接MySQL—phpMyAdmin管理工具
    Java成员方法的声明和调用
    ALevel课程组合建议
  • 原文地址:https://blog.csdn.net/m0_57310550/article/details/125433304