在Wicket中,您可以使用“ PropertyModel ”类将表单组件绑定到类的属性。 请参阅以下示例,向您展示如何:
1.用户类别
一个用户类,具有两个属性-“名称”和“年龄”。
package com.mkyong.user;
import java.io.Serializable;
public class User implements Serializable{
private String name;
private int age;
//setter and getter methods
}
2. PropertyModel示例
使用“ PropertyModel ”将文本框组件绑定到“ user ”对象的属性。
package com.mkyong.user;
import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.model.PropertyModel;
public class UserPage extends WebPage {
private User user = new User();
private String nickname;
public UserPage(final PageParameters parameters) {
add(new FeedbackPanel("feedback"));
final TextField<String> tName = new TextField<String>("name",
new PropertyModel<String>(user, "name"));
final TextField<Integer> tAge = new TextField<Integer>("age",
new PropertyModel<Integer>(user, "age"));
final TextField<String> tNickname = new TextField<String>("nickname",
new PropertyModel<String>(this, "nickname"));
Form<?> form = new Form<Void>("userForm") {
@Override
protected void onSubmit() {
PageParameters pageParameters = new PageParameters();
pageParameters.add("name", user.getName());
pageParameters.add("age", Integer.toString(user.getAge()));
pageParameters.add("nickname", nickname);
setResponsePage(SuccessPage.class, pageParameters);
}
};
add(form);
form.add(tName);
form.add(tAge);
form.add(tNickname);
}
}
将“ tName”文本框组件绑定到“ User”对象,“ name”属性 。
final TextField<String> tName = new TextField<String>("name",
new PropertyModel<String>(user, "name"));
将“ tNickname”文本框组件绑定到当前UserPage的“昵称”属性 。
final TextField<String> tNickname = new TextField<String>("nickname",
new PropertyModel<String>(this, "nickname"));
下载它– Wicket-PropertyModel-Examples.zip (10KB)
参考文献
标签: 检票口
翻译自: https://mkyong.com/wicket/wicket-propertymodel-example/