Apache wicket property model example code

In my earlier wicket tutorial, we have seen that how to design a wicket web form using TextField component. In this tutorial, let us see how to integrate model (JavaBean) with forms in wicket application. Wicket provides many model classes.

1. org.apache.wicket.model.Model
2. org.apache.wicket.model.PropertyModel
3. org.apache.wicket.model.CompoundPropertyModel.

org.apache.wicket.model.PropertyModel is a special model class which binds a property (field) with form components like TextFiled , label and etc… The form components which are bound through property model, are updated automatically when the value of the JavaBean field values are changed. Now let us design a simple registration form which displays current date with time and accepts name and email.

You can bind a javabean(regBean) property “name” with a TextField in form using wicket PropertyModel with below line

TextField name = new TextField("name",new PropertyModel(regBean, "name"));

The complete java code using wicket PropertyModel is given below

RegBean.java

package com.javaonline;

import java.util.Date;

public class RegBean {
private String regDate="";
private String name="abc";
private String emailId="abc@xyz.com";

public String getRegDate() {
return new Date().toString();
}
public void setRegDate(String regDate) {
this.regDate = regDate;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmailId() {
return emailId;
}
public void setEmailId(String emailId) {
this.emailId = emailId;
}

}

RegistrationInputPage.java

package com.javaonline;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.request.mapper.parameter.PageParameters;

	public class RegistrationInputPage extends WebPage {
		Form<?> form = null;
		RegBean regBean= new RegBean();

		public RegistrationInputPage(final PageParameters page) throws Exception {
		    super();
		   	Label regDate = new Label("regDate",new PropertyModel(regBean, "regDate"));
		    TextField name = new TextField("name",new PropertyModel(regBean, "name"));
			TextField emailId = new TextField("emailId", new PropertyModel(regBean, "emailId"));
			name.setRequired(true);
			emailId.setRequired(true);

			form = new Form("regForm") {

				public void onSubmit() {

					page.add("name", regBean.getName());
					page.add("email", regBean.getEmailId());
					page.add("regDate",regBean.getRegDate());
					try {
						setResponsePage(new Acknowledgement(page));
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
				}

			};

					form.add(regDate);
					form.add(name);
					form.add(emailId);
					add(form);

		}
	} 

All the fields (regDate, name, emailId) in the java bean (RegBean) are bound with the form components Label and TextFields. So when the form is loaded, all the components are filled with default values automatically.

The corresponding RegistratonInputPage.html is given below

<html>
	<head>
  		<title>Registration Form</title>
	</head>
	<body>
		<form id="regForm1" method="get" wicket:id="regForm">
  			<fieldset>
    			<legend style="color: #F90">Fill your  details</legend>
    			    Registration Date : <span wicket:id="regDate"></span><br/>
    				<span>Your Name: </span><input wicket:id="name" type="text" id="name1" /><br/>
    				<span>Email: </span><input wicket:id="emailId" type="text" id="email" /><br/>
    				<p>
    					<input type="submit" name="Submit" value="Submit"/>
    				</p>
  	   	    </fieldset>
		</form>
	</body>
</html>

Acknowledgement.java

package com.javaonline;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.request.mapper.parameter.PageParameters;

public class Acknowledgement extends WebPage {

	public Acknowledgement(final PageParameters page) throws Exception {

			Label name = new Label("name", page.get("name"));
			Label email = new Label("email", page.get("email"));
			Label regDate = new Label("regDate", page.get("regDate"));

			add(name);
			add(email);
			add(regDate);
	}
}

The corresponding Acknowledgement.html is given below

<html>
<body>
Your Details are registered with us. <br/>
    <span>Registration Date & Time :</span><span wicket:id="regDate"></span><br/>
    <span>Name :</span><span wicket:id="name"></span><br/>
    <span>Email :</span><span wicket:id="email"></span><br/>
</body>
</html>

Running the above project you will get the below page.

propertymodel1

Enter values and submit

propertymodel2

You can see the  Registration Date and Time is automatically changed when submit because regDate property returns the current date and time and is bound with form component Label (regDate).

Reference :  Wicket Model and Forms

 

 

You may also like

Leave a Reply