Wicket PropertyModel example

阅读更多
转自: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 tName = new TextField("name",
				new PropertyModel(user, "name"));
 
		final TextField tAge = new TextField("age",
				new PropertyModel(user, "age"));
 
		final TextField tNickname = new TextField("nickname",
				new PropertyModel(this, "nickname"));
 
		Form form = new Form("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 tName = new TextField("name",
	new PropertyModel(user, "name"));


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

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

你可能感兴趣的:(wicket,propertymodel)