感谢您阅读我的问题。
如何使用Form Array的响应?
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 :(得分:0)
你没有确切地说出你的问题是什么,但我试着猜测。我可以给你一个替代方案吗?它应该适用于Play 2.1.x。
您可能想要使用Map而不是数组。它也可以作为表格的回应获得。
public static Result completeData() {
DynamicForm f = play.data.Form.form().bindFromRequest();
Map<String, String> formData = f.data();
for (Entry<String, String> entry : formData.entrySet()) {
String key = entry.getKey();
if (key.equals("contents")) {
yourModel.setSomeData(entry.getValue());
}
}
}
如果你真的想要你的数组,你可以用formData.entrySet()。toArray()来处理它。
祝你好运!