java - 在 Play Framework 2 上使用 ManyToOne 字段绑定(bind)表单请求

标签 java scala playframework playframework-2.0

当我将 manytoone 字段添加到我的模型“项目”并尝试绑定(bind)相应的表单时出现此错误。

Execution exception
[IllegalStateException: No value] at line 31

=> Item item = itemForm.bindFromRequest().get();

“项目”模型: 封装模型;

@Entity 
public class Item extends Model {

    @Id
    public Long id;

    @ManyToOne
    @Constraints.Required
    @Formats.NonEmpty
    public Category category;

    @Constraints.Required
    public String title;

    @Constraints.Required
    public String content;

    public String picture;

    (..)    
}

表单查看:

    @helper.form(action = routes.Application.newItem(), 'id -> "item_form", 'method -> "POST", 'enctype -> "multipart/form-data"){
    <fieldset>
        @helper.inputText(
        itemForm("title"),
        '_label -> "Titre"  )

        @helper.select(
        itemForm("category"), 
        helper.options(Category.list),
        '_label -> "Categorie")

        @helper.textarea(
        itemForm("content"),
        '_label -> "Description")

         @helper.inputFile(
         field = itemForm("picture"), 
         '_display -> "Attachment", 
         '_label -> Messages("Image") )
         <input type="submit" value="Ajouter">

    </fieldset>
    }

控制者:

public static Result newItem(){
        Item item = itemForm.bindFromRequest().get(); //SOMETHING GO WRONG HERE
        MultipartFormData body = request().body().asMultipartFormData();
        FilePart picture = body.getFile("picture");  
        if (picture != null) {
              (...)
        }
            else{
              (...)
            }
}

最佳答案

类别字段的表单 View 应该是,考虑到类别模型有 id 字段。

@helper.select(
        itemForm("category.id"), 
        helper.options(Category.list),
        '_label -> "Categorie")

关于java - 在 Play Framework 2 上使用 ManyToOne 字段绑定(bind)表单请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11802566/

相关文章:

java - 我可以使用带有较新 JDK 的 ant 为较旧的 Java 版本编译 java,以便它在编译使用较新 API 的代码时生成输出吗?

java - 分别分析字符串的每个单词

java - twitter4j 是否提供在 2 个日期之间搜索推文的功能

scala - StringOps.split(String,Int)中第二个参数的含义

scala - Playframework、scala 案例类和属性未找到

playframework - 在 Play Framework 中使用 sendmail 作为 SMTP 服务器

java - Spring queryForLong - 精度损失

java - 如何将 "java.nio.HeapByteBuffer"转换为字符串

scala 直接映射序列元素的字段

java - 由于嵌套 bean,无法在 playframework 中使用 GSON 解析 JSON