I have a page that has a html components like a select list and radio
buttons. And those components will invoke the onchange event to submit
the form or move to the next page.
It doesn't look like I am moving to the next page. My "model" values
dont' seem to be retained, at least they aren't retained in for the
radio buttons, text boxes.
For example, I was thinking like Struts or Spring, I would call
"onchange" on a radio button, the page would reload and all of my values
would be retained. The values are NOT retained.
Here are two radio buttons. I want to select one, do an onchange and
have retain the value of radiogroup2. That is not happening:
Also, I am losing the values of my textfields.
What would I need to to keep the values of the widgets regardless if I
submit the page or do an onchange.
Source in a paste bin:
http://paste.lisp.org/display/112321
=========
Snippet, two radio buttons. Radio two loses its values.
final RadioChoice radioGroupYesNo2 = new RadioChoice("radioGroup2", new
PropertyModel(bean, "yesNo2"), Arrays.asList(YES_NO)) {
@Override
public boolean wantOnSelectionChangedNotifications() {
return true;
}
@Override
public void onSelectionChanged(Object newSel) {
this.setResponsePage(TestPage.class);
}
@Override
public boolean isVisible() {
return true;
}
}; // End of add radio group //
radioGroupYesNo2.setPrefix("");
radioGroupYesNo2.setSuffix("");
form.add(radioGroupYesNo2);
// Add Link //
this.add(new SubmitLink("linkSubmit", form) {
@Override
public void onSubmit() {
// Here what is the difference between the Class and the Object
this.setResponsePage(TestPage.class);
}
});
}
public final DropDownChoice createDropDown(final String str) {
// Add list select elements.
final ChoiceRenderer choiceRenderer = new ChoiceRenderer("name",
"value");
final List<Select> list1 = new ArrayList<Select>();
list1.add(new Select("select1", "select1"));
list1.add(new Select("select2", "select2"));
final DropDownChoice dropDown = new DropDownChoice(str, list1,
choiceRenderer) {
private static final long serialVersionUID = 3169945459254179351L;
@Override
protected boolean wantOnSelectionChangedNotifications() {
return false;
}
@Override
protected void onSelectionChanged(Object newSelection) {
this.setResponsePage(TestPage.class);
}
};
dropDown.setRequired(true);
return dropDown;
}
==============
Full Source:
/**
* File: TestPage.java
*/
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.ChoiceRenderer;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.RadioChoice;
import org.apache.wicket.markup.html.form.SubmitLink;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.PropertyModel;
import TestBean.Select;
public class TestPage extends WebPage {
public static final String [] YES_NO = {
"Yes", "No"
};
@SuppressWarnings("unchecked")
public TestPage(final PageParameters parameters) {
super(parameters);
TestBean bean = new TestBean();
if (WicketSession.get().getTestBean() == null) {
bean = new TestBean();
WicketSession.get().setTestBean(bean);
} else {
bean = WicketSession.get().getTestBean();
}
final Form<TestBean> form = new Form<TestBean>("form", new
CompoundPropertyModel(bean));
this.add(form);
form.add(new TextField("firstName"));
form.add(new TextField("lastName"));
form.add(createDropDown("selectBox1"));
form.add(createDropDown("selectBox2"));
final RadioChoice radioGroupYesNo = new RadioChoice("radioGroup1", new
PropertyModel(bean, "yesNo1"), Arrays.asList(YES_NO)) {
@Override
public boolean wantOnSelectionChangedNotifications() {
return true;
}
@Override
public void onSelectionChanged(Object newSel) {
this.setResponsePage(TestPage.class);
}
@Override
public boolean isVisible() {
return true;
}
}; // End of add radio group //
radioGroupYesNo.setPrefix("");
radioGroupYesNo.setSuffix("");
form.add(radioGroupYesNo);
final RadioChoice radioGroupYesNo2 = new RadioChoice("radioGroup2",
new PropertyModel(bean, "yesNo2"), Arrays.asList(YES_NO)) {
@Override
public boolean wantOnSelectionChangedNotifications() {
return true;
}
@Override
public void onSelectionChanged(Object newSel) {
this.setResponsePage(TestPage.class);
}
@Override
public boolean isVisible() {
return true;
}
}; // End of add radio group //
radioGroupYesNo2.setPrefix("");
radioGroupYesNo2.setSuffix("");
form.add(radioGroupYesNo2);
// Add Link //
this.add(new SubmitLink("linkSubmit", form) {
@Override
public void onSubmit() {
// Here what is the difference between the Class and the Object
this.setResponsePage(TestPage.class);
}
});
}
public final DropDownChoice createDropDown(final String str) {
// Add list select elements.
final ChoiceRenderer choiceRenderer = new ChoiceRenderer("name",
"value");
final List<Select> list1 = new ArrayList<Select>();
list1.add(new Select("select1", "select1"));
list1.add(new Select("select2", "select2"));
final DropDownChoice dropDown = new DropDownChoice(str, list1,
choiceRenderer) {
private static final long serialVersionUID = 3169945459254179351L;
@Override
protected boolean wantOnSelectionChangedNotifications() {
return false;
}
@Override
protected void onSelectionChanged(Object newSelection) {
this.setResponsePage(TestPage.class);
}
};
dropDown.setRequired(true);
return dropDown;
}
} // End of the Class //
public class TestBean implements Serializable {
private String firstName = "";
private String lastName = "";
private String yesNo1 = "";
private String yesNo2 = "";
private String yesNo3 = "";
private Select selectBox1;
private Select selectBox2;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public static class Select {
private String name = "";
private String value = "";
public Select(String a, String b) {
name = a;
value = b;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
public String getYesNo1() {
return yesNo1;
}
public void setYesNo1(String yesNo1) {
this.yesNo1 = yesNo1;
}
public String getYesNo2() {
return yesNo2;
}
public void setYesNo2(String yesNo2) {
this.yesNo2 = yesNo2;
}
public String getYesNo3() {
return yesNo3;
}
public void setYesNo3(String yesNo3) {
this.yesNo3 = yesNo3;
}
public Select getSelectBox1() {
return selectBox1;
}
public void setSelectBox1(Select selectBox1) {
this.selectBox1 = selectBox1;
}
public Select getSelectBox2() {
return selectBox2;
}
public void setSelectBox2(Select selectBox2) {
this.selectBox2 = selectBox2;
}
}
=============
<html>
<body>
<form wicket:id="form">
<input wicket:id="firstName" />
<br />
<input wicket:id="lastName" />
<br />
<select wicket:id="selectBox1"><option /></select>
<br />
<select wicket:id="selectBox2"><option /></select>
<br />
<table><tr>
<td>Radio 1</td>
<td>
<wicket:container wicket:id="radioGroup1">
<input type="radio">y</input>
<input type="radio">n</input>
</wicket:container>
</td>
</tr></table>
<br />
<table><tr>
<td>Radio 2</td>
<td>
<wicket:container wicket:id="radioGroup2">
<input type="radio">y</input>
<input type="radio">n</input>
</wicket:container>
</td>
</tr></table>
</form>
<a wicket:id="linkSubmit">Submit Link</a>
</body>
</html>
Berlin Brown