播放框架中的发布请求中的表单值为空 [英] Form values are null in post request in play framework

查看:113
本文介绍了播放框架中的发布请求中的表单值为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  @(message:String)

@main(Contact){
< link rel =stylesheetmedia =screenhref =@ routes.Assets.at(stylesheets / contact.css)>
< div id =pageContainer>
< div id =form>
< div id =topText>
< p>有疑问或需要一些帮助吗?请告诉我们,我们会尽快回复您。< / p>
< / div>
< br />
< form method =POSTaction =@ routes.Home.contact()>

< div id =contactInfo>
< label class =contactLabel>名字:< / label>< input type =textname =firstNameid =firstNameclass =contactInput/> < br />
< label class =contactLabel>姓氏:< / label>< input type =textname =lastNameid =lastNameclass =contactInput/> < br />
< label class =contactLabel>电子邮件:< / label>< input type =textname =emailid =emailfirstNameclass =contactInput/><< < / label>< input type =textid =companyname =companyclass =contactInput/ >< br />
<输入类型=隐藏ID =隐藏名称=隐藏>< /输入>
< p id =废话> ; @ message< / p>
< / div>
< b>
< div id =message>
< label class = contactLabel >消息:其中/标签>< textarea的COLS = 50\" 行= 10 >< / textarea的>
将/ DIV>
将输入类型=提交name =submitid =submitvalue =Submit>< / input>
< / form>
< / div>

< / div>

}

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

  public static Result contact()
{

//地图<字符串,字符串[] GT; 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));
}

但是当我查看形式的结果( ).bindFromRequest(),它总是空的。我错过了什么?

解决方案

这是我如何做到的:




$ b

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

@import helper._

@main(Contact){
< link rel =stylesheetmedia =screenhref =@ routes。 Assets.at( 样式表/ contact.css)>
< div id =pageContainer>
< div id =form>
< div id =topText>
< p>有疑问或需要一些帮助吗?请告诉我们,我们会尽快回复您。< / p>
< / div>
< br />
@form(routes.Home.contact()){
< div id =contactInfo>
< label class =contactLabel>名字:< / label> @inputText(contactForm(firstName),'class - >contactInput'')< br />
< label class =contactLabel>姓氏:< / label> @inputText(contactForm(lastName),'class - >contactInput'')< br />
< label class =contactLabel>电子邮件:< / label> @inputText(contactForm(email),'class - >contactInput'')< br />
< label class =contactLabel>公司:< / label> @inputText(contactForm(company),'class - >contactInput'')< br />
< input type =hiddenid =hiddenname =hidden>< / input>
< p id =crap> @ message< / p>
< / div>
< br />
< div id =message>
< label class =contactLabel>消息:< / textarea>< / label>< / textarea cols =50rows =10>
< / div>
< input type =submitvalue =Submit/>
}
< / div>
< / div>
}

联系人 $ b

  public class Contact {
public String firstName;
public String lastName;
public String email;
public String company;
//其他东西
}

控制器

  public static Result contact(){
Form< Contact> contactForm = form(Contact.class).bindFromRequest();
if(contactForm.hasErrors()){
// process
} else {
// contactForm.get()。firstName应该填充正确的数据
return ok(views.html.contact.render(message,contactForm));
}
}

它是否适合您?
您应该查看关于以下主题的Play2文档:


  1. JavaForms

  2. JavaForm Helpers

编辑


如果您可以解释为什么我之前做的 b $ b

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



除此之外,您可以从 DynamicForm Javadoc


get(java.lang.String key)

获取具体值如果提交是成功的


背后的内部映射 DynamicForm如果没有错误,有值,所以你可以在实际从字段获取具体值之前检查 hasErrors

从我的角度来看,使用表单(Contact.class).bindFromRequest()样式更好也更容易,它将填充一个实例类联系,这并不是说可以使用Java批注验证字段。


I have html to post back a form, like this:

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

}

Back in the controller, it looks like this:

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

But when I look through the result of form().bindFromRequest(), it is always empty. Am I missing something?

解决方案

Here is how I would do that:

template

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

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

Does it work for you ? You should take a look at the Play2 documentation about these topics:

  1. JavaForms
  2. JavaForm Helpers

Edit

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

I'm not sure about that, but I can see one problem in your code, you don't have to call data() on the dynamic form, using directly DynamicForm.get() is sufficient, so first = form.get("firstName") is sufficient.

Besides as you can see from the DynamicForm Javadoc

get(java.lang.String key)

Gets the concrete value if the submission was a success.

The internal map behind DynamicForm has values if there is no errors, so you may check with hasErrors before actually getting concrete values from field.

From my point of view, it is better and easier to use the form(Contact.class).bindFromRequest() style that will fill an instance of class Contact, this without saying that fields can be validated using Java annotations.

这篇关于播放框架中的发布请求中的表单值为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆