简体   繁体   English

如何将响应用于表单数组?[PlayFramework]

[英]How to use Response for Form Array?[ PlayFramework ]

Thank you for reading my Question. 感谢您阅读我的问题。

How to use response for Form Array ? 如何对表单数组使用响应?

Please read printSystem.scala.html 请阅读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 扭矩系统.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 printSystem.scala.html


@(form: Form[Torque])

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

Please help me. 请帮我。

You didn't tell exactly what your problem is but I try to guess. 您没有确切说明问题所在,但我尝试猜测。 May I present you an alternative? 我可以给你一个替代方案吗? It should work with Play 2.1.x. 它应该与Play 2.1.x一起使用。

Instead of an array you might want to use a Map. 您可能要使用Map而不是数组。 It also obtainable as response from the form. 也可以从表单获得作为响应。

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());
        }
    }
}

If you really want your array you can process it with formData.entrySet().toArray(). 如果您确实想要数组,则可以使用formData.entrySet()。toArray()处理它。

Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM