⌛ SpringMVC 入门学习

1.为什么需要学习SpringMVC

Spring Web MVC是基于Servlet API构建的原始Web框架,从一开始就包含在Spring Framework中。正式名称"Spring Web MVC"来自其源模块的名称( spring-webmvc ),但它通常被称为"Spring MVC"。

在控制层框架历经Strust、WebWork、Strust2等诸多产品的历代更迭之后,目前业界普遍选择了SpringMVC作为Java EE项目表述层开发的首选方案。之所以能做到这一点,是因为SpringMVC具备如下显著优势:

  • Spring 家族原生产品,与IOC容器等基础设施无缝对接
  • 表述层各细分领域需要解决的问题全方位覆盖 ,提供全面解决方案
  • 代码清新简洁,大幅度提升开发效率
  • 内部组件化程度高,可插拔式组件即插即用,想要什么功能配置相应组件即可
  • 性能卓著,尤其适合现代大型、超大型互联网项目要求

2. SpringMVC快速入门

  1. 添加jar
java 复制代码
<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>6.0.11</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>6.0.11</version>
    </dependency>
    <dependency>
        <groupId>jakarta.platform</groupId>
        <artifactId>jakarta.jakartaee-web-api</artifactId>
        <version>9.1.0</version>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.24</version>
    </dependency>
</dependencies>
  1. 创建web环境,带有web.xml

3) 在web.xml文件中配置中央控制器

java 复制代码
<?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-name>DispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>DispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>
  1. 新建Hello01Controller 其中需要注意两个注解:@Controller , @RequestMapping
java 复制代码
//1.演示的路径的精确匹配和模糊匹配

@Controller
public class Hello01Controller {

   @RequestMapping("/hello/h01")
   @ResponseBody
   public String h01(){
       System.out.println("Hello springmvc!");
       return "succ" ;
   }
}
  1. 配置tomcat
  1. 启动tomcat 成功显示页面

3.@RequestMapping详解

  1. 路径匹配 - 精确匹配 @RequestMapping("/hello/h01")

    http://localhost:8080/hello/h01

  2. 路径匹配 - 模糊匹配 - 单层

    @RequestMapping("/hello/*")

    http://localhost:8080/hello/a

    http://localhost:8080/hello/b

    http://localhost:8080/hello/c

  3. 路径匹配 - 模糊匹配 - 多层

    @RequestMapping("/hello/**")

    http://localhost:8080/hello/a/b/c/d

  4. @RequestMapping还可以写在类上

  5. @RequestMapping还可以指定请求方式

    @RequestMapping(value="/h02",method = {RequestMethod.GET,RequestMethod.DELETE})

  6. @RequestMapping可以和请求方式合并成新的注解:

    @DeleteMapping 等价于 @RequestMapping(method = RequestMethod.DELETE)

    @PutMapping 等价于 @RequestMapping(method = RequestMethod.PUT)

    @PostMapping 等价于 @RequestMapping(method = RequestMethod.POST)

    @GetMapping 等价于 @RequestMapping(method = RequestMethod.GET)

4. 获取请求参数(重点)

  1. 获取普通的请求参数(名称一致)

请求路径

http://localhost:8080/hello03/h01?uname=jim&hobby=football&hobby=basketball&hobby=pingpong&address=China

java 复制代码
@GetMapping("/h01")
public String h01(String uname , String[] hobby , String address){
    //String request.getParameter("uname")
    //String[] request.getParameterValues("hobby")
    System.out.println("uname = " + uname);
    System.out.println(Arrays.toString(hobby));
    System.out.println("address = " + address);

    return "succ" ;
}
  1. 获取普通的请求参数(名称不一致) - @RequestParam

请求路径

http://localhost:8080/hello03/h02?uname=jim&address=China

java 复制代码
@GetMapping("/h02")
public String h02(@RequestParam("uname") String username , String address){
   System.out.println("username = " + username);
   System.out.println("address = " + address);
   return "succ" ;
}
  1. 获取普通的请求数据,但是这些数据对应控制器方法中的对象

请求路径

http://localhost:8080/hello03/h03?uname=jim&address=China

java 复制代码
@GetMapping("/h03")
public String h03(User user){
    System.out.println("user = " + user);
    return "succ" ;
}
java 复制代码
@Data
public class User {
    private String uname ;
    private String address ;
}
  1. 获取普通的请求数据,一个名称对应多个值,使用集合而不是数组(一个名称对应多个值)

    • 对应一个名称对于多个值的情况,我们可以使用数组去接收

    • 如果使用的集合List,那么需要添加@RequestParam注解:

请求路径

http://localhost:8080/hello04/h01?uname=jim&hobby=football&hobby=basketball&hobby=pingpong&address=China

java 复制代码
@GetMapping("/h04")
public String h04(String uname ,@RequestParam List<String> hobby , String address){
    System.out.println("uname = " + uname);
    System.out.println("hobby = " + hobby);
    System.out.println("address = " + address);

    return "succ" ;
}
  1. 路径参数(重点)

①) 路径参数的格式如下:

http://localhost:8080/hello04/h01/jim/20/china

②) 在Controller中获取路径参数的值:

java 复制代码
@GetMapping("/h01/{uname}/{age}/{address}")
public String h01(@PathVariable Integer age ,@PathVariable String address , @PathVariable String uname){
    System.out.println("uname = " + uname);
    System.out.println("age = " + age);
    System.out.println("address = " + address);
    return "succ" ;
}
相关推荐
半夏知半秋12 小时前
rust学习-闭包
开发语言·笔记·后端·学习·rust
LucianaiB13 小时前
【保姆级教程】10分钟把手机变成AI Agent:自动刷课、回消息,学不会我“退网”!
后端
Mr -老鬼13 小时前
功能需求对前后端技术选型的横向建议
开发语言·前端·后端·前端框架
IT=>小脑虎13 小时前
Go语言零基础小白学习知识点【基础版详解】
开发语言·后端·学习·golang
Eric_见嘉14 小时前
NestJS 🧑‍🍳 厨子必修课(九):API 文档 Swagger
前端·后端·nestjs
a程序小傲14 小时前
小红书Java面试被问:TCC事务的悬挂、空回滚问题解决方案
java·开发语言·人工智能·后端·python·面试·职场和发展
短剑重铸之日14 小时前
《SpringBoot4.0初识》第五篇:实战代码
java·后端·spring·springboot4.0
heartbeat..14 小时前
Spring MVC 全面详解(Java 主流 Web 开发框架)
java·网络·spring·mvc·web
jump_jump14 小时前
SaaS 时代已死,SaaS 时代已来
前端·后端·架构
a努力。15 小时前
国家电网Java面试被问:最小生成树的Kruskal和Prim算法
java·后端·算法·postgresql·面试·linq