URL Mapping

添加 Controller 用于接受 get, post 请求

@Controller
@RequestMapping("/um") //若写到 方法上面, 则说明不区分 get, post 请求, 不推荐
public class URLMappingController {
    
    @GetMapping("/g") // "http://localhost:8080/um/g"
    @ResponseBody
    public String getMapping() {//使用 @RequestParam 对 下划线命名 和 驼峰命名进行映射
        return "<h1> This is get method </h1>";
    }

    @PostMapping("/p") // "http://localhost:8080/um/p"
    @ResponseBody
    public String postMapping(){
        return "<h1> This is post method </h1>";
    }
    
}

测试 get 请求

点击 IDEA 右上角三角形 启动工程, 浏览器输入 http://localhost:8080/um/g 即可看到返回值。

测试 post 请求

在 src\main\webapp 目录下 新增 request_mapping.html 文件

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <!-- 一定使用 http://localhost:8080/ 打开这个html文件-->
    <h1> Post request demo !</h1>
    <form action="/um/p" method="post">
        <input type="submit" value="提交" >
    </form>
</body>
</html>

点击 IDEA 右上角三角形 启动工程, 浏览器输入 http://localhost:8080/request_mapping.html 点击 提交 按钮 即可跳转到 http://localhost:8080/um/p 并看到返回值。

Last updated