• 【SpringMVC】基础部分


    SpringMvc

    • Spring MVC 是Spring提供的一个实现了Web MVC设计模式的轻量级Web框架。

    • MVC(Model View Controller),一种用于设计创建Web应用程序表现层的模式

      • Model(模型):数据模型,用于封装数据
      • View(视图):页面视图,用于展示数据
      • Controller(Handle 处理器):处理用户交互的调度器,用于根据用户需求处理程序逻辑

    三层架构

    • 表现层:负责数据展示

    • 业务层:负责业务处理

    • 数据层:负责数据操作

    SpringMVC项目步骤

    1 新建maven的web项目
    在这里插入图片描述
    2 导入maven依赖

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.1.0</version>
      <scope>provided</scope>
    </dependency>
    <!--jsp坐标-->
    <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>jsp-api</artifactId>
      <version>2.1</version>
      <scope>provided</scope>
    </dependency>
    <!--spring的坐标-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.1.9.RELEASE</version>
    </dependency>
    <!--spring web的坐标-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>5.1.9.RELEASE</version>
    </dependency>
    <!--springmvc的坐标-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.1.9.RELEASE</version>
    </dependency>
    <dependency>
          <groupId>org.projectlombok</groupId>
          <artifactId>lombok</artifactId>
          <version>1.18.24</version>
        </dependency>
        <dependency>
          <groupId>jstl</groupId>
          <artifactId>jstl</artifactId>
          <version>1.2</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

    3 创建controller
    在这里插入图片描述

    @Controller
    public class UserController {
        @RequestMapping("/save")
        public String say(){
            System.out.println("你好");
            return "a.jsp";
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    4 创建spring-mvc.xml配置文件(本质就是spring的配置件)

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:context="http://www.springframework.org/schema/context"
           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
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context.xsd
        ">
    
        <context:component-scan base-package="com.xinzhi"/>
    </beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    5 web.xml中配置前端控制器

    <servlet>
      <servlet-name>DispatcherServlet</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:spring-mvc.xml</param-value>
      </init-param>
    </servlet>
    <servlet-mapping>
      <servlet-name>DispatcherServlet</servlet-name>
      <url-pattern>/</url-pattern>
    </servlet-mapping>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    6 新建a.jsp文件

    <%--
      Created by IntelliJ IDEA.
      User: 86166
      Date: 2023/9/16
      Time: 11:54
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <%@ page isELIgnored="false"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
    <html>
    <head>
        <title>Title</title>
    
    </head>
    <body>
    <p>p标签</p>
    ${name}
    </body>
    </html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    7 配置tomcat
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    测试

    参数绑定代码

    //    默认类型:
    
        @RequestMapping("/test1")
        public ModelAndView tset1(HttpServletRequest request,ModelAndView modelAndView){
            String name = request.getParameter("name");
            System.out.println(name);
            modelAndView.setViewName("a.jsp");
            modelAndView.addObject("name",name);
            return modelAndView;
        }
    //    简单类型:
        @RequestMapping("/test2")
        public String test2(String name,String age){
            System.out.println(name);
            System.out.println(age);
            return "a.jsp";
        }
    //# 对象类型
        @RequestMapping("/test3")
        public String test3(User user){
            System.out.println(user);
            return "a.jsp";
        }
    //# 数组
        @RequestMapping("/test4")
        public String test4(String[] name){
            System.out.println(Arrays.toString(name));
            return "a.jsp";
        }
    
    //# list类型
        @RequestMapping("/test5")
        public String test5(@RequestParam("name") List<String> name){
            System.out.println(name);
            return "a.jsp";
        }
    //类型转换
        @RequestMapping("/test6")
        @ResponseBody
        public String test6(Date time){
            System.out.println(time);
            return "bbb";
        }
    
    • 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
    • 自定义数据绑定

      • 定义转换器
      public class MyDateConverter implements Converter {
      
          @Override
          public Date convert(String s) {
              DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
              Date date = null;
              try {
                  date = df.parse(s);
              } catch (ParseException e) {
                  e.printStackTrace();
              }
              return date;
          }
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 注解驱动,使转换器起作用
      
      
      
      
          
          
              
              
                  
                  
              
          
      
      
      
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
  • 相关阅读:
    【API篇】六、Flink输出算子Sink
    爬山算法介绍
    Python环境搭建之OpenCV
    C++-ffmpeg-2-1-RGB-YUV内存中的存储+SDL2对其渲染
    pycharm中做web应用(13)基于Django和mysql 做用户登录验证3
    Pandas多级索引数据处理及fillna()填充方式
    2022.11 Windows系统下配置深度学习环境的一些记录
    C++里sscanf()与swscanf()的使用
    七天入门node.js(01)
    什么是SpringBoot自动配置
  • 原文地址:https://blog.csdn.net/m0_71500484/article/details/132922313