gpt4 book ai didi

java - Play 2.x : Binding model which contains a list to the template

转载 作者:行者123 更新时间:2023-11-30 03:39:21 24 4
gpt4 key购买 nike

我有两个模型“用户”和“地址”。

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" />
}

在我的 Controller 中,我有 add 方法。

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

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

在 Play 1.1 中,我见过 POJO 集合之类的东西,但我正在使用 2.3。

最佳答案

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

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

示例

用户模型:

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 Controller :

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 View

@(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"/>

}

}

关于java - Play 2.x : Binding model which contains a list to the template,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27132411/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com