我正在使用java的play框架,想学习表单提交。所以我在我的控制器中有这个方法:
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()));
}
问题是包含“SimpleUser user = userForm.bind(anyData).get();”的行给出错误:
java:无法访问org.codehaus.jackson.JsonNode 未找到org.codehaus.jackson.JsonNode的类文件
SimpleUser类定义:
public class SimpleUser {
protected String name;
public void setName(String name) {this.name = name;}
public String getName() {return name;}
}
实际上这个错误让我感到困惑,因为我不知道它对JsonNode有什么影响。为什么会出现此错误以及如何解决?
非常感谢!
答案 0 :(得分:0)
当我将代码复制/粘贴到我的Play 2.4应用程序中时,我得到以下内容
[RuntimeException: Cannot instantiate class controllers.Application$SimpleUser. It must have a default constructor]
然而,这段代码完美无缺。
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()));
}
看看它是否适合您。有一个带有ObjectNode的.bind()版本,但也有一个版本可以获取地图。