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

前端之家收集整理的这篇文章主要介绍了表格 – Play Framework 2根据请求绑定表单前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我是Play2的新手(我已经使用Play1开发了一个项目),我遇到了来自请求的表单绑定问题.
关于表单的文档非常简单.

这是我的控制器的代码

  1. private final static Form<Estimation> estimationForm = form(Estimation.class);
  2.  
  3. /**
  4. * Get an estimation by form
  5. * @return
  6. */
  7. public static Result estimation() {
  8. return ok(views.html.rate.estimation.render(
  9. estimationForm,City.findAll()
  10. ));
  11. }
  12.  
  13. /**
  14. * Display estimation results
  15. * @return
  16. */
  17. public static Result results() {
  18. if (request().method().equals("POST")) {
  19. Form<Estimation> form = estimationForm.bindFromRequest();
  20. if (form.hasErrors()) {
  21. System.out.println(form.errorsAsJson().toString());
  22. return ok(views.html.rate.estimation.render(
  23. form
  24. City.findAll()
  25. ));
  26. }
  27. else {
  28. System.out.println(form.get());
  29. return ok(views.html.rate.results.render(
  30.  
  31. ));
  32. }
  33. }
  34. else {
  35. return estimation();
  36. }
  37. }

我在选择中显示城市:

  1. <select id="city" name="city">
  2. <option value="1">Paris,France</option>
  3. <option value="2">Lyon,France</option>
  4. <option value="3">Marseille,France</option>
  5. <option value="4">Barcelona,Spain</option>
  6. <option value="5">Berlin,Germany</option>
  7. </select>

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

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

谢谢.

解决方法

将选择字段的名称设置为name =“city.id”

猜你在找的HTML相关文章