java - Issue with bindFromRequest in Play! Framework 2.3 -
i'm trying use automatic binding feature of play, without success. i'm developing in java, on eclipse 4.4 luna.
here form :
<h2>create 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" /> guest? <input type="checkbox" name="isguest" /> <input type="submit" value="create user" /> </form>
here 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 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 content of request dynamicform requestdata = form.form().bindfromrequest(); string firstname = requestdata.get("firstname"); string lastname = requestdata.get("lastname"); // printing content works, able see correct values system.out.println(firstname); // bob system.out.println(lastname); // smith // 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 automatic binding of values doesn't work. have made sure fields name in form correspond attributes names in class, , should enough form binding work, right?
i using eclipse luna, , turned off automatic project build (i manually console). know eclipse can cause issues because of auto-build feature. note: way go, didn't clean project using activator command, user dmitri suggested. also, have once, long don't turn on automatic build feature in eclipse.
i have tried restarting eclipse , application several times, without success...
edit: tried using string attributes users class, since requestdata.get(string s) method returns string. still no success...
edit 2: i'm going bind values manually... if have idea, please post :)
edit 3: i've updated code follow rules mentioned in answer below
edit 4: can't autobinding working when using postgresql 9.3 database. when use in-memory database, works smoothly. also, since there no jdbc driver java 8 , postgresql 9.3, i'm using older version of driver (actually driver on pgsql's website, couldn't working play). have check happens db, i'll report here!
edit 5: tried create custom data binder 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; } });
... didn't work!
edit 6: user dmitri has found working solution: have compile project outside of eclipse. seems there incompatibilities between eclipse's compilator , play! framework's compilator...
i have been struggling same problem: bindfromrequest returned nulls "name" field. did same guy in play java introduction video did: youtube.com/watch?v=blrmnjpqszc . still no luck. i've been working on windows 7 jdk 1.8. ide: eclipse 4.4.0. , run activator through cygwin.
this solved problem me:
- in eclipse: project -> build automatically - > turn off
- in cygwin: ./activator clean; ./activator compile; ./activator run;
after this, bindfromrequest binds name correctly , puts database.
Comments
Post a Comment