我正在嘗試使用 @ModelAttribute 和 MediaType.APPLICATION_FORM_URLENCODED_VALUE 作為使用的數(shù)據(jù)類型將簡(jiǎn)單的 HTML 表單發(fā)布到 Spring RestController。我已經(jīng)仔細(xì)檢查了所有與我的請(qǐng)求 bean 匹配的表單字段。當(dāng)請(qǐng)求進(jìn)入映射方法時(shí),所有請(qǐng)求 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貢獻(xiàn)1871條經(jīng)驗(yàn) 獲得超8個(gè)贊
name
您的 HTML 輸入中缺少該屬性,id
發(fā)布 HTML 表單時(shí)該屬性毫無(wú)意義
<input type="text" name="param1">
添加回答
舉報(bào)
0/150
提交
取消