我正在尝试使用 @ModelAttribute 和 MediaType.APPLICATION_FORM_URLENCODED_VALUE 作为使用的数据类型将简单的 HTML 表单发布到 Spring RestController。我已经仔细检查了所有与我的请求 bean 匹配的表单字段。当请求进入映射方法时,所有请求 bean 字段都为空。@RestController@EnableWebMvcpublic class WebServiceController { @RequestMapping( value = "/test", method = RequestMethod.POST, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE) public ResponseEntity<?> post(@ModelAttribute FormBean request){ // request.getParam() == null return ResponseEntity.ok().build(); }}public class FormBean { private String param1; public String getParam1() { return param1; } public void setParam1(String param1) { this.param1 = param1; }}<html> <head> <title>Test Form</title> </head> <body> <form action="/test" method="POST"> <input type="text" id="param1"> <button type="submit">Submit</button> </form> </body></html>
1 回答

ITMISS
TA贡献1871条经验 获得超8个赞
name
您的 HTML 输入中缺少该属性,id
发布 HTML 表单时该属性毫无意义
<input type="text" name="param1">
添加回答
举报
0/150
提交
取消