2013-09-24 19 views
1

感謝您閱讀我的問題。如何使用表單數組的響應?[PlayFramework]

如何使用表單數組的響應?

請仔細閱讀printSystem.scala.html

public class Test extends Controller { 

    static int form_max_length = 100; 
    static Form<Torque> torqueForm; 
    static Torque[] torque = new Torque[form_max_length]; 
    static int checkData = 0; 

    public static Result completeData() { 
     torqueForm = form(Torque.class).bindFromRequest(); 

     for (int checkData=0; checkData<form_max_length; checkData++) { 
      if (torque[checkData] != null) { 
       checkData++;    
      } else { 
       torque[checkData] = torqueForm.get(); 
       torque[checkData].save(); 
       break; 
      } 
     } 

     return TODO; 
     } 

    public static Result printData() { 

     return ok(printSystem.render(torqueForm)); 

    } 
} 

torqueSystem.scala.html


@main("") { 

<form action="/test" method="POST"> 
    <div> 
     <label> Title </label> 
     <input type="text" name="title"> 
    </div> 
    <div> 
     <label> Contents </label> 
     <input type="text" name="contents"> 
    </div> 
    <div> 
     <input type="submit" value="submit"> 
    </div> 
</form> 

} 

printSystem.scala.html


 
    @(form: Form[Torque]) 

    main("") { 
     @form.field("id").value() 
     @form.field("title").value() 
     @form.field("contents").value() 
    } 

請幫助我。

回答

0

你沒有確切地告訴你的問題是什麼,但我試圖猜測。我可以給你一個替代方案嗎?它應該可以與Play 2.1.x一起使用。

而不是一個數組,你可能想要使用一個地圖。它也可以作爲表格的迴應。

public static Result completeData() { 
    DynamicForm f = play.data.Form.form().bindFromRequest(); 
    Map<String, String> data = f.data(); 

    for (Entry<String, String> entry : formData.entrySet()) { 
     String key = entry.getKey(); 
     if (key.equals("contents")) { 
      yourModel.setSomeData(entry.getValue()); 
     } 
    } 
} 

如果你真的想你的陣列可以用formData.entrySet處理它()。指定者()。

祝你好運!

+0

謝謝.i我將使用此源^^♥ – toyou04net