java - 如何使用play框架2绑定(bind)和处理表单中的复选框列表

标签 java forms checkbox playframework playframework-2.0

我使用的是 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/

相关文章:

Django Modelform(带有排除字段)

java - 使用Guava类时出现NoClassDefFoundError,即使Graava包含在Gradle构建文件和类路径中也是如此

java - web.xml 中的内容辅助

javascript - Google Script - 侧边栏按钮不断打开一个新标签

javascript - jqgrid 中的多个复选框列。处理onchange事件

css - 为选中的复选框添加删除线

html - 在 CSS 中的 "checkmark:checked"条件之前选择一个 div

java - 根据 id 和日期以最少的对象数量获取种子

java - 获取触发事件的类 - Java

带有 polyfill 的 html5 表单 - 值得吗?