简体   繁体   English

Spring RestController POST 接受基本的 HTML 形式

[英]Spring RestController POST accepting a basic HTML form

I'm attempting to post a simple HTML form to Spring RestController using @ModelAttribute and MediaType.APPLICATION_FORM_URLENCODED_VALUE as consumed data type.我正在尝试使用 @ModelAttribute 和 MediaType.APPLICATION_FORM_URLENCODED_VALUE 作为消费数据类型将简单的 HTML 表单发布到 Spring RestController。 I've double checked all of my forms fields which match my request bean.我已经仔细检查了与我的请求 bean 匹配的所有 forms 字段。

When the request enters the mapped method, all of the request beans fields are null.当请求进入映射方法时,所有的请求bean字段都是null。

@RestController
@EnableWebMvc
public 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>

You are missing the name attribute in your HTML inputs, id attribute is meaningless when posting an HTML form您在 HTML 输入中缺少name属性,发布 HTML 表单时id属性没有意义

<input type="text" name="param1">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM