表格 – Play Framework 2根据请求绑定表单

表格 – Play Framework 2根据请求绑定表单,第1张

概述我是Play2的新手(我已经使用Play1开发了一个项目),我遇到了来自请求的表单绑定问题. 关于表单的文档非常简单. 这是我的控制器的代码: private final static Form<Estimation> estimationForm = form(Estimation.class);/** * Get an estimation by form * @return */ 我是Play2的新手(我已经使用Play1开发了一个项目),我遇到了来自请求的表单绑定问题.
关于表单的文档非常简单.

这是我的控制器的代码:

private final static Form<Estimation> estimationForm = form(Estimation.class);/** * Get an estimation by form * @return */public static Result estimation() {    return ok(vIEws.HTML.rate.estimation.render(        estimationForm,City.findAll()    ));}/** * display estimation results * @return */public static Result results() {    if (request().method().equals("POST")) {        Form<Estimation> form = estimationForm.bindFromrequest();        if (form.hasErrors()) {            System.out.println(form.errorsAsJson().toString());            return ok(vIEws.HTML.rate.estimation.render(                form                City.findAll()            ));        }        else {            System.out.println(form.get());            return ok(vIEws.HTML.rate.results.render(            ));        }    }    else {        return estimation();    }}

我在选择中显示城市:

<select ID="city" name="city">          <option value="1">Paris,France</option>    <option value="2">Lyon,France</option>    <option value="3">Marseille,France</option>    <option value="4">barcelona,Spain</option>    <option value="5">Berlin,Germany</option></select>

当我提交表单时,我有以下错误:
{“city”:[“无效的值”]}

所以这是我的问题:绑定器似乎适用于简单字段(例如我的模型中的String属性),但是@ManyToOne关系呢?

谢谢.

解决方法 将选择字段的名称设置为name =“city.ID” 总结

以上是内存溢出为你收集整理的表格 – Play Framework 2根据请求绑定表单全部内容,希望文章能够帮你解决表格 – Play Framework 2根据请求绑定表单所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1144676.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-31
下一篇2022-05-31

发表评论

登录后才能评论

评论列表(0条)

    保存