Issue with bindFromRequest in Play! Framework 2.3

前端 未结 6 1578
夕颜
夕颜 2021-01-12 01:37

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 :

相关标签:
6条回答
  • 2021-01-12 01:48

    I solved this by Adding Setters and Getters. If you have Entity/Model class you should add setters and getters. If you have FormData classes add setters and getters for it as well.

    So when you call

    Form<YourFormData> formData =       Form.form(YourFormData.class).bindFromRequest(); 
     YourFormData formData = formData.get(); 
    

    Your formData will now have all the values set. Hope this helps!

    0 讨论(0)
  • 2021-01-12 02:02

    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.

    0 讨论(0)
  • 2021-01-12 02:03

    In your code you have :

    Users u = Form.form(Users.class).bindFromRequest().get(); 
    

    Try with this instead :

    Users user = new Users();
    Form <Users> u = Form.form(Users.class).fill(user).bindFromRequest();
    

    EDIT :

    May be the problem is the input types you're using. Try to generate your form like this :

    @form(routes.Backend.createUser()) {
    
                <label>First Name:</label> @inputText(userForm("first_name")) <br />
                <label>Last Name:</label> @inputText(userForm("first_name")) <br />
                <label>Email:</label> @inputText(userForm("email")) <br />
                <label>Pin:</label> @inputText(userForm("pin")) <br />
                <label>Status:</label> @inputText(userForm("status")) <br />
                <label>Is Guest:</label> @checkbox(userForm("is_guest")) <br />
                <input type="submit" value="Create user" />
        }
    

    Then in User Entity : try to change all columns type to String

    @Entity
    public class Users extends Model {
    
    // Database columns
    @Id
    public int user_id;
    
    public String first_name;
    public String last_name;
    public String email;
    public String pin;
    public String status;
    public String is_guest;
    
    }
    

    In your controller :

    public class Backend extends Controller {
    
      public static Result createUser() {
        Form <Users> userForm = Form.form(Users.class).bindFromRequest();
        Users user = userForm .get();
        user.save();
    }
    }  
    
    0 讨论(0)
  • 2021-01-12 02:03

    I know this post has an accepted answer but wanted to post my experience with this issue.

    I had the same issue and performed all the steps mentioned in the answer above i.e. Eclipse build automatically and then clean and compile through the activator. However, this did not work. I tried many different ways and even created a project from scratch without creating any Eclipse dependencies. Still, it did not work.

    Then, I looked at my model and decided to try by changing the case of my property names, and then voila! It worked!

    Here is what my model looked like BEFORE:

    public class Test {
     public String FirstName;
     public String LastName;}
    

    Now, I changed it to look like this:

    public class Test {
      public String firstName;
      public String lastName;}
    

    Just wanted to point this out since it is not obvious and I am coming from .Net

    0 讨论(0)
  • 2021-01-12 02:05

    Create getters/setters for your data model. It has solved my problem.

    0 讨论(0)
  • 2021-01-12 02:05

    There is absolutely no link between the binding and your database. Do not follow @blackbishop's advice telling you to change all the fields of your model to String. That's a very bad idea, if there are different types, there is a reason...

    Moreover, Ebean (supposing you're using it) or JPA generate database column types according to your Java properties type. Why would you store a 0 or a 1 in a varchar column ?

    Follow these rules :

    1. Use a singular name for your models (User instead of Users)
    2. Always use camel case for your properties, no underscores (firstName instead of first_name, lastName instead of last_name...)
    3. Check errors before getting the value after binding

    That should give you this :

    public static Result createUser() {
        Form<User> form = Form.form(User.class).bindFromRequest();
        if (form.hasErrors()) {
            // Do what you have to do (i.e. : redirect to the form with a flash message)
        }
        User u = form.get();
        u.save();
        return redirect(routes.Backend.allUsers());
    }
    

    By the way, in your testing lines, the user_id is correctly generated because you have no validation rules and this data comes from the database, not the form.

    0 讨论(0)
提交回复
热议问题