欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Wicket PropertyModel example wicketpropertymodel 

程序员文章站 2022-03-09 14:07:19
...
转自:http://www.mkyong.com/wicket/wicket-propertymodel-example/

In Wicket, you can use “PropertyModel” class bind form component to a property of a class. See following example to show you how :
1. User class

An User class, with two properties – “name” and “age”.

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 example

Uses “PropertyModel” to bind textbox components to property of “user” object.

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);
 
	}
}


Binds “tName” textbox component to “User” object, “name” property.

final TextField<String> tName = new TextField<String>("name",
	new PropertyModel<String>(user, "name"));


Binds “tNickname” textbox component to current UserPage’s “nickname” property.

final TextField<String> tNickname = new TextField<String>("nickname",
	new PropertyModel<String>(this, "nickname"));