播放2.x:包含模板列表的绑定模型

时间:2014-11-25 16:56:05

标签: java scala playframework

我有两种型号用户和地址。

public class User extends Model{
   public String name;
   public List<Address> addresses;
}

public class Address extends Model{
   public String street;
   public String house;
}

我有一个模板来添加用户。

@(form: Form[User])

@helper.form(action=routes.User.add())
{
   <label>Add User<label>
   @text(form("name"), label="Name")
   <input type="submit" class="btn btn-primary" value="Save" />
}

在我的控制器中,我有add方法。

public static Results add()
{
   Form<User> filledForm = userForm.bindFromRequest();
   User user = filledForm.get();
}

我已经跳过了错误处理等不必要的信息。 现在我该如何绑定地址?我希望能够在表单中添加多个地址。

在Play 1.1中,我看到过像POJO系列这样的东西,但我使用的是2.3。

2 个答案:

答案 0 :(得分:1)

正如评论中所提到的,JavaScript和AJAX可能是最舒适的解决方案,您可以创建一个非活动的用户,并仅在以后添加至少一个地址时激活他。

其他解决方案可以使用包含来自(用户和地址)字段的自定义类,因此在验证所有必填字段后,您可以创建用户和相关地址。

样品

用户型号:

public class User extends Model {

    public String firstName;
    public String lastName;

    @OneToMany(mappedBy = "user")
    public List<Address> addresses; // This field doesn't keep relations it informs that relation is kept in `user` field of `Address` model

}

地址型号:

public class Address extends Model {

    @ManyToOne
    public User user;

    public String street;
    public String house;

}

UserAdmin 控制器:

public static class UserWithAddress { //This is our class for validating data for user and his `primary` address

    // User's fields
    @Required
    public String firstName;

    @Required
    public String lastName;

    // Address' fields
    @Required
    public String street;

    @Required
    public String house;

}

public static Result addUserWithAddress() {
    Form<UserWithAddress> userForm = Form.form(UserWithAddress.class);
    return ok(userWithAddressView.render(userForm));
}

public static Result saveUserWithAddress() {
    Form<UserWithAddress> userForm = Form.form(UserWithAddress.class).bindFromRequest();

    if (userForm.hasErrors()) {
        return badRequest(userWithAddressView.render(userForm));
    }

    // form is valid, let's extract data from form, create and save both objects
    UserWithAddress data = userForm.get();

    User user = new User();
    user.firstName = data.firstName;
    user.lastName = data.lastName;
    user.save(); // Save new user before creating new Address, cause we need a new ID to associate...

    Address address = new Address();
    address.user = user;
    address.street = data.street;
    address.house = data.house;
    address.save();

    return ok("Hooray you're registered now! And we know where you live ;)");
}

userWithAddressView.scala.html 视图

@(userForm: Form[controllers.UsersAdmin.UserWithAddress])

@main(title = "Input your basic data and address data") {

    @helper.form(action=routes.UsersAdmin.saveUserWithAddress()){

        @helper.inputText(userForm("firstName"))
        @helper.inputText(userForm("lastName"))
        @helper.inputText(userForm("street"))
        @helper.inputText(userForm("house"))
        <input type="submit" value="Save"/>

    }

}

答案 1 :(得分:1)

所以我使用@helper.repeat构建它。最好的例子是Play提供的样本。它被称为表格。

有关快速参考,可在此处找到说明 - https://www.playframework.com/documentation/2.2.x/JavaFormHelpers

@helper.inputText(userForm("name"))

@helper.repeat(userForm("emails"), min = 1) { emailField =>

    @helper.inputText(emailField)

}

对于地址示例:

@(form: Form[User])

@helper.form(action=routes.User.add())
{
   <label>Add User<label>
   @text(form("name"), label="Name")

   @helper.repeat(form("addresses"), min= 1){ address =>
      @helper.inputText(address("street"))
      @helper.inputText(address("house"))      
   }
   <input type="submit" class="btn btn-primary" value="Save" />
}

如果您在streethouse上添加所需的注释,验证将正常工作。您唯一需要做的就是在地址列表中添加@Validjavax.validation.Valid)注释。

@Valid
public List<Address> addresses;

要添加和删除地址,最好查看样本。