Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/372.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.0中绑定复杂类型_Java_Playframework 2.0 - Fatal编程技术网

Java 如何在play framework 2.0中绑定复杂类型

Java 如何在play framework 2.0中绑定复杂类型,java,playframework-2.0,Java,Playframework 2.0,我有一个模型类,其结构如下: public class User { public String name; public Long id; } public class Play { public String name; public User user; } 现在我想要一个基于Play类的表单。因此,我有一个editPlay视图,它以Form[Play]作为输入。 在视图中,我有一个表单,在提交时调用更新操作: @form (routes.PlayControl

我有一个模型类,其结构如下:

public class User {
   public String name;
   public Long id;
}

public class Play {
   public String name;
   public User user;
}
现在我想要一个基于Play类的表单。因此,我有一个
editPlay
视图,它以
Form[Play]
作为输入。 在视图中,我有一个表单,在提交时调用更新操作:

@form (routes.PlayController.update()) 
{..}
但我无法找到正确的方法来绑定用户字段,以便在控制器中正确接收它:

Form<Play> formPlay = form(Play.class).bindFromRequest();
Play playObj = formPlay.get();
formPlay=Form(Play.class).bindFromRequest();
Play playbj=formPlay.get();
根据,
Form.Field
值始终是字符串。是否有其他方法自动将输入绑定到用户对象


谢谢

我不太确定我是否理解你的问题,但基本上我一直在处理这样的表格:

 final static Form<Play> playForm = form(Play.class);
 ...
 public static Result editPlay(){
     Form<Play> newPlayForm = form(User.class).bindFromRequest();
     Play newPlay = newPlayForm.get();
     ....    
 }
然后在模板中:

 @(playForm: Form[Play])
 @import helper._

 @helper.form(action = routes.Application.editPlay()) {
      @helper.inputText(playForm("name"))
      ...
 }

您可以使用自定义
DataBinder
在play.scla.html中:

@form (routes.PlayController.update()) 
{
  <input type="hidden" name="user" id="user" value="@play.user.id"/>
}
@表单(routes.PlayController.update())
{
}
在控制器中的方法中

public static Result update()
{
  // add a formatter which takes you field and convert it to the proper object
  // this will be called automatically when you call bindFromRequest()

  Formatters.register(User.class, new Formatters.SimpleFormatter<User>(){
    @Override
    public User parse(String input, Locale arg1) throws ParseException {
      // here I extract It from the DB
      User user = User.find.byId(new Long(input));
      return user;
    }

    @Override
    public String print(User user, Locale arg1) {
      return user.id.toString();
    }
  });
  Form<Play> formPlay = form(Play.class).bindFromRequest();
  Play playObj = formPlay.get();
}
公共静态结果更新()
{
//添加一个格式化程序,将字段转换为适当的对象
//调用bindFromRequest()时,将自动调用此函数
register(User.class,new Formatters.SimpleFormatter()){
@凌驾
公共用户解析(字符串输入,区域设置arg1)引发ParseException{
//这里我从数据库中提取它
User=User.find.byId(新长(输入));
返回用户;
}
@凌驾
公共字符串打印(用户,区域设置arg1){
返回user.id.toString();
}
});
Form formPlay=Form(Play.class).bindFromRequest();
Play playbj=formPlay.get();
}

Form类最近发生了一些变化,您可能需要查看新文档。是否可以将register调用放在全局对象或类似的地方,这样就不需要在每个控制器中使用此样板代码?请注意,在调用Form.Form(…).bindFromRequest()之前,您必须调用Formatters.register(…)如果希望此格式化程序正常工作。在Play 2.5中,Formatters.register方法现在是非静态的。请参阅文档以了解在这种情况下如何处理它。如果没有
static
,我如何处理它?我认为您应该使用editPlay方法(而不是Play.class)编写表单(Play.class)
public static Result update()
{
  // add a formatter which takes you field and convert it to the proper object
  // this will be called automatically when you call bindFromRequest()

  Formatters.register(User.class, new Formatters.SimpleFormatter<User>(){
    @Override
    public User parse(String input, Locale arg1) throws ParseException {
      // here I extract It from the DB
      User user = User.find.byId(new Long(input));
      return user;
    }

    @Override
    public String print(User user, Locale arg1) {
      return user.id.toString();
    }
  });
  Form<Play> formPlay = form(Play.class).bindFromRequest();
  Play playObj = formPlay.get();
}