⌛ 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" ;
}
相关推荐
用户4099322502121 分钟前
如何用 Git Hook 和 CI 流水线为 FastAPI 项目保驾护航?
后端·ai编程·trae
召摇5 分钟前
Java 21到25的核心API演进总结
java·后端
buddy_red12 分钟前
Knox工具调用功能测试
人工智能·后端·程序员
知其然亦知其所以然16 分钟前
SpringAI 玩转 OCI GenAI:这次我们聊聊 Cohere 聊天模型
java·后端·spring
种子q_q20 分钟前
Redis的三种典型的 “缓存失效” 问题
后端·面试
金銀銅鐵20 分钟前
[Java] 观察 CompactStrings 选项的影响
java·后端
程序猿二饭21 分钟前
Spring Boot 项目启动报错:MongoSocketOpenException 连接被拒绝排查日记
后端
UP22 分钟前
【C++基础】内存管理——malloc/free和new/delete之间的盘根错节
后端
齐穗穗25 分钟前
springboot集成websocket
spring boot·后端·websocket
玉衡子28 分钟前
四、索引优化实战
java·后端