Play中的bindFromRequest问题!框架2.3 [英] Issue with bindFromRequest in Play! Framework 2.3

查看:485
本文介绍了Play中的bindFromRequest问题!框架2.3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Play的自动绑定功能,但没有成功。我正在用Java 4.4 Luna开发Java。



这是我的表单:

 < h2>创建新用户< / h2> 
< form action =@ routes.Backend.createUser()method =post>
名字
< input type =textname =firstName/>
姓氏
< input type =textname =lastName/>
电子邮件
< input type =emailname =email/>
PIN
< input type =numbername =pin/>
状态
< input type =textname =status/>
是客人吗?
< input type =checkboxname =isGuest/>

< input type =submitvalue =创建用户/>
< / form>

这是我的班级用户:



< pre class =lang-java prettyprint-override> @Entity
公共类用户扩展模型{

//数据库列
@Id
public int userId;

public String firstName;
public String lastName;
public String email;
public int pin;
public String status;
public boolean isGuest;

}

这是我的控制器:

  public class Backend extends Controller {

public static Result createUser(){
形式<用户和GT; form = Form.form(Users.class).bindFromRequest();
if(form.hasErrors()){
// doSomething()
} else {
Users u = form.get();
u.save();
}

// TESTING
//检查请求的内容
DynamicForm requestData = Form.form()。bindFromRequest();
String firstName = requestData.get(firstName);
String lastName = requestData.get(lastName);
//打印内容有效,我能看到正确的值
System.out.println(firstName); // Bob
System.out.println(lastName); //史密斯
//这在某种程度上不起作用...
System.out.println(u.firstName); // NULL
System.out.println(u.lastName); // NULL
System.out.println(u.userId); //正确生成
//结束测试

返回重定向(routes.Backend.allUsers());
}
}

我想知道为什么值的自动绑定不会工作。我已经确保我的表单中的字段名称对应于类中的属性名称,这应该足以使表单绑定起作用,对吗?



I我正在使用Eclipse Luna,我关闭了自动项目构建(我从控制台手动完成)。我知道有时Eclipse会因为自动构建功能而导致问题。 注意:这是要走的路,但是我没有使用activator命令清理项目,正如用户Dmitri建议的那样。此外,只需要在Eclipse中不打开自动构建功能,就只需执行一次。



我尝试重启Eclipse和应用程序多次,没有成功...



编辑:
我尝试仅使用我的Users类的String属性,因为requestData.get(String s)方法返回一个String。但仍然没有成功...



编辑2:
我要手动绑定值...如果有人有个主意,请发帖:)



编辑3:
我已更新我的代码,以遵循上述规则回答如下



编辑4:
我只能在使用Postgresql 9.3数据库时才能进行自动绑定。当我使用内存数据库时,一切都很顺利。此外,由于Java 8和postgresql 9.3没有JDBC驱动程序,我使用的是较旧版本的驱动程序(实际上驱动程序在PGSQL的网站上,但我无法使用Play)。我将不得不检查另一个数据库会发生什么,然后我会在这里报告!



编辑5:
我试过了创建我的自定义数据绑定器:

  Formatters.register(User.class,new Formatters.SimpleFormatter< User>() {
@Override
public User parse(String arg0,Locale arg1)抛出ParseException {
User u = new Model.Finder< Integer,User>(Integer.class,User.class)。 byId(Integer.parseInt(arg0));
return u;
}
@Override
public String print(User arg0,Locale arg1){
returnUser :+ arg0.firstName;
}
});

...但它不起作用!



编辑6:
用户Dmitri找到了一个可行的解决方案:您必须在Eclipse之外编译项目。似乎Eclipse的编译器和Play之间存在一些不兼容性!框架的编译器......

解决方案

我一直在努力解决完全相同的问题:bindFromRequest为name字段返回了空值。这个Play for Java介绍视频中的人做的完全相同:youtube.com/watch?v = bLrmnjPQsZc。但仍然没有运气。
我一直在使用JDK 1.8开发Windows 7。 IDE:Eclipse 4.4.0。我通过cygwin运行激活器。



这就解决了我的问题:


  1. 在Eclipse中:项目 - >自动构建 - >关闭

  2. 在cygwin中:
    ./activator clean;
    ./activator compile;
    ./activator run;

此后,bindFromRequest正确绑定名称并将其放入数据库。


I'm trying to use the automatic binding feature of Play, without success. I'm developing in Java, on Eclipse 4.4 Luna.

Here is my form :

<h2>Create a new user</h2>
<form action="@routes.Backend.createUser()" method="post">
    First Name
    <input type="text" name="firstName" />
    Last Name
    <input type="text" name="lastName" />
    E-mail
    <input type="email" name="email" />
    PIN
    <input type="number" name="pin" />
    Status
    <input type="text" name="status" />
    Is guest?
    <input type="checkbox" name="isGuest" />

    <input type="submit" value="Create user" />
</form>

Here is my class "Users":

@Entity
public class Users extends Model {

// Database columns
@Id
public int userId;

public String firstName;
public String lastName;
public String email;
public int pin;
public String status;
public boolean isGuest;

}

And here is my controller:

public class Backend extends Controller {

  public static Result createUser() {
    Form<Users> form = Form.form(Users.class).bindFromRequest();
    if (form.hasErrors()) {
        // doSomething()
    } else {
        Users u = form.get();
        u.save();
    }

    // TESTING
    // Checking the content of the request
    DynamicForm requestData = Form.form().bindFromRequest();
    String firstName = requestData.get("firstName");
    String lastName = requestData.get("lastName");
    // Printing the content works, I am able to see the correct values
    System.out.println(firstName); // Bob
    System.out.println(lastName); // Smith
    // This somehow doesn't work...
    System.out.println(u.firstName); // NULL
    System.out.println(u.lastName); // NULL
    System.out.println(u.userId); // Correctly generated
    // END OF TESTING

    return redirect(routes.Backend.allUsers());
  }
}

I wonder why the automatic binding of values doesn't work. I have made sure that the fields name in my form correspond to the attributes names in the class, and this should be enough for the form binding to work, right?

I am using Eclipse Luna, and I turned off automatic project build (I do it manually from the console). I know that sometimes Eclipse can cause issues because of that auto-build feature. Note: This was the way to go, but I didn't clean the project using the activator command, as user Dmitri suggested. Also, you only have to do this once, as long as you don't turn on the automatic build feature in Eclipse.

I have tried restarting Eclipse and the application several times, without success...

EDIT: I tried using only String attributes for my Users class, since the requestData.get(String s) method returns a String. But still no success...

EDIT 2: I'm going to bind the values manually... If anyone have an idea, please post :)

EDIT 3: I've updated my code to follow the rules mentioned in the answer below

EDIT 4: I can't get autobinding working only when using my Postgresql 9.3 database. When I use in-memory database, everything works smoothly. Also, since there was no JDBC driver for Java 8 and postgresql 9.3, I'm using an older version of the driver (actually the driver is on PGSQL's website, but I couldn't get it working with Play). I will have to check what happens with another DB, then I'll report back here!

EDIT 5: I tried to create my custom data binder like this:

        Formatters.register(User.class, new Formatters.SimpleFormatter<User>() {
        @Override
        public User parse(String arg0, Locale arg1) throws ParseException {
            User u = new Model.Finder<Integer, User>(Integer.class, User.class).byId(Integer.parseInt(arg0));
            return u;
        }
        @Override
        public String print(User arg0, Locale arg1) {
            return "User : " + arg0.firstName;
        }
    });

... but it didn't work!

EDIT 6: User Dmitri has found a working solution: you have to compile the project outside of Eclipse. It seems that there is some incompatibilities between Eclipse's compilator and Play! Framework's compilator...

解决方案

I have been struggling with exactly the same problem: bindFromRequest returned nulls for "name" field. I did exactly the same what a guy in this Play for Java introduction video did: youtube.com/watch?v=bLrmnjPQsZc . But still no luck. I've been working on Windows 7 with JDK 1.8. IDE: Eclipse 4.4.0. And I run activator through cygwin.

This is what solved the problem for me:

  1. In Eclipse: Project -> Build Automatically - > turn off
  2. In cygwin: ./activator clean; ./activator compile; ./activator run;

After this, bindFromRequest binds name correctly and puts it into the database.

这篇关于Play中的bindFromRequest问题!框架2.3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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