Wicket和Java:对页面的更改不会持续存在

时间:2011-05-05 17:14:22

标签: java wicket

当谈到Wicket时,我是一个n00b,就像我本周早些时候开始的那样。我想要做的是在页面上显示一个人员列表,其中有单选按钮描述他们的性别。现在,当页面加载时,单选按钮被正确选择,但是当我提交页面时,不会保留更改(如果有的话)。我已经习惯了asp.net MVC,所以我熟悉模型绑定,但是wicket对我来说有点陌生,说实话,这个具体问题没有太多文档。所以,如果你们真的很友好地检查我的代码,我会非常感激。 任何评论和/或修正也将不胜感激。

.java代码

public class FlightInfo extends WebPage 
{
private static final long serialVersionUID = 1L;

private ListView<GuestInfo> guestRecordListView;

public FlightInfo() {
    this.init();
}

private void init() 
{
    FeedbackPanel feedbackPanel = new FeedbackPanel("flightinfo.errormessages");
    add(feedbackPanel);

    final SearchResponse searchResponse = ((DciSession)super.getSession()).getSearchResponse();

    //Data lists
    guestRecordListView = new ListView<GuestInfo>("guestRecordListView", searchResponse.getObj().getGuestList())
    {
        private static final long serialVersionUID = 1L;
        private static final long MALE = 1L;
        private static final long FEMALE = 2L;

        @Override
        protected void populateItem(ListItem<GuestInfo> item)
        {
            item.add(new Label("guestRecordNameListItem", new PropertyModel<String>(item.getModel().getObject(), "Name")));

            String gender = item.getModel().getObject().getGender();

            IModel genderModel = new PropertyModel<String>(item.getModel(), "Gender");

            final RadioGroup<String> guestTypeRadioGroup = new RadioGroup("radio-passengerType", genderModel);
            item.add(guestTypeRadioGroup);

            final Model mModel = new Model(new PersonGenderXRef(item.getModel().getObject(), MALE));
            final Model fModel = new Model(new PersonGenderXRef(item.getModel().getObject(), FEMALE));
            final Model cModel = new Model(new PersonGenderXRef(item.getModel().getObject(), CHILD));

            guestTypeRadioGroup.add(new Radio("radio-male", mModel));
            guestTypeRadioGroup.add(new Radio("radio-female", fModel));
            guestTypeRadioGroup.add(new Radio("radio-child", cModel));

            if(gender.toUpperCase().equals("M"))
            {
                guestTypeRadioGroup.setModel(mModel);
            }
            else 
            {
                if (gender.toUpperCase().equals("F"))
                {
                    guestTypeRadioGroup.setModel(fModel);                   
                }
                else
                {
                    if (gender.toUpperCase().equals("C"))
                    {
                        guestTypeRadioGroup.setModel(cModel);
                    }
                }

            }
        }
    };

    //Buttons
    Button continueButton = new Button("input.submitChanges");

    Form form = new Form("form.reviewAndEditForm")
    {
        private static final long serialVersionUID = 1L;

        @SuppressWarnings("serial")
        protected void onSubmit()
        {       

            try
            {                       
                List<GuestInfo> guests = (List<GuestInfo>) guestRecordListView.getModelObject();
                List<Person> people = new ArrayList<Person>();
                //Construct people list
                for(int i = 0; i < guests.size(); i++)
                {
                    people.add(guests.get(i).getPerson());
                }
                //Submit request
            }
            catch(Exception e)
            {

            }
        }           
    };
    add(form);

    form.add(continueButton);
    form.add(guestRecordListView);
    form.add(flightRecordListView);
}

private final void saveStuff(ListView<GuestInfo> data)
{
    ListView<GuestInfo> dataview2 = data;
}
}

提及的其他课程

public class PersonGenderXRef implements Serializable
{
/**
 * 
 */
private static final long serialVersionUID = 1L;

private GuestInfo guest;
private long gender;

public PersonGenderXRef(GuestInfo guest, long gender)
{
    this.guest = guest;
    this.gender = gender;
}

public GuestInfo guest()
{
    return this.guest;
}

public void setGuest(GuestInfo guest)
{
    this.guest = guest;
}

public long getGender()
{
    return this.gender;
}

public void setGender(long gender)
{
    this.gender = gender;
}
}

这是标记

                    <table id="guestTable">
                        <tr>
                            <th width="20"><!-- <input id="select_all_guests" type="checkbox" /> --></th>
                            <th width="150"><b>Name</b></th>
                            <th colspan="3"><b>Guest Type<span style="color:red">*</span></b></th>
                        </tr>
                        <tr wicket:id="guestRecordListView">
                            <td width="20"><input type="checkbox" /></td>
                            <td width="150"><span wicket:id="guestRecordNameListItem"></span></td>
                            <wicket:container wicket:id="radio-passengerType">
                                <td width="80"><input wicket:id="radio-male" type="radio" />Male</td>
                                <td width="80"><input wicket:id="radio-female" type="radio" />Female</td>
                            </wicket:container>
                        </tr>
                    </table>

1 个答案:

答案 0 :(得分:1)

RadioGroup的模型应该指向持久存储到数据库的底层bean的属性。

例如,假设您有一个根对象的模型,该对象知道如何加载和保持对数据库的更改(或者更改在表单提交中保留):

IModel personModel = new PropertyModel<Person>(item.getModel(), "person");

您希望广播组成为性别属性的人的子属性:

IModel genderModel = new PropertyModel<String>(personModel, "gender")