• 01【SpringMVC快速入门】



    01【SpringMVC快速入门】

    一、SpringMVC快速入门

    1.1 SpringMVC概述

    1.1.1 SpringMVC是什么

    SpringMVC是Spring为表现层提供的基于 MVC 设计理念的优秀的 Web 框架,SpringMVC 已经成为目前最主流的 MVC 框架之一, 从 Spring3.0 的发布, 就已全面超越 Struts2,成为最优秀的 MVC 框架。它通过一套注解,让一个简单的 Java 类成为处理请求的控制器,而无须实现任何接口。同时它还支持RESTful 编程风格的请求。

    • SpringMVC在三层架构中的位置:

    在这里插入图片描述

    1.2 SpringMVC环境搭建

    1.2.1 创建web项目

    创建一个web项目,引入依赖

    在这里插入图片描述

    1.2.2 Maven依赖:

    <dependencies>
        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webmvcartifactId>
            <version>5.2.9.RELEASEversion>
        dependency>
    
        <dependency>
            <groupId>org.apache.tomcatgroupId>
            <artifactId>tomcat-apiartifactId>
            <version>8.5.71version>
        dependency>
    
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <version>1.18.18version>
        dependency>
    dependencies>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    webmvc依赖中包含有spring和springmvc所依赖的jar;

    在这里插入图片描述

    1.2.3 配置web.xml

    
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns="http://java.sun.com/xml/ns/javaee"
             xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
             version="2.5">
    
        
        <servlet>
            <servlet-name>springmvcservlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
        servlet>
        <servlet-mapping>
            <servlet-name>springmvcservlet-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

    1.2.4 SpringMVC配置

    
    <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.dfbz"/>
    
        
        <mvc:annotation-driven/>
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 配置文件说明:

    注意:默认的SpringMVC配置文件放在WEB-INF下,名为{servlet-name}-servlet.xml

    {servlet-name}指的是,核心控制器配置的名字

    如:dispatcherServlet-servlet.xml

    当请求被springmvc处理时,springmvc会去默认路径下加载xxxx-servlet.xml核心配置文件

    但是我们在开发中一般都是把配置文件写在classes下的,我们可以在web.xml中设置springmvc配置文件的路径

    
    <servlet>
        <servlet-name>springmvcservlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
        
        <init-param>
            <param-name>contextConfigLocationparam-name>
            <param-value>classpath:springmvc.xmlparam-value>
        init-param>
    servlet>
    <servlet-mapping>
        <servlet-name>springmvcservlet-name>
        
        <url-pattern>/url-pattern>
    servlet-mapping>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    1.2.5 编写Controller

    package com.dfbz.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.servlet.ModelAndView;
    
    @Controller
    public class HelloController{
    
        // 代表此方法的访问路径为/hello.form
        @RequestMapping("/hello.form")
        public ModelAndView hello(){
    
            ModelAndView mav=new ModelAndView();
            mav.addObject("msg","Hello SpringMVC!");
            mav.setViewName("/hello.jsp");
    
            return mav;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    1.2.6 编写视图页面

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    
    
        Title
    
    
    <%
        System.out.println("JSP脚本渲染了!");
    %>
    

    ${msg},欢迎您!

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    访问:http://localhost:8080/hello.form

    在这里插入图片描述
    查看控制台:

    在这里插入图片描述

  • 相关阅读:
    Go 语言 设计模式-工厂模式
    【面试普通人VS高手系列】HashMap是怎么解决哈希冲突的?
    《opencv学习笔记》-- 形态学滤波:形态学梯度、开运算、闭运算、顶帽、黑帽
    C语言中,“>>=”;“<<=”;“&=”;“|=”分别代表什么
    Vue怎么通过JSX动态渲染组件
    LeetCode刷题复盘笔记——93. 复原 IP 地址(一文搞懂回溯解决把一长串数字字符串转换成IP地址问题)
    slurm是什么,怎么用? For slurm和For Pytorch有什么区别和联系?
    LabVIEW创建类 2
    MFC 学习笔记
    了解ASEMI代理英飞凌TLE6208-6G其功能和应用的综合指南
  • 原文地址:https://blog.csdn.net/Bb15070047748/article/details/128104852