代码之家  ›  专栏  ›  技术社区  ›  Half_Duplex

接受基本html表单的spring restcontroller post

  •  0
  • Half_Duplex  · 技术社区  · 6 年前

    我正试图使用@modelattribute和mediatype.application\u form\u urlencoded\u值作为消费数据类型,将一个简单的html表单发布到spring restcontroller。我已经仔细检查了所有与请求bean匹配的表单字段。

    当请求进入映射方法时,所有请求bean字段都为空。

    @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>
    
    1 回复  |  直到 6 年前
        1
  •  2
  •   Nowres Rafed    6 年前

    你错过了 name HTML输入中的属性, id 属性在发布HTML表单时没有意义

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