如何指定一个请求参数绑定到某个控制器方法参数而不是Spring MVC中的模型属性?

时间:2017-12-15 07:26:14

标签: spring spring-mvc servlets request

这是一个模型类:

public class Contract{
   private Integer id;
   private String code;
}

这是一个控制器方法:

@RequestMapping(value="/generate-contract", method=GET)
public JsonResult generateContract(Contract contract, String code) throws Exception{
    //TODO
    return JsonResult.ok(data);
}

如果我将参数id=1&code=DW23传递给方法,如何仅指定contract code属性存储值或仅指定方法参数{{1存储值code

现在,从我的实验中,他们都获得了价值。

1 个答案:

答案 0 :(得分:0)

You can use <?php setcookie('username', 'Test', time() + (86400 * 30), "/"); ?> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0-alpha1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.min.js"></script> <script> $(function(){ $('document').ready(function(){ alert($.cookie("username")); }); }); </script> to map the request parameters to java variables. It also does the type conversion.

@RequestParam

The parameters can be made optional by specifying the @RequestMapping(value="/generate-contract", method=GET) public JsonResult generateContract(@RequestParam int id, @RequestParam String code) throws Exception{ //TODO return JsonResult.ok(data); } attribute of required. e.g. @RequestParam .With this, you will have to populate the properties of Contract manually by creating a new instance.