zoukankan      html  css  js  c++  java
  • RestFul风格讲解

     

    实现:利用@PathVariable注解,通过占位符方式,让方法参数的值对应绑定到一个URL模板变量上

    @Controller
    public class RestfulController {
        @RequestMapping("/hello/{a}/{b}")
        public String restful(@PathVariable int a,@PathVariable int b, Model model){
            int res = a+b;
            model.addAttribute("msg","结果为:"+res);
            return "hello";
    
        }
    }

    扩展:

     

       注意:默认走到是GET--->相当与@GetMapping

          其他的还有,@PostMapping,@PutMapping,@DeleteMapping,使用相应的注解就能实现相应提交方式,

          最后达到,同一个url却能实现不同功能,或者说跳转到不同的页面

    package com.king.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.*;
    
    @Controller
    public class RestfulController {
    
        @GetMapping("hello/{a}/{b}")
        public String restful(@PathVariable int a,@PathVariable int b, Model model){
            System.out.println("成功了");
            int res = a+b;
            model.addAttribute("msg","结果为:"+res);
            return "hello";
    
        }
        @PostMapping("hello/{a}/{b}")
        public String restfull(@PathVariable int a,@PathVariable int b, Model model){
            System.out.println("成功了");
            int res = a+b;
            model.addAttribute("msg","结果为:"+res);
            return "hello";
        }
        @DeleteMapping("hello/{a}/{b}")
        public String restfulll(@PathVariable int a,@PathVariable int b, Model model){
            System.out.println("成功了");
            int res = a+b;
            model.addAttribute("msg","结果为:"+res);
            return "hello";
    
        }
    }

     restful风格优点:简洁,高效,安全(不会再url中暴漏参数 )

     

  • 相关阅读:
    oracle表解锁
    pl/sql 如何配置连接远程一个或多个数据库
    Hibernate通过自编写Sql修改
    Hibernate通过自编写sql查询
    java生成临时令牌和访问令牌
    java生成字母首位8位随机码
    [C] 创建目录_mkdir()函数
    [C] 判断目录 / 文件是否存在access()函数
    [C] getopt使用说明
    [C] Segmentation fault (core dumped)
  • 原文地址:https://www.cnblogs.com/CL-King/p/13945777.html
Copyright © 2011-2022 走看看