开发者

Wicket and Java : Changes to page do not persist

开发者 https://www.devze.com 2023-03-02 22:23 出处:网络
I am a n00b when it comes to Wicket, as i started earlier this week. What i am trying to do is display a list of people on a page with radio buttons describing their gender. Now the radio buttons are

I am a n00b when it comes to Wicket, as i started earlier this week. What i am trying to do is display a list of people on a page with radio buttons describing their gender. Now the radio buttons are selected properly when the page loads, however when i submit the page, the changes (if any are made) are not persisted. I am used to asp.net MVC so i am familiar with Model Binding, but wicket is kind of alien to me and to be honest, there isnt much documentation relating to this specific problem. So if you guys would be so kind as to check over my code i would very much appreciate it. Any comments and/or fixes would also be appreciated.

.java code

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;
}
}

Other class that is mentioned

public class PersonGenderXRef implem开发者_JS百科ents 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;
}
}

And here is the markup

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


The model of the RadioGroup should point to the property on an underlying bean which is persisted to the database.

For example, suppose you have a model for a root object which knows how to load and persist changes to the database (or the changes are persisted on form submit):

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

You want the radio group to be a sub-property of the person which is the gender property:

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

精彩评论

暂无评论...
验证码 换一张
取 消