[学习笔记]springmvc-restfull风格

1 RestFull风格实例如下:
在这里插入图片描述传统URL传参风格是http://localhost:8080/add?a=1&b=2, restfull风格通过@PathVariable修饰参数以达到路径类型传参。

2 前端访问的路径一样时,可以通过不同的请求方式予以区别

//RestFull风格是访问 http://localhost:8080/add/1/2
    @RequestMapping(value = "/add/{a}/{b}",method = RequestMethod.GET)
    public String demotest1(@PathVariable int a, @PathVariable int b, Model model){
        int res = a + b;
        model.addAttribute("msg","结果为:"+res);
        return "test";
    }

或者这样写:

//    @PostMapping
//    @PutMapping
//    @DeleteMapping
    @GetMapping(value = "/add/{a}/{b}")
    public String demotest2(@PathVariable int a, @PathVariable int b, Model model){
        int res = a + b;
        model.addAttribute("msg","结果为:"+res);
        return "test";
    }

一些原生态的转发和重定向,可以欣赏一下:
在这里插入图片描述注意:下图中的return中的页面如果是在WEB-INF中,前面需要加上/WEB-INF/
在这里插入图片描述

发布了7 篇原创文章 · 获赞 0 · 访问量 104

猜你喜欢

转载自blog.csdn.net/szwdong_jeff/article/details/105556972