如何表单域绑定到对象中播放2框架(How to bind form field to an obje

2019-10-17 07:22发布

我一直有麻烦我的表单绑定正确(基本上试错)工作。 在播放2.0.3(JAVA)是什么将窗体绑定到它是由其他对象的模型的正确方法?

我做了这个小例子,试图更好地理解它。 但是,即使这个简单的例子似乎有问题。

这我想表单绑定的简单类有3个领域的一个普通的字符串字段,一个字符串列表,并且这仅仅是围绕一个字符串的包装的自定义字段。 在提交表单的所有字段填充,除了这仍然是空的自定义字段。

下面是实际的代码

调节器

static Form<Simple> simpleform=form(Simple.class);
public static Result simpleForm(){
Form<Simple> filledForm=simpleform.bindFromRequest();
        System.out.println(filledForm);
    return ok(views.html.simpleForm.render(filledForm.get().toString()));
}

模型

public class Simple {
    public String text;
    public List<String> stringList;
    public SimpleWrapper wrappedText;
    @Override
    public String toString(){
        return text +"-"+simpleWrapper+"-"+stringList;
}

public  class SimpleWrapper{
        String otherText;
        public SimpleWrapper(){}
        public SimpleWrapper(String otherText){
            this.otherText=otherText;
        }
        @Override
        public String toString(){
            return otherText;
        }
    }

视图

@(text:String)
@import helper._
@form(routes.Management.simpleForm()){
  <input type="hidden" value="string" name="stringList[0]">
  <input type="hidden" value="stringAgain" name="stringList[1]">
  <input type="hidden" value="wrapped" name="wrappedText.otherText">
  <input type="text" id="text" name="text">
  <input type="submit" value="submit">
}
This was passed @text

Answer 1:

为了让你必须为你的class.In我的实验SimpleWrapper类失踪的setter方法的类应该是提供一个setter方法的对象的自动绑定

public  class SimpleWrapper{
    String otherText;
    public SimpleWrapper(){}

    public setOtherText(String otherText){
     this.otherText=otherText;
    }

    @Override
    public String toString(){
        return otherText;
    }
}

看来即使是构造无关。

这是一个关于底层春数据粘合剂链接的链接可能会有所帮助。 我得到了来自谷歌玩团



文章来源: How to bind form field to an object in Play 2 Framework