Wicket - order of adding components in Java matters? -
i have created simple wicket form dropdownchoice, submit button , 2 textfields, in order try model-chaining. 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>
and java code:
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); } }
the dropdownchoice , 2 textfields share same model (personmodel), when user selects person dropdownchoice , submits form clicking button , page reloads, 2 fields values person selected via model. works expected , no errors. now, if change order in add components form (working):
form.add(new textfield("name", new propertymodel(personmodel, "name"))); form.add(new textfield("age", new propertymodel(personmodel, "age"))); form.add(dropdown);
to (not working)
form.add(dropdown); form.add(new textfield("name", new propertymodel(personmodel, "name"))); form.add(new textfield("age", new propertymodel(personmodel, "age")));
i error when submit form:
method [public int com.asbjorntest.homepage$person.getage()]. can't convert null value primitive class: int setting on com.asbjorntest.homepage$person@71460b93
i understand error comes fact submitting "age" textfield without value , can't converted int in person class. why error happen if add dropdownchoice before adding textfields in java code? or maybe should ask: why not happen when add after? order in add components form or page in java code matter, or missing here?
thanks in advance answers or clues!
i think order in html
irrelevent. order in java
can relevent, order of components determines how validated.
as put dropdownchoice
latest, null
age
textfield
overridden value selected in dropdown , works fine.
if put components other way around, age
null
(value textfield
, error.
to short: be careful when multiple components edit same model value.
Comments
Post a Comment