gpt4 book ai didi

java - Play Framework 中的发布请求中的表单值为null

转载 作者:行者123 更新时间:2023-12-02 07:05:06 25 4
gpt4 key购买 nike

我有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>

}

回到 Controller 中,它看起来像这样:
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));
}

但是,当我查看 form().bindFromRequest()的结果时,它始终为空。我想念什么吗?

最佳答案

这是我的处理方式:

模板

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

class 联系
public class Contact {
public String firstName;
public String lastName;
public String email;
public String company;
// other stuff
}

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

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

  • 编辑

    if you can explain why the way I was doing it previously didn't work



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

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

    get(java.lang.String key)

    Gets the concrete value if the submission was a success.



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

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

    关于java - Play Framework 中的发布请求中的表单值为null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13688668/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com