表格 – Play Framework 2根据请求绑定表单
发布时间:2020-12-14 19:40:47 所属栏目:资源 来源:网络整理
导读:我是Play2的新手(我已经使用Play1开发了一个项目),我遇到了来自请求的表单绑定问题. 关于表单的文档非常简单. 这是我的控制器的代码: private final static FormEstimation estimationForm = form(Estimation.class);/** * Get an estimation by form * @re
我是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> 当我提交表单时,我有以下错误: 所以这是我的问题:绑定器似乎适用于简单字段(例如我的模型中的String属性),但是@ManyToOne关系呢? 谢谢. 解决方法
将选择字段的名称设置为name =“city.id”
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |