Spring RestController POST 接受一个基本的 HTML 表单 [英] Spring RestController POST accepting a basic HTML form

查看:59
本文介绍了Spring RestController POST 接受一个基本的 HTML 表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用@ModelAttribute 和 MediaType.APPLICATION_FORM_URLENCODED_VALUE 作为消费数据类型将一个简单的 HTML 表单发布到 Spring RestController.我已经仔细检查了与我的请求 bean 匹配的所有表单字段.

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.

当请求进入映射方法时,所有的请求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>

推荐答案

您在 HTML 输入中缺少 name 属性,id 属性在发布 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">

这篇关于Spring RestController POST 接受一个基本的 HTML 表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆