• spring mvc 数据绑定入门


    1:基本数据类型key 是必传的并且必须类型一致

    // http://localhost:8080/bind/baseType.do?xage=10
    @RequestMapping(value = "/baseType.do")
    @ResponseBody
    public String baseType(@RequestParam("xage") int age) {
    return "age:" + age;
    }


    2:包装数据类型key 是不是必须传入的

    / http://localhost:8080/bind/baseType2.do?age=10@RequestMapping(value = "/baseType2.do")
    @ResponseBody
    public String baseType2(Integer age) {
    return "age:" + age;
    }

    3:
    数组类型绑定
    http://localhost:8080/bind/array.do?name=Tom&name=Lucy
    @RequestMapping(value = "/array.do")
    @ResponseBody
    public String array(String[] name) {

    StringBuilder stringBuilder = new StringBuilder();

    for (String item : name) {
    stringBuilder.append(item).append(" ");
    }
    return stringBuilder.toString();
    }

    4:
    简单对象绑定

    public class Admin {

    private String name;

    private Integer age;
    }

    public class User {

    private String name;

    private Integer age;

    private ContactInfo contactInfo;

    }
    http://localhost:8080/bind/object.do?name=Tom&age=10
    http://localhost:8080/bind/object.do?name=Tom&age=10&contactInfo.phone=10086&contactInfo.address=1
    @RequestMapping(value = "/object.do")
    @ResponseBody
    public String object(User user, Admin admin) {

    return user.toString() + " " + admin.toString();
    }
    5:简单对象绑定扩展到不同对象的相同属性名
    http://localhost:8080/bind/object.do?user.name=Tom&
    user.age=10&contactInfo.phone=10086&
    contactInfo.address=1&admin.name=Lucy&admin.age=15

    添加如下bind
    @InitBinder("user")
    public void initUser(WebDataBinder binder) {
    binder.setFieldDefaultPrefix("user.");
    }

    @InitBinder("admin")
    public void initAdmin(WebDataBinder binder) {
    binder.setFieldDefaultPrefix("admin.");
    }

    6:List对象绑定

    public class UserListForm {
    private List<User> users;
    }


    http://127.0.0.1:8080/bind/list.do?users[0].name=Tom&users[1].name=Lucy
    http://127.0.0.1:8080/bind/list.do?users[0].name=Tom&users[20].name=Lucy

    @RequestMapping(value = "list.do")
    @ResponseBody
    public String list(UserListForm userListForm) {
    return "listsize:" + userListForm.getUsers().size() + userListForm.toString();
    }


    7:
    map对象绑定

    public class UserMapForm {

    private Map<String,User> users;
    }
    http://127.0.0.1:8080/bind/map.do?users['x'].name=Tom&users['x'].age=10&users['y'].name=Lucy

    @RequestMapping(value = "map.do")
    @ResponseBody
    public String map(UserMapForm userMapForm) {
    return userMapForm.toString();

    }

    8:Json类型数据绑定

    {
    "name":"Jim",
    "age":16,
    "contactInfo":{
    "address":"beijing",
    "phone":"10010"
    }
    }
    @RequestMapping(value = "json.do")
    @ResponseBody
    public String map(@RequestBody User user) {
    return user.toString();

    }
    需要引入
    <dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-mapper-asl</artifactId>
    <version>${org.codehaus.jackson.version}</version>
    </dependency>



  • 相关阅读:
    SSH免密登录
    要不要学AI
    俞军产品方法论-笔记
    数据产品经理:实战进阶-笔记
    java代码中引用了scala类,Maven打包编译时爆出找不到scala类的异常
    Flink unable to generate a JAAS configuration file
    开始学习首席AI架构师
    flink checkpoinnt失败
    程序员的三种发展方向
    每日站会
  • 原文地址:https://www.cnblogs.com/zyy1688/p/10276507.html
Copyright © 2020-2023  润新知