web参数传递方式

阅读更多

方法一:

方法参数名即为请求参数名

  @RequestMapping(value = "/test/query1", method = RequestMethod.GET)

  public String testQuery1(String username, String password) {

    System.out.println("username=" + username + ", password=" + password);

    return "username=" + username + ", password=" + password;

 

  }

 方法二:

从HttpServletRequest中提取参数

  @RequestMapping(value = "/test/query2", method = RequestMethod.GET)

  public String testQuery2(HttpServletRequest request) {

    String username = request.getParameter("username");

    String password = request.getParameter("password");

    System.out.println("username=" + username + ", password=" + password);

    return "username=" + username + ", password=" + password;

 

  }

方法三:
方法参数名和请求参数名可以不一样,通过@RequestParam注解来绑定参数

  @RequestMapping(value = "/test/query3", method = RequestMethod.GET)

  public String testQuery3(@RequestParam("username") String un, @RequestParam("password") String pw) {

    System.out.println("username=" + un + ", password=" + pw);

    return "username=" + un + ", password=" + pw;

 

  }

方法四:
创建一个实体类对象作为参数承载体,spring会根据参数名称自动将参数绑定到实体类对象的属性上

  @RequestMapping(value = "/test/query4", method = RequestMethod.GET)

  public String testQuery4(User user) {

    String username = user.getUsername();

    String password = user.getPassword();

    System.out.println("username=" + username + ", password=" + password);

    return "username=" + username + ", password=" + password;

 

  }

方法五:

json格式。需要添加请求头:Content-Type: application/json;charset=UTF-8

5.1实体类,将json对象解析成实力类,需要添加RequestBody注解

  @RequestMapping(value = "/test/json1", method = RequestMethod.POST)

  public String testJson1(@RequestBody User user) {

    String username = user.getUsername();

    String password = user.getPassword();

    System.out.println("username=" + username + ", password=" + password);

    return "username=" + username + ", password=" + password;

 

  }

5.2通过JSONObject 接收参数

  @RequestMapping(value = "/test/json2", method = RequestMethod.POST)

  public String testJson2(@RequestBody JSONObject json) {

    String username = json.getString("username");

    String password = json.getString("password");

    System.out.println("username=" + username + ", password=" + password);

    return "username=" + username + ", password=" + password;

 

  }

5.3通过map接收参数

  @RequestMapping(value = "/test/json3", method = RequestMethod.POST)

  public String testJson3(@RequestBody Map userMap) {

    String username = userMap.get("username");

    String password = userMap.get("password");

    System.out.println("username=" + username + ", password=" + password);

    return "username=" + username + ", password=" + password;

 

  }

方式六:

路径参数;请求参数为url中的一部分,格式为:url/参数1/参数2...需要用到@PathVariable注解

@RequestMapping(value = "/test/url/{username}/{password}", method = RequestMethod.GET)

  public String testUrl(@PathVariable String username, @PathVariable String password) {

    System.out.println("username=" + username + ", password=" + password);

    return "username=" + username + ", password=" + password;

  }

总结:通过上面方式传递参数实际上就是通过名称映射填充数据的,也就是说参数名称相同就会接收到对应的参数值,这里可能需要一些额外的转换注解,@PathVariable,@RequestBody,@RequestParam等,

这里在补充说明一下参数类型,常见的就是时间参数的传递了,前台通常传递的都是字符串类型的时间,格式有多种了,可以自定义,当后台同样适用字符串接收时,是能够获取到参数的,但是如何后台定义的参数类型为Date类型时,就不会接收到数据,这是因为没有做时间类型的转换,通常使用simpledateformat进行转换,不过得先使用String接收到参数,后再转换,可有时候,比如说用DTO接收数据时,就需要另一种方式就是时间转换注解的形式了@DateTimeFormat(pattern="yyyy-MM-dd")

你可能感兴趣的:(web,spring,DateTimeFormat)