Wicket 教程4:如何使用Form 和form组件 2

下面来看一个复杂一点的例子: 使用选择框
java code:
final CheckGroup group = new CheckGroup("group", new ArrayList());
           Form form = new Form("form") {
                 protected void onSubmit() {
                       info("selected person(s): " + group.getModelObjectAsString());
                 }
           };

           add(form);
           form.add(group);
           group.add(new CheckGroupSelector("groupselector"));
           ListView persons = new ListView("persons", getPersons()) {

                 protected void populateItem(ListItem item) {
                       item.add(new Check("checkbox", item.getModel()));
                       item.add(new Label("name", new PropertyModel(item.getModel(),
                                   "name")));
                       item.add(new Label("lastName", new PropertyModel(item
                                   .getModel(), "lastName")));
                 }

           };

           group.add(persons);
           //multiCheckForm
           
           final Input input = new Input();
           Form multiCheckForm = new Form("multiCheckForm",new CompoundPropertyModel(input))
           {
                 protected void onSubmit()
                 {
                       info("input: " + input);
                 }
           };
           add(multiCheckForm);

           // add a couple of checkbox multiple choice components, notice the model
           // used is a compound model set on the page
           CheckBoxMultipleChoice listChoice = new CheckBoxMultipleChoice("sites", SITES);
           multiCheckForm.add(listChoice);

           listChoice = new CheckBoxMultipleChoice("choices", MANY_CHOICES);
           multiCheckForm.add(listChoice);
           
           // radio group
           final RadioGroup radioGroup = new RadioGroup("radioGroup", new Model());
           Form radioGroupForm = new Form("radioGroupForm")
           {
                 protected void onSubmit()
                 {
                       info("selected person: " + radioGroup.getModelObjectAsString());
                 }
           };

           add(radioGroupForm);
           radioGroupForm.add(radioGroup);

           ListView persons2 = new ListView("persons2", getPersons())
           {

                 protected void populateItem(ListItem item)
                 {
                       item.add(new Radio("radio", item.getModel()));
                       item.add(new Label("name", new PropertyModel(item.getModel(), "name")));
                       item.add(new Label("lastName", new PropertyModel(item.getModel(), "lastName")));
                 }

           };

           radioGroup.add(persons2);
     

           add(new FeedbackPanel("feedback"));

HTml code:


<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
  <title>Wicket Examples - component reference</title>
  <link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<body>

     <h1>wicket.markup.html.form.ListChoice</h1>
     <wicket:link><a href="Index.html">[back to the reference]</a></wicket:link>

     <p>
     A CheckBoxGroup and CheckBoxComponnet components let users select multiple values from a group of checkboxes. These components are more flexible then the CheckBoxMultipleChoice component in that individual checkboxes are full components, unlike with CheckBoxMultipleChoice, and thus can be used anywhere in the markup.
     </p>
     <p>
      <form wicket:id="form">
           <span wicket:id="group">
                 <table style="border: 2px dotted #fc0; width: 400px; padding: 5px;">
                  <tr>
                  <td valign="top">Select persons</td>
                  <td>
                        <input type="checkbox" wicket:id="groupselector">check/uncheck all </input> <br/>
                        <table cellspacing="0" cellpadding="2">
                             <tr>
                                   <td><b>Select</b></td>
                                   <td><b>First Name</b></td>
                                   <td><b>Last Name</b></td>
                             </tr>
                             <tr wicket:id="persons">
                                   <td><input type="checkbox" wicket:id="checkbox"/></td>
                                   <td><span wicket:id="name">[this is where name will be]</span></td>
                                   <td><span wicket:id="lastName">[this is where lastname will be]</span></td>
                             </tr>
                        </table>
                        <span valign="top">
                        </span>
                  </td>
                  </tr>
                  <tr>
                  <td colspan="2" align="center">
                    <input type="submit" value="submit" />
                  </td>
                  </tr>
                 </table>
           </span>
      </form>
      <h1> multi Check Form</h1>
      <form wicket:id="multiCheckForm">

           <table style="border: 2px dotted #fc0; width: 400px; padding: 5px;">
            <tr>
            <td valign="top">Select your favorite sites</td>
            <td>
                 <span wicket:id="sites">
                 </span>
            </td>
            </tr>
            <tr>
            <td valign="top">Select some of these excellent choices</td>
            <td>
              <span wicket:id="choices">
              </span>
            </td>
            </tr>
            <tr>
            <td colspan="2" align="center">
              <input type="submit" value="submit" />
            </td>
            </tr>
           </table>

      </form>
      <h1> Radio Group</h1>
      <form wicket:id="radioGroupForm">
           <span wicket:id="radioGroup">
                 <table style="border: 2px dotted #fc0; width: 400px; padding: 5px;">
                  <tr>
                  <td valign="top">Select a person</td>
                  <td>
                        <table cellspacing="0" cellpadding="2">
                             <tr>
                                   <td><b>Select</b></td>
                                   <td><b>First Name</b></td>
                                   <td><b>Last Name</b></td>
                             </tr>
                             <tr wicket:id="persons2">
                                   <td><input type="radio" wicket:id="radio"/></td>
                                   <td><span wicket:id="name">[this is where name will be]</span></td>
                                   <td><span wicket:id="lastName">[this is where lastname will be]</span></td>
                             </tr>
                        </table>
                        <span valign="top">
                        </span>
                  </td>
                  </tr>
                  <tr>
                  <td colspan="2" align="center">
                    <input type="submit" value="submit" />
                  </td>
                  </tr>
                 </table>
           </span>
      </form>
     
      <span wicket:id="feedback">feedbackmessages will be put here</span>
</body>
</html>
Wicket 教程4:如何使用Form 和form组件 2
Wicket 教程4:如何使用Form 和form组件 2