在play框架中的post请求中,表单值为null

Phi*_*idt 2 java forms jsp playframework playframework-2.0

我有html发回一个表单,像这样:

@(message: String)

@main("Contact") {
<link rel="stylesheet" media="screen" href="@routes.Assets.at("stylesheets/contact.css")">
<div id="pageContainer">
    <div id="form">
        <div id="topText">
            <p>Have a question or need some assistance? Let us know and we'll get back to you ASAP.</p>
        </div>
        <br/>
        <form method="POST" action="@routes.Home.contact()">

            <div id="contactInfo">
                <label class="contactLabel">First Name:</label><input type="text" name="firstName" id="firstName" class="contactInput" /> <br />
                <label class="contactLabel">Last Name:</label><input type="text" name="lastName" id="lastName" class="contactInput" /> <br />
                <label class="contactLabel">Email:</label><input type="text" name="email" id="email" firstName" class="contactInput" /> <br />
                <label class="contactLabel">Company:</label><input type="text" id="company" name="company" class="contactInput" /> <br />
                <input type="hidden" id="hidden" name = "hidden"></input>
                <p id="crap">@message</p>
            </div>
            <br/>
            <div id="message">
                <label class="contactLabel">Message:</label><textarea cols="50" rows="10"></textarea>
            </div>
            <input type="submit" name="submit" id="submit" value="Submit"></input>
            </form>
    </div>

</div>

}
Run Code Online (Sandbox Code Playgroud)

回到控制器,它看起来像这样:

public static Result contact()
{

    //Map<String,String[]> values = request().body();
    DynamicForm form = form().bindFromRequest();
    String first = "";
    if(form.data().get("firstName") != null)
    first = form.data().get("firstName").toString();

    return ok(views.html.contact.render(first));
}
Run Code Online (Sandbox Code Playgroud)

但是当我查看结果时form().bindFromRequest(),它总是空的.我错过了什么吗?

Ale*_*lex 8

我将如何做到这一点:

模板

@(message: String, contactForm: Form[Contact])

@import helper._

@main("Contact") {
<link rel="stylesheet" media="screen" href="@routes.Assets.at("stylesheets/contact.css")">
<div id="pageContainer">
    <div id="form">
        <div id="topText">
            <p>Have a question or need some assistance? Let us know and we'll get back to you ASAP.</p>
        </div>
        <br/>
        @form(routes.Home.contact()) {
            <div id="contactInfo">
                <label class="contactLabel">First Name:</label> @inputText(contactForm("firstName"), 'class -> "contactInput"') <br />
                <label class="contactLabel">Last Name:</label> @inputText(contactForm("lastName"), 'class -> "contactInput"') <br />
                <label class="contactLabel">Email:</label> @inputText(contactForm("email"), 'class -> "contactInput"') <br />
                <label class="contactLabel">Company:</label> @inputText(contactForm("company"), 'class -> "contactInput"') <br />
                <input type="hidden" id="hidden" name="hidden"></input>
                <p id="crap">@message</p>
            </div>
            <br/>
            <div id="message">
                <label class="contactLabel">Message:</label><textarea cols="50" rows="10"></textarea>
            </div>
            <input type="submit" value="Submit" />
        }
    </div>
</div>
}
Run Code Online (Sandbox Code Playgroud)

班级联系

public class Contact {
    public String firstName;
    public String lastName;
    public String email;
    public String company;
    // other stuff
}
Run Code Online (Sandbox Code Playgroud)

调节器

public static Result contact() {
    Form<Contact> contactForm = form(Contact.class).bindFromRequest();
    if (contactForm.hasErrors()) {
        // process
    } else {
        // contactForm.get().firstName should be filled with the correct data
        return ok(views.html.contact.render("message", contactForm)); 
    }
}
Run Code Online (Sandbox Code Playgroud)

对你起作用吗 ?您应该查看有关这些主题的Play2文档:

  1. JavaForms
  2. JavaForm助手

编辑

如果你能解释一下为什么我以前做的方式工作

我不确定,但我可以在你的代码中看到一个问题,你不必调用data()动态表单,直接使用DynamicForm.get()就足够了,所以first = form.get("firstName")就足够了.

此外,您可以从DynamicForm Javadoc中看到

get(java.lang.String key)

如果提交成功,则获取具体值.

DynamicForm如果没有错误,则后面的内部映射具有值,因此您可以hasErrors在从字段实际获取具体值之前进行检查.

从我的角度来看,使用form(Contact.class).bindFromRequest()填充类实例的样式会更好更容易Contact,不用说可以使用Java注释验证字段.