SpringMVC——参数传递

一、接收零散参数

  1.装配原则为传递参数名和方法接收参数名一致

  2.手动装配@RequestParam  name代表页面发送的参数名字  required代表参数是否必须传递  false代表可以不传递,默认为true defaultValue代表默认值

 public String formRequest(@RequestParam(name = "userCode",required = false,defaultValue = "王xx") String userCoder, @RequestParam(name = "userPwd",required = false)String userPwd, Model model){
        System.out.println(userCoder+"	"+userPwd);
        model.addAttribute("userCode",userCoder);
        return "welcome";
    }

二、接收对象参数

  普通属性传递:传递的对象参数和对象中的属性名保持一致

  域属性传递:传递参数为:域属性.属性名

  集合参数传递:集合名[下标].属性名

@RequestMapping(value = "/fourthRequest")
    public String fourthRequest(UserInfo info){
        System.out.println(info.toString());

        return "welcome";
    }

三、使用@PathVariable参数映射

  get请求时,如果需要传递数据,那么则不能使用以往方式?name=xxx&age=yy,但是现在要遵循restful风格,举例 xxx/yyy/zzz

  根据地址栏URL匹配拿值 使用@PathVariable(name=地址栏中的参数映射)

 @RequestMapping(value = "/restfulRequest/{b}/{d}")
    public String restfulRequest(@PathVariable(name = "b") String name,@PathVariable(name = "d") String age){
        System.out.println(name+"	"+age);
        return "welcome";
    }
原文地址:https://www.cnblogs.com/xiao-ran/p/11832894.html