【问题标题】:Spring controller isn't binding data from html formSpring 控制器未绑定来自 html 表单的数据
【发布时间】:2015-09-13 21:11:52
【问题描述】:

在我当前的 spring-boot 项目中,我有一个表单,由这个控制器方法处理数据:

  @RequestMapping(value="alterar", method=RequestMethod.POST)
    @ResponseBody
  public void altera(@ModelAttribute("object") E object, BindingResult result) throws Exception {
    serv.altera(object);
  }

这个方法在服务类中调用那个:

  public void altera(E e) throws Exception {
        settings.save_settings(e);
    }

但是当我尝试通过此控制器提交数据时,尽管浏览器的开发人员工具显示字段正在发送到服务器,但控制器没有收到任何数据(我看到了 objectresult.getModel() 的两个值;对于两者,字段的值为空)。

任何人都可以在这里看到我做错了什么?

ps.:表单的html:

<form role="form" class="form" id="form" method="post" enctype="application/x-www-form-urlencoded" action="/Paypal/alterar">                                            
<div class="field-box">
    <label>http.ConnectionTimeOut</label>
    <div class="col-md-7">
        <input type="text" name="http_ConnectionTimeOut" class="form-control" value="" />
    </div>
</div>
...
</form>

【问题讨论】:

    标签: spring forms spring-mvc controller


    【解决方案1】:

    html表单中没有定义模型属性

    尝试将您的视图更新为

    <form role="form" class="form" id="form" method="post" enctype="application/x-www-form-urlencoded" action="/Paypal/alterar" modelAttribute="object">
    

    【讨论】:

      【解决方案2】:

      此外,modelattribute 只会将您的整个班级与一个单独的对象绑定,即object,并将直接给出默认值null。尝试改用@ResponseBody

      【讨论】:

      • 我不明白。我已经在这里使用了 RespondeBody(参见上面的控制器代码)。我还能做什么?
      • 使用 RequestBody 进行 POST AND PUT 而不是模型属性
      猜你喜欢
      • 1970-01-01
      • 2014-05-20
      • 1970-01-01
      • 2018-06-03
      • 1970-01-01
      • 2018-05-17
      • 1970-01-01
      • 2018-01-17
      • 1970-01-01
      相关资源
      最近更新 更多