【问题标题】:Wicket - order of adding components in Java matters?Wicket - 在 Java 中添加组件的顺序很重要?
【发布时间】:2014-06-29 19:43:53
【问题描述】:

我创建了一个简单的检票口表单,其中包含一个 DropDownChoice、一个提交按钮和两个 TextField,以便尝试一些模型链接。 html:

    <!DOCTYPE html>
<html xmlns:wicket="http://wicket.apache.org">
    <head>
        <meta charset="utf-8" />
        <title>DropDownTest</title>
    </head>
    <body>      
        <form wicket:id="selectForm">   
            <select wicket:id="dropDown"></select>              
            <input type="submit" wicket:id="bt"/>           
            <input type="text" wicket:id="age"/>
            <input type="text" wicket:id="name"/>           
        </form>             
    </body>
</html>

还有java代码:

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

private class Person implements Serializable {
    private int age;
    private String name;

    public Person(){};

    public Person(int pAge, String pName) {     
        age = pAge;
        name = pName;           
    }

    public int getAge() {return age;}

    public void setAge(int age) {this.age = age;}

    public String getName() {return name;}

    public void setName(String name) {this.name = name;}

}   

public List<Person> getPersons() {

    List<Person> persons = new ArrayList<Person>();
    persons.add(new Person(34, "Hanna"));
    persons.add(new Person(17, "Ivan"));
    persons.add(new Person(64, "Carol"));   
    return persons;
}

private Form form;
private DropDownChoice<Person> dropDown;

public HomePage(final PageParameters parameters) {
    super(parameters);      

    Model<Person> personModel = new Model<Person>();

    dropDown = new DropDownChoice<Person>("dropDown", personModel, getPersons(), 
            new ChoiceRenderer<Person>("name"));

    form = new Form("selectForm");

    form.add(new TextField("name", new PropertyModel(personModel, "name")));
    form.add(new TextField("age", new PropertyModel(personModel, "age")));      
    form.add(dropDown);

    form.add(new Button("bt"));     

    add(form);
}   
}

下拉选项和两个文本字段共享相同的模型(personModel),因此当用户从下拉选项中选择一个人并单击按钮提交表单并重新加载页面时,这两个字段从通过选择的人那里获取它们的值该模型。这按预期工作,我没有收到任何错误。现在,如果我更改将组件添加到表单的顺序(工作):

form.add(new TextField("name", new PropertyModel(personModel, "name")));
form.add(new TextField("age", new PropertyModel(personModel, "age")));      
form.add(dropDown);

到这个(不工作)

form.add(dropDown);
form.add(new TextField("name", new PropertyModel(personModel, "name")));
form.add(new TextField("age", new PropertyModel(personModel, "age")));

提交表单时出现错误:

方法 [public int com.asbjorntest.HomePage$Person.getAge()]。无法将 null 值转换为原始类:int 用于在 com.asbjorntest.HomePage$Person@71460b93 上设置它

我了解错误来自以下事实:我提交的“年龄”文本字段没有任何值,并且无法在我的 Person 类中转换为 int。但是,为什么只有在我在 Java 代码中添加文本字段之前添加 dropdownchoice 时才会发生此错误?或者也许我应该问:为什么在我添加它之后它不会发生?我在 java 代码中将组件添加到表单或页面的顺序是否重要,或者我在这里完全遗漏了什么?

提前感谢任何答案或线索!

【问题讨论】:

    标签: java wicket


    【解决方案1】:

    我认为HTML 中的顺序无关紧要。 Java 中的顺序可能是相关的,因为组件的顺序决定了它们的验证方式。

    当您将DropDownChoice 放入最新时,TextField 中的null age 将被您在下拉列表中选择的值覆盖,并且一切正常。

    如果你把你的组件反过来,你的age将是null(来自TextField的值,你会得到你的错误。

    简而言之:当多个组件编辑同一个模型值时要小心

    【讨论】:

      【解决方案2】:

      据我所知,不鼓励在组件之间共享模型,因此建议为每个组件创建一个新模型。 请注意,当您创建 personModel 最初没有附加具体人员时,我怀疑它是在添加到页面时创建的。因此,在创建文本字段并将其添加到表单时,模型的 person 为 null。

      总而言之,我会向您推荐类似的东西:

      public HomePage(final PageParameters parameters) {
          super(parameters);      
      
          Person person = new Person();
          form = new Form("selectForm");
      
          dropDown = new DropDownChoice<Person>("dropDown", new Model<Person>(person), getPersons(), new ChoiceRenderer<Person>("name"));
              form.add(dropDown);
      
          // I think you can use person as first parameter, else use new Model<Person(person),
          form.add(new TextField("name", new PropertyModel(person, "name")));
          form.add(new TextField("age", new PropertyModel(person, "age")));      
      
          form.add(new Button("bt"));     
      
          add(form);
      } 
      

      无论如何,我已经很久没有使用 Wicket 了,所以我可能完全错了:P

      【讨论】:

      • 好的,谢谢您的回答!我是 Wicket 的新手,我正在遵循 apache 的指南/教程,其中描述了共享模型可用于进行模型链接,其中一个组件的模型结果(在我的情况下为下拉列表)可以是模型-另一个组件的来源。但我想,作为@RobAu cmets,由于验证问题,我必须小心订单。问候!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-10-18
      • 2016-06-19
      • 2022-10-05
      • 1970-01-01
      • 2015-03-14
      • 2021-10-12
      • 2012-09-28
      相关资源
      最近更新 更多