• Spring MVC-从零开始-@RequestMapping结合@RequestParam (从HTTP键值对中取值,作用于函数参数)


    1、@RequestParam 注解使用的时候可以有一个值,也可以没有值;如果请求参数和处理方法参数的名称一样的话,@RequestParam 注解的 value 这个参数就可省掉了;@RequestParam 注解的 required 这个参数定义了参数值是否是必须要传的。

    package com.jt;
    
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    @Controller
    @RequestMapping(value="/FirstControl")
    public class HelloControl {
        @RequestMapping(value={"/testParam1"})
        @ResponseBody
        public String opt1(@RequestParam String name){
            return "testParam1:"+name;
        }
        
        @RequestMapping(value="/testParam2")
        @ResponseBody
        public String opt2(@RequestParam(value="myname") String name){
            return "testParam2:"+name;
        }
        
        @RequestMapping(value="/testParam3")
        @ResponseBody
        public String opt3(@RequestParam(value="myname",defaultValue="jt") String name){
            return "testParam3:"+name;
        }
        
        @RequestMapping(value="/testParam4")
        @ResponseBody
        public String opt4(@RequestParam(value="myname",required=false) String name){
            return "testParam4"+name;
        }
    }

    效果1:url参数名称与函数参数名称一致

    效果2:url参数名称与函数参数名称不一致

     效果3:为url添加参数默认值

     

     效果4:设置url参数非必填


    效果5:url参数名称与函数参数名称不一致

  • 相关阅读:
    关东升的《从零開始学Swift》即将出版
    input子系统驱动学习之中的一个
    linux 搭建https server (apache)
    Http协议具体解释
    三天学会HTML5——SVG和Canvas的使用
    阿里2016实习offer五面经验与总结
    你所须要知道的项目管理知识
    【hadoop2.6.0】用C++ 编写mapreduce
    【leetcode】 Letter Combinations of a Phone Number(middle)
    【杂感】目标跟踪的用途
  • 原文地址:https://www.cnblogs.com/jiangtao1218/p/8586198.html
Copyright © 2020-2023  润新知