【问题标题】:Play framework 2.0 Form.bindFromRequest().get() returns empty modelPlay framework 2.0 Form.bindFromRequest().get() 返回空模型
【发布时间】:2012-05-05 06:27:28
【问题描述】:

我需要从套接字通信接收相同的 POST 数据。

这是发送 POST 和接收响应的代码,似乎工作正常:

String data = "t=" + URLEncoder.encode("Title", "UTF-8") +
    "&u=" + URLEncoder.encode("http://www.myurl.com", "UTF-8");

URL url = new URL("http://localhost:9000/adserver");
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();

BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String output = "Data received\r\n", line;
while ((line = rd.readLine()) != null) {
    output += line;
}
wr.close();
rd.close();

return ok(output);

这是接收 POST 的代码:

Form<AdRequest> form = form(AdRequest.class).bindFromRequest();

if(form.hasErrors()) {
    return badRequest("error");
} else {
    AdRequest adr = form.get();
    return ok(adr.t + " - " + adr.u);
}

AdRequest 模型是这样定义的:

public class AdRequest {
    public String t;
    public String u;
}

表单对象接收数据是因为我可以在调试中看到它们,但是get()方法返回的adr对象只包含空值:

adr = {
    t: null,
    u: null
}

相反,如果我使用此代码读取数据,它可以正常工作:

Map<String, String[]> asFormUrlEncoded = request().body().asFormUrlEncoded();
return ok(asFormUrlEncoded.get("t")[0] + " - " + asFormUrlEncoded.get("u")[0]);

我做错了什么? 是 Play 框架的错误吗?

谢谢。

【问题讨论】:

  • 你在哪里命名你的 POST 变量?如果你想得到它们,你必须把它们绑定到你的模型上。
  • 我假设绑定是通过匹配属性名称自动完成的。所以我现在就试试。
  • 它不起作用。我正在寻找文档 (playframework.org/documentation/2.0/JavaForms),但即使是使用 HashMap 的简单代码也不起作用。我总是返回空值。我应该如何绑定它们?可以举个例子吗?
  • 调用form(AdRequest.class).bindFromRequest()后能否提供变量form的数据?
  • form.toString() 打印:Form(of=class classes.AdRequest, data={}, value=None, errors={})

标签: post http-post playframework-2.0


【解决方案1】:

对我来说,问题似乎在于 Eclipse 干扰了代码生成,并且通常会弄乱生成的字节码。

在 Eclipse 中关闭“自动构建”解决了这个问题。

此链接有帮助:https://groups.google.com/forum/?fromgroups#!topic/play-framework/JYlkz_Nh31g

【讨论】:

    【解决方案2】:

    这个问题很老了,但我会描述我们的问题,以防万一有人遇到同样的情况。

    简答

    尝试添加设置器AdRequest

    长答案

    我发现现在表单模型类需要设置器,否则 Play 不会填充它们。我不知道为什么。对于一些同事来说,代码就像以前一样工作,而对我来说,Play 需要二传手。不知道。

    调试Form.bindFromRequest,最终到了这一行:

    package org.springframework.beans;
    
    class BeanWrapperImpl {
        ...
        private void setPropertyValue(BeanWrapperImpl.PropertyTokenHolder tokens, PropertyValue pv) throws BeansException {
            ...
            throw new NotWritablePropertyException(this.getRootClass(), this.nestedPath + propertyName, matches.buildErrorMessage(), matches.getPossibleMatches());
    

    matches.buildErrorMessage() 会生成这样的消息:

    Bean property email is not writable or has an invalid setter method
    

    添加设置器(例如setEmail)后,它就可以工作了。

    更新:

    我们发现表单绑定依赖于spring framework版本。我们使用的是 4.0.3-RELEASE,然后我们更新到 4.0.5-RELEASE,这就是 Play 表单开始失败的时候。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多