ArrayList的大小始终返回零

时间:2014-04-19 14:04:03

标签: java arraylist playframework playframework-2.0

美好的一天!我正在使用Play框架,我有models的这个类 People.java

package models;

import java.util.*;

import javax.persistence.*;
import javax.validation.*;

import play.data.Form;
import play.data.validation.Constraints.MaxLength;
import play.data.validation.Constraints.*;
import play.db.ebean.*;
import play.db.ebean.Model.Finder;

@Entity
public class People extends Model{

    @Id
    public Long id;

    @Required @MaxLength(100) @Column(unique=true)
    public String label;

    @MaxLength(200)
    public String notes;

    public List<RelatedPeople> relatedPeoples = new ArrayList<RelatedPeople>();

    public static Finder<Long, People> find = new Finder(Long.class, People.class);

    public People() {}

    public People(Long id, String label, String notes, RelatedPeople... relatedPeoples) {
        this.id = id;
        this.label = label;
        this.notes = notes;
        this.relatedPeoples = new ArrayList<RelatedPeople>();
        for(RelatedPeople relatedPeople: relatedPeoples) {
            this.relatedPeoples.add(relatedPeople);
        }
    }

    public static List<People> all() {

        return find.all();
    }

    public static void create(People people){
        people.save();
    }

    public static void delete(Long id){
        find.ref(id).delete();
    }

    public static void update(Long id, People people) {
        people.update(id); // updates this entity, by specifying the entity ID
    }


    public static class RelatedPeople {

        @Required
        public People people;

        public String relationship;

        public RelatedPeople() {}

        public RelatedPeople(People people, String relationship) {
            this.people = people;
            this.relationship = relationship;
        }

    }

}

我有这个表单要求People的详细信息,其中包括labelnotesrelatedPeoples(也是People 1}})和relationship与相关人员。
使用复选框检查relatedPeoples,该复选框应形成所选项目的ArrayList summary.scala.html

@(peoples: List[People], peopleForm: Form[People])

@import helper._
@import helper.twitterBootstrap._

@title = {
    Add a new people 
}

@relatedPeopleGroup(field: Field, className: String = "profile") = {
    <div class="twipsies well @className">
        <table>
        @for(i <- 0 to peoples.length - 1) {
            @defining(i) {count =>
                @defining(peoples(i)) { t =>
                    <tr>

                        <td> @checkbox(
                                peopleForm("peoples[" + count + "]"),
                                '_label -> None, '_text -> t.label
                            )

                        <td> 

                        <td> @select(
                                peopleForm("peoples[" + count + "]"),
                                options(Seq("parent", "child", "peer")),
                                '_label -> "Relationship"
                            )

                    </tr>

                }
            }
        }
        </table>

    </div>
}

@main(title, nav = "people") {

    @if(peopleForm.hasErrors) {
        <div class="alert-message error">
            <p>Required fields</p>
        </div>
    }

    @helper.form(action = routes.Peoples.submit(), 'id -> "form") {

        <fieldset>
            <legend>Add People</legend>

            @inputText(
                peopleForm("label"), 
                '_label -> "Name"
            )

            @textarea(
                peopleForm("notes"), 
                '_label -> "Notes"
            )

        </fieldset>

        @if(peoples.size()>1) {
        <fieldset>
            <legend>Related Peoples</legend>

            <div id="profiles">

                @repeat(peopleForm("relatedPeoples")) { relatedPeople =>

                    @relatedPeopleGroup(relatedPeople)

                }

                @**
                 * Keep an hidden block that will be used as template for Javascript copy code
                 **@
                @relatedPeopleGroup(
                    peopleForm("relatedPeoples[x]"),
                    className = "profile_template"
                )


            </div>

        </fieldset>
        }

        <div class="actions">
            <input type="submit" class="btn primary" value="Add People">
            <a href="@routes.Application.index()" class="btn">Cancel</a>
        </div>

    }

提交表单后,会将其重定向到此页面。

@(people: People) 

@main(Html("People Information"), nav = "people") {

    <h2>@people.label</h2> <br>
    <h3>@people.notes</h3>

    <h4>Related Peoples:</h4>
    @people.relatedPeople.size()

    @for(rp <- people.relatedPeoples){
        @rp.people.label
    }



}

即使我确定检查了其他People,尺寸也始终显示为零。

在控制器中,它处理表单提交。

public static Result submit() {
        Form<People> filledForm = peopleForm.bindFromRequest();
        if(filledForm.hasErrors()) {
            return badRequest(form.render(People.all(), filledForm));
        } else {
            People.create(filledForm.get());
            People created = filledForm.get();
            return ok(summary.render(created));
        }
    }


为什么尺寸始终为零? views部分(表单编码的地方)有问题吗?或者问题出在表单提交处理程序(controller)中?请帮我。我是使用此框架的新手,我将非常感谢您的帮助。非常感谢你!

修改

form中,我进行了修改。

<td> @checkbox(
       tagForm("relatedTags[" + count + "].tag.label"),
        '_label -> None, '_text -> t.label
        )

<td> 

<td> @select(
      tagForm("relatedTags[" + count + "].relationship"),
        options(Seq("parent", "child", "peer")),
            '_label -> "Relationship"
        )


并删除for中的summary.scala.html循环,因为它返回空指针异常。现在,在添加了一个人及其相关人员之后,它会返回有多少人,而不是已经检查过的相关人员。所以我认为问题在于提交处理?

0 个答案:

没有答案