Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Play Framework 2:如何从控制器中的各种源正确构造对象,然后验证它们?_Java_Json_Validation_Playframework_Playframework 2.2 - Fatal编程技术网

Java Play Framework 2:如何从控制器中的各种源正确构造对象,然后验证它们?

Java Play Framework 2:如何从控制器中的各种源正确构造对象,然后验证它们?,java,json,validation,playframework,playframework-2.2,Java,Json,Validation,Playframework,Playframework 2.2,对于本例,用户希望为其站点创建一篇博客文章。表单提交是通过AJAX处理的。POST请求由控制器中的createPost方法处理。该方法提取json数据并将其与用户会话数据相结合,以构造适当的Post对象。然后,它验证数据并返回适当的响应 邮政模式如下: @Entity public Post extends model { @Id public Long id; @Required public User author; @Required public String t

对于本例,用户希望为其站点创建一篇博客文章。表单提交是通过AJAX处理的。POST请求由控制器中的createPost方法处理。该方法提取json数据并将其与用户会话数据相结合,以构造适当的Post对象。然后,它验证数据并返回适当的响应

邮政模式如下:

@Entity
public Post extends model {
  @Id
  public Long id;
  @Required
  public User author;
  @Required
  public String title;
  @Required
  public String body;
}
@BodyParser.Of(BodyParser.Json.class)
public static Result createPost() {
  JsonNode json = request().body().asJson();
  Post post = new Post();

  post.author = User.findbyId(request().username());
  post.title = json.findPath("title").textValue();
  post.body = json.findPath("body").textValue();

  Form<Post> filledForm = Form.form(Post.class).bind(Json.toJson(post));

  if (filledForm.hasErrors())
    return badRequest(filledForm.errorsAsJson());

  // save post

  return ok();  
}
控制器方法如下所示:

@Entity
public Post extends model {
  @Id
  public Long id;
  @Required
  public User author;
  @Required
  public String title;
  @Required
  public String body;
}
@BodyParser.Of(BodyParser.Json.class)
public static Result createPost() {
  JsonNode json = request().body().asJson();
  Post post = new Post();

  post.author = User.findbyId(request().username());
  post.title = json.findPath("title").textValue();
  post.body = json.findPath("body").textValue();

  Form<Post> filledForm = Form.form(Post.class).bind(Json.toJson(post));

  if (filledForm.hasErrors())
    return badRequest(filledForm.errorsAsJson());

  // save post

  return ok();  
}

现在,这个方法可以工作了,但是,必须有更好的方法来完成它,而不是接受json请求,将其提取到一个对象中,然后将该对象转换回json,以便将其绑定到表单。有什么想法吗?

通常的方法是在控制器端使用bindFromRequest

public static Result savePost() {
    Form<Post> postForm= form(Post .class).bindFromRequest();
    if(postForm.hasErrors()) {
        Logger.error("Form has errors: " + postForm.errorsAsJson());
        return badRequest(filledForm.render(postForm));
   }
   Post post=postForm.get();
   post.save()
   ....
}

表单类不应该真的是域类,这是一个经常讨论的问题。仅在简单情况下有效


另一种方法是创建表单数据类并绑定该类。表单数据类可以包含来自许多不同模型的数据。然后让代码从表单数据对象加载域对象Post副本字段。我使用表单数据到域对象代码的代码生成。YMMV.

谢谢你的回答,但这根本不能回答问题。如果处理包含所有模型字段的提交表单,那么,是的,标准的.bindingFromRequest方法将是理想的。然而,在本例中,问题是如何组合来自不同来源的字段,而不仅仅是表单,然后验证它们。