0

I am using play framework for java and want to learn form submission. So I have this method in my controller:

public Result index(String url) {
    Form<SimpleUser> userForm = Form.form(SimpleUser.class);
    Map<String,String> anyData = new HashMap();
    anyData.put("name", "hossein");
    SimpleUser user = userForm.bind(anyData).get();
    return ok(views.html.index.render(user.getName()));
}

The problem is that the line containing "SimpleUser user = userForm.bind(anyData).get();" gives error :

java: cannot access org.codehaus.jackson.JsonNode class file for org.codehaus.jackson.JsonNode not found

The class SimpleUser definition:

public class SimpleUser {
    protected String name;
    public void setName(String name) {this.name = name;}
    public String getName() {return name;}

}

Actually this error confuses me because I don't know what does it have with JsonNode. Why this error occurs and how can I fix it?

Thanks a lot!

HsnVahedi
  • 1,271
  • 3
  • 13
  • 34

1 Answers1

0

When I copy/paste your code into my play 2.4 application I get the following

[RuntimeException: Cannot instantiate class controllers.Application$SimpleUser. It must have a default constructor]

This code, however, worked perfectly.

   public static class SimpleUser {
        protected String name;
        protected String username;
        protected String password;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getUsername() {
            return username;
        }

        public void setUsername(String username) {
            this.username = username;
        }

        public String getPassword() {
            return password;
        }

        public void setPassword(String password) {
            this.password = password;
        }
    }
    public Result index2() {
        Form<SimpleUser> userForm = Form.form(SimpleUser.class);
        Map<String,String> anyData = new HashMap();
        anyData.put("name", "hossein");
        anyData.put("password", "123");
        anyData.put("username", "hsn.vahedi");
        SimpleUser user = userForm.bind(anyData).get();
        return ok();
        //return ok(views.html.index.render(user.getName()));
    }

See if that works for you. There is a version of .bind() that takes a ObjectNode, but there is also a version that takes the map.

Justin Edwards
  • 152
  • 1
  • 11
  • Thanks for answer but making SimpleUser static gives me an error. And I found that java doesn't let designing static non-inner classes. http://stackoverflow.com/questions/3584113/why-are-you-not-able-to-declare-a-class-as-static-in-java – HsnVahedi Jan 24 '16 at 17:32