2012-10-05 19 views
1

我一直無法讓表單綁定正常工作(基本上是試用版和錯誤)。 In Play 2.0.3(Java)將表單綁定到由其他對象組成的模型的正確方法是什麼?如何將表單字段綁定到Play 2框架中的對象

我捏造了這個小例子,試圖更好地理解它。 但即使這個基本的例子似乎有問題。

我試圖綁定窗體的簡單類有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())); 
} 

型號

查看

@(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 

回答

1

要允許對象,你必須提供您的class.In我的實驗setter方法的SimpleWrapper類失蹤的setter方法的類應該已經

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

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

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

的自動裝訂看來連構造函數是不相關的。

這是一個有關底層Spring數據綁定器link的鏈接,可能會有幫助。我從玩谷歌組合

相關問題