Play framework 2.0 Form.bindFromRequest()。get()返回空模型 [英] Play framework 2.0 Form.bindFromRequest().get() returns empty model

查看:338
本文介绍了Play framework 2.0 Form.bindFromRequest()。get()返回空模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to receive same POST data from a socket communication.

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

This is the code that send the POST and receive the response, and seems to work correctly:

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的代码:

This is the code that receive the 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模型以这种方式定义:

The AdRequest model is defined in this way:

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

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

The form object receive the data because I can see them in debug, but the adr object returned by the get() method contains only null values:

adr = {
    t: null,
    u: null
}

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

Instead, if I use this code to read the data it works correctly:

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

我做错了什么?
这是Play Framework的错误吗?

What I'm doing wrong? Is it a Play Framework bug?

谢谢。

推荐答案

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

The problem for me, it seems, was that Eclipse was interfering with the code generation and generally messing up the generated bytecode.

关闭自动构建 Eclipse解决了这个问题。

Turning off "Build Automatically" in Eclipse fixed the problem.

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

这篇关于Play framework 2.0 Form.bindFromRequest()。get()返回空模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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