简体   繁体   中英

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. I've double checked all of my forms fields which match my request bean.

When the request enters the mapped method, all of the request beans fields are 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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