Spring RestController POST 接受基本的 HTML 表单

我正在尝试使用 @ModelAttribute 和 MediaType.APPLICATION_FORM_URLENCODED_VALUE 作为使用的数据类型将简单的 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>


蝴蝶不菲
浏览 162回答 1
1回答

ITMISS

name您的 HTML 输入中缺少该属性,id发布 HTML 表单时该属性毫无意义<input&nbsp;type="text"&nbsp;name="param1">
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java