我使用的是 Play 框架 2.2。我有一个关于表单中的多个复选框以及如何正确绑定(bind)和处理它们的问题。我正在寻找 Java 解决方案。
假设我们有一个博客网站。我们希望实现这样的功能:当用户创建/编辑博客条目时,他们可以选择向该博客条目添加零到多个标签。这些标签选项在表单上显示为一长串复选框。例如,如果他/她创建了博客条目“业余时间在月球上要做的 10 件事”,并希望将其与“观星”、“散步”和“高尔夫”标签相关联,他/她会只需在创建过程中选中相应的复选框即可。
我不确定如何将模型绑定(bind)到 View 或提交请求后如何处理复选框列表。这是一个实验代码示例。
首先我们有模型:(它们是 ebean 实体)
public class Blog extends Model {
...
@ManyToMany(cascade=CascadeType.REMOVE)
public List<Tag> tags = new ArrayList<>();
...
}
public class Tag extends Model {
@Id
public Long id;
public String name;
...
}
在 View 中,我们将完整的标签列表传递给 View : (不知道这个方法是否正确)
@(tags: java.util.List[Tag], blogEntryForm: Form[Blog])
...
@form(routes.Application.create(), 'id -> "blogEntryForm") {
...
@for((tag, index) <- tags.zipWithIndex) {
<div class="checkbox">
<label>
<input type="checkbox" name="tags[@index]" value="@tag.id">
@tag.name
</label>
</div>
}
...
}
在 Controller 中,我们处理表单:
public static Result createBlogEntry() {
Form<Blog> filledForm = Form.form(Blog.class).bindFromRequest();
if (filledForm.hasErrors()) {
...
}
// process checkboxes
???
}
任何帮助将不胜感激。
最佳答案
这似乎有效。
@(tags: java.util.List[Tag], blogEntryForm: Form[Blog])
@helper.form(routes.Application.createBlogEntry(), 'id -> "blogEntryForm") {
@for((tag, index) <- tags.zipWithIndex) {
<div class="checkbox">
<label>
<input type="checkbox" name="tags[@index].id" value="@tag.id">
@tag.name
</label>
</div>
}
<input type="submit" />
}
在我的 Controller 中我有
public static Result renderTestForm() {
Tag tag1 = new Tag();
tag1.id = 1L;
tag1.name = "tag1";
Tag tag2 = new Tag();
tag2.id = 2L;
tag2.name = "tag2";
List<Tag> tagList = new ArrayList<>();
tagList.add(tag1);
tagList.add(tag2);
Form<Blog> blogForm = Form.form(Blog.class);
return ok(views.html.testForm.render(tagList, blogForm));
}
public static Result createBlogEntry() {
Form<Blog> filledForm = Form.form(Blog.class).bindFromRequest();
if (filledForm.hasErrors()) {
return ok();
}
// process checkboxes
Blog b = filledForm.get();
b.save();
play.Logger.debug(filledForm.toString());
play.Logger.debug(filledForm.get().toString());
List<Blog> bList = Blog.finder.all();
List<Tag> tList = Tag.finder.all();
play.Logger.debug("********");
play.Logger.debug(bList.toString());
play.Logger.debug(tList.toString());
play.Logger.debug("********");
return ok();
}
这只是为了测试整个事情是否有效,因此我为 View 中的复选框创建两个标签并渲染它们。 createBlogEntry
方法仅生成调试输出,以确保数据已成功绑定(bind)到 Blog
对象。
我的路线文件有这些。
GET /testForm controllers.Application.renderTestForm
POST /testForm controllers.Application.createBlogEntry
这是我的模型。
@Entity
@Table(name = "blogs")
public class Blog extends Model {
@Id
public Long id;
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "blog_tags")
public List<Tag> tags = new ArrayList<>();
public String toString() {
String s = "";
for (Tag t : tags) {
s += " " + t.toString();
}
return s;
}
public static final Finder<Long, Blog> finder = new Finder<>(Long.class, Blog.class);
}
@Entity
@Table(name = "tags")
public class Tag {
@Id
public Long id;
public String name;
@ManyToMany(mappedBy = "tags")
public List<Blog> blogs = new ArrayList<>();
public static final Model.Finder<Long, Tag> finder = new Model.Finder<>(Long.class, Tag.class);
}
我的代码中没有注释,因为我在测试它是否有效时没有使用数据库。
关于java - 如何使用play框架2绑定(bind)和处理表单中的复选框列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23204008/