• @RequestParam与@PathVariable的区别


    @PathVariable绑定URI模板变量值

    @PathVariable是用来获得请求url中的动态参数的

    @PathVariable用于将请求URL中的模板变量映射到功能处理方法的参数上。//配置url和方法的一个关系@RequestMapping("item/{itemId}")

    /* @RequestMapping 来映射请求,也就是通过它来指定控制器可以处理哪些URL请求,类似于struts的action请求
    * @responsebody表示该方法的返回结果直接写入HTTP response body中
    *一般在异步获取数据时使用,在使用@RequestMapping后,返回值通常解析为跳转路径,加上@responsebody后返回结果不会被解析为跳转路径,而是直接写入HTTP response *body中。
    *比如异步获取json数据,加上@responsebody后,会直接返回json数据。*
    *@Pathvariable注解绑定它传过来的值到方法的参数上
    *用于将请求URL中的模板变量映射到功能处理方法的参数上,即取出uri模板中的变量作为参数
    */
    @ResponseBody
    public TbItem getItemById(@PathVariable Long itemId){

     1 @RequestMapping("/zyh/{type}")
     2   public String zyh(@PathVariable(value = "type") int type) throws UnsupportedEncodingException {
     3     String url = "http://wx.diyfintech.com/zyhMain/" + type;
     4     if (type != 1 && type != 2) {
     5       throw new IllegalArgumentException("参数错误");
     6     }
     7     String encodeUrl = URLEncoder.encode(url, "utf-8");
     8     String redirectUrl = MessageFormat.format(OAUTH_URL, WxConfig.zyhAppId, encodeUrl, "snsapi_userinfo", UUID.randomUUID().toString().replace("-", ""));
     9     return "redirect:" + redirectUrl;
    10   }

    在SpringMVC后台控制层获取参数的方式主要有两种:

    一种是request.getParameter("name"),另外一种是用注解@RequestParam直接获取

    这里主要讲这个注解 @RequestParam

    接下来我们看一下@RequestParam注解主要有哪些参数:

    value:参数名字,即入参的请求参数名字,如username表示请求的参数区中的名字为username的参数的值将传入;

    required:是否必须,默认是true,表示请求中一定要有相应的参数,否则将报404错误码;

    defaultValue:默认值,表示如果请求中没有同名参数时的默认值,例如:

    public List<EasyUITreeNode> getItemTreeNode(@RequestParam(value="id",defaultValue="0")long parentId)

     1 @Controller
     2 @RequestMapping("/wx")
     3 public class WxController {
     4 
     5     @Autowired
     6     private WxService wxService;
     7     private static final Log log= LogFactory.getLog(WxController.class);
     8 
     9     @RequestMapping(value = "/service",method = RequestMethod.GET)
    10     public void acceptWxValid(@RequestParam String signature, @RequestParam String timestamp, @RequestParam String nonce,
    11                               @RequestParam String echostr, HttpServletResponse response) throws IOException {
    12         PrintWriter out = response.getWriter();
    13         if (SignUtil.checkSignature(signature, timestamp, nonce)) {
    14             out.print(echostr);
    15         }else
    16             out.print("fail");
    17         out.flush();
    18         out.close();
    19     }

    参考:https://www.cnblogs.com/zlw-xf/p/8035215.html

    spring MVC中,两者的作用都是将request里的参数的值绑定到contorl里的方法参数里的,区别在于,URL写法不同。

    使用@RequestParam时,URL是这样的:http://host:port/path?参数名=参数值

    使用@PathVariable时,URL是这样的:http://host:port/path/参数值

     1     @RequestMapping(value="/user",method = RequestMethod.GET)  
     2        public @ResponseBody  
     3        User printUser(@RequestParam(value = "id", required = false, defaultValue = "0")  
     4        int id) {  
     5         User user = new User();  
     6            user = userService.getUserById(id);  
     7            return user;  
     8        }  
     9          
    10        @RequestMapping(value="/user/{id:\d+}",method = RequestMethod.GET)  
    11        public @ResponseBody  
    12        User printUser2(@PathVariable int id) {  
    13            User user = new User();  
    14            user = userService.getUserById(id);  
    15            return user;  
    16        }  

    参考:https://www.cnblogs.com/sharpest/p/9954942.html

  • 相关阅读:
    [.net 面向对象编程基础] (13) 重构
    [.net 面向对象编程基础] (12) 面向对象三大特性——多态
    [.net 面向对象编程基础] (11) 面向对象三大特性——继承
    [.net 面向对象编程基础] (10) 面向对象三大特性——封装
    [.net 面向对象编程基础] (9) 类的成员(字段、属性、方法)
    [.net 面向对象编程基础] (8) 类和类的实例
    [.net 面向对象编程基础] (7) 基础中的基础——修饰符
    反向代理和正向代理区别
    Vue的路由及静态路由和动态路由的区别
    一次性讲明白vue插槽slot
  • 原文地址:https://www.cnblogs.com/116970u/p/11213263.html
Copyright © 2020-2023  润新知