SpringMvc的可传递参数,参数绑定,json,@ResponseBody ,对象嵌套接收,数组,List

相关链接

 SpringMvc 注解详解 

请求参数绑定

一、默认支持ServletAPI

HttpServlet 对象,主要包括HttpServletRequest 、HttpServletResponse 和HttpSession 对象

@RequestMapping("requestWried")
public void requestWried(HttpServletResponse response, HttpServletRequest request, HttpSession session){
   System.out.println(response);
   System.out.println(request);
   System.out.println(session);
}

二、绑定简单的数据类型

  前提:
  1.传入的参数类型要匹配
  2.参数的名称,必须要与方法的参数列表的名称保持一致

@RequestMapping("simpleDataBindOne")
public ModelAndView simpleDataBindOne(Integer id, String isAdmin, String type, ModelAndView modelAndView) {...}

  前端

<a href="${pageContext.request.contextPath}/requestParam/simpleDataBindOne.action?id=1&isAdmin=1&type=admin">

三、@RequestParam 参数绑定注解

主要是给参数名不同的参数进行相应绑定

@RequestMapping("simpleDataBindTwo")
public ModelAndView simpleDataBindTwo(@RequestParam("ids") Integer id, ModelAndView modelAndView) {...}
<a href="${pageContext.request.contextPath}/requestParam/simpleDataBindTwo.action?ids=123">

四、绑定Pojo对象

Pojo类

public class User {
    private Integer id;
    private String username;
    private String sex;
    private Date birthday;
    //toString\getter\setter
}

请求参数绑定,将请求参数绑定到对象中
  要求: 请求参数名称在User对象中存在,则SpringMVC会自动将参数设置给对象,并且请求参数名和实体名相同即可

@RequestMapping("dataBindPojo")
public ModelAndView dataBindPojo(User user, ModelAndView modelAndView) {...}
<a href="${pageContext.request.contextPath}/requestParam/dataBindPojo.action?id=3&username=波波&sex=女">

五、绑定pojo的包装类对象UserVo

Pojo类

public class UserVo {
    private User user;
    private List<User> userList;
    private Map<String,User> userMap;
    // toString,getter,setter
}

分别对实体中的User对象,List集合,Map集合赋值

@RequestMapping("dataBindPojoVoList")
public ModelAndView dataBindPojoVoList(UserVo userVo, ModelAndView modelAndView) {...}

封装到User对象中

<a href="${pageContext.request.contextPath}/requestParam/dataBindPojoVo.action?user.id=1&user.username=柳岩&user.sex=女">测试</a>

封装到List对象中

<form action="${pageContext.request.contextPath}/requestParam/dataBindPojoVoList.action" method="post">
    用户【1】:
    用户id : <input type="text" name="userList[0].id"/><br>
    用户名称 : <input type="text" name="userList[0].username"/><br>
    用户性别 : <input type="text" name="userList[0].sex"/><br>
    用户【2】:
    用户id : <input type="text" name="userList[1].id"/><br>
    用户名称 : <input type="text" name="userList[1].username"/><br>
    用户性别 : <input type="text" name="userList[1].sex"/><br>
    <input type="submit" value="提交">
</form>

封装到Map对象中

<form action="${pageContext.request.contextPath}/requestParam/dataBindPojoVoMap.action" method="post">
    用户【1】:
    用户id : <input type="text" name="userMap['k1'].id"/><br>
    用户名称 : <input type="text" name="userMap['k1'].username"/><br>
    用户性别 : <input type="text" name="userMap['k1'].sex"/><br>
    用户【2】:
    用户id : <input type="text" name="userMap['k2'].id"/><br>
    用户名称 : <input type="text" name="userMap['k2'].username"/><br>
    用户性别 : <input type="text" name="userMap['k2'].sex"/><br>
    <input type="submit" value="提交">
</form>

六、通过@PathVariable获取路径中的参数

当使用@RequestMapping URI template 样式映射时, 即 someUrl/{paramId}, 这时的paramId可通过 @Pathvariable注解绑定它传过来的值到方法的参数上。

示例代码:

@Controller  
@RequestMapping("/owners/{ownerId}")  
public class RelativePathUriTemplateController {  
  
  @RequestMapping("/pets/{petId}")  
  public void findPet(@PathVariable String ownerId, @PathVariable String petId, Model model) {      
    // implementation omitted   
  }  
} 

上面代码把URI template 中变量 ownerId的值和petId的值,绑定到方法的参数上。若方法参数名称和需要绑定的uri template中变量名称不一致,需要在@PathVariable("name")指定uri template中的名称。

@Controller
@RequestMapping ( "/test/{variable1}" )
public class MyController {

    @RequestMapping ( "/showView/{variable2}" )
    public ModelAndView showView( @PathVariable String variable1, @PathVariable ( "variable2" ) int variable2) {
       ModelAndView modelAndView = new ModelAndView();
       modelAndView.setViewName( "viewName" );
       modelAndView.addObject( " 需要放到 model 中的属性名称 " , " 对应的属性值,它是一个对象 " );
       return modelAndView;
    }
} 

URI 模板就是在URI 中给定一个变量,然后在映射的时候动态的给该变量赋值。如URI 模板http://localhost/app/{variable1}/index.html ,这个模板里面包含一个变量variable1 ,那么当我们请求http://localhost/app/hello/index.html 的时候,该URL 就跟模板相匹配,只是把模板中的variable1 用hello 来取代。这个变量在SpringMVC 中是使用@PathVariable 来标记的。在SpringMVC 中,我们可以使用@PathVariable 来标记一个Controller 的处理方法参数,表示该参数的值将使用URI 模板中对应的变量的值来赋值。

代码中我们定义了两个URI 变量,一个是控制器类上的variable1 ,一个是showView 方法上的variable2 ,然后在showView 方法的参数里面使用@PathVariable 标记使用了这两个变量。所以当我们使用/test/hello/showView/2.do 来请求的时候就可以访问到MyController 的showView 方法,这个时候variable1 就被赋予值hello ,variable2 就被赋予值2 ,然后我们在showView 方法参数里面标注了参数variable1 和variable2 是来自访问路径的path 变量,这样方法参数variable1 和variable2 就被分别赋予hello 和2 。方法参数variable1 是定义为String 类型,variable2 是定义为int 类型,像这种简单类型在进行赋值的时候Spring 是会帮我们自动转换的。

七、RequestBody 的 接收请求体中json对象并映射到对象,主要获取请求体重的json或xml

1、@RequestBody 主要用来接收前端传递给后端的 json 格式的数据的(请求体中的数据的)

2、GET 方式无请求体,所以 @RequestBody 接收数据时,前端必须是 POST 方式进行提交,然后给页面的数据默认也是 json

3、同一个方法中,@RequestBody 与 @RequestParam() 可以同时使用,前者最多只能有一个,后者可以有多个,

4、@RequestBody 接收的是请求体里面的数据

使用ajax发送请求

<a href="javascript:void(0)" id="id2">ajaxName</a>
$("#id2").click(function () {
        //JavaScript的对象
        let user = {name: "迪丽热巴", age: "女", birthday: "2001-01-09"};
        //变为json的字符串,
        let userStr = JSON.stringify(user);
        //contentType
        //类型:String 默认值: "application/x-www-form-urlencoded"。发送信息至服务器时内容编码类型。
        //dataType 设置接收参数类型
        console.log(userStr)
      $.ajax({
            url: "${pageContext.request.contextPath}/ajaxName",
            type: "post",
            data: userStr,
            dataType: "json",
            contentType: "application/json;charset=UTF-8",
            success: function (response) {
                // alert(response);
                console.log(response);
          },
            error: function (error) {
                // alert(error);
          }
     });
});

这里有两个步骤需要注意,第一个就是将js的json对象转成了json数组,第二个就是添加了  contentType: "application/json;charset=UTF-8",这两个是使用@RequestBody的必须设置。

只有application/json; 和 application/xml; 才是请求体中的数据。

 java代码

@RequestMapping(value = "ajaxReqTest", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, method = RequestMethod.POST)
@ResponseBody
public User ajaxReq(@RequestBody User user) {
    System.out.println(user);
    return user;
}

@ResponseBody 是控制返回的,如果导入了jackson包那么可以根据返回类型进行映射,如返回对象或集合那么就映射为json。

补充一点,如果导入了jackson包,那么@RequestBody 在映射参数时也会自动适配,比如 前端传一个String 的日期 “2019-01-01”,后端的对象中是一个Date类型,那么可以自动转换。

下面展示 @ResponseBody  注解除了映射对象之外的其他情况

7.1 直接接受字符串

$.ajax({
   ...
     data: "迪丽热巴",
     contentType: "application/json;charset=UTF-8",
   ...
});
@RequestMapping("oneString")
@ResponseBody
public String oneString(@RequestBody String name){
      System.out.println(name);
      return name;
}

7.2 接收字符串数组 映射到数组或List

$.ajax({
  ... data: JSON.stringify(["q", "w", "e"]), contentType: "application/json;charset=UTF-8",
...
});
//接受数组或list
@RequestMapping("toArrString")
@ResponseBody
public List<String> toArrString(@RequestBody List<String> str) {
   System.out.println(str);
   return str;
}
@RequestMapping("toArrString")
@ResponseBody
public String[] toArrString(@RequestBody String[] str) {
   System.out.println(str);
   return str;
}

7.3 对象数组

$.ajax({
  ...
   data: JSON.stringify([{name: "111", age: "111", birthday: "2019-01-01"},{name: "222", age: "222", birthday: "2019-01-01"}]),
   contentType: "application/json;charset=UTF-8",
  ...
});
//接收User数组
@RequestMapping("toArrPojo")
@ResponseBody
public List<User> toArrPojo(@RequestBody List<User> users){
   users.forEach(System.out::println);
   return users;
}
@RequestMapping("toArrPojo")
@ResponseBody
public List<Map<String,String>> toArrPojo(@RequestBody List<Map<String,String>> users){
   users.forEach(System.out::println);
   return users;
}

7.4 复杂情况 嵌套 json嵌套对象和数组

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Complex {
    private String name;
    private User user;
    private List<User> arrStr;
    private Map<String, Integer> mapStr;
}
$.ajax({
    ...
data: JSON.stringify({
  name: "111",
  user: {name: "222", age: "222", birthday: "2019-01-01"},
  arrStr: [
    {name: "333", age: "333", birthday: "2019-01-01"},
    {name: "444", age: "444", birthday: "2019-01-01"}
  ],
    mapStr: {num1: 1, num2: 2}
  }),
      contentType: "application/json;charset=UTF-8",
    ....
});
@RequestMapping("toComplex")
@ResponseBody
public Complex toComplex(@RequestBody Complex complex) {
   System.out.println(complex);
   return complex;
}
posted @ 2020-09-12 17:50  一半人生  阅读(2392)  评论(0编辑  收藏  举报