|
First, download a release or get the source from subversion and build it.
This guide assumes a basic understanding of Wicket and JPA. For more guides on how to get started with Wicket, refer to the Wicket site. Setting up a project with Maven The easiest way to use Wicket RAD in your application is by using Maven, simply create a dependency on the wicket-rad-core (groupId=org.wicketrad, artifactId=wicket-rad-core, version=[check the release you have downloaded]). Setting up a JPA enabled project For this we are assuming that you want a JPA persistence enabled project, for this, you will need to:
Create a Wicket WebPage with the associated markup. Add a div or span to the markup for the form, it could look something like this in MyCreatePage.html: <div wicket:id="form"></div> Now create the bean you want to edit and persist, it should implement the org.wicketrad.service.Identifiable interface in order to be recognized by Wicket RAD's JPA functionality: @Table(name="mybean") @Entity public class MyBean implements Identifiable<Long>{ @Column(name="bean_id") @Id @GeneratedValue private Long id; @Column(name="name") private String name; @Column(name="someOtherValue") private String someOtherValue; public Long getId(){return id;} public void setId(Long id){this.id = id;} @FieldOrder(1) @TextField @Length(min=5, max=10) public String getName(){return name;} public void setName(String name){this.name = name;} @FieldOrder(2) @TextArea public String getSomeOtherValue(){ return someOtherValue;} public void setSomeOtherValue(String value){ this.someOtherValue = value;} } As you might notice, the private instance variables and the class itself are annotated with JPA annotations. The Wicket RAD specific form annotations on the other hand are on the getter-methods, and these annotations should always be on the getters, as this is where Wicket RAD will look for them. The bean above will generate a form with a text field, with a length validator asserting that any input is between 5 and 10 characters long. The second field will be a textarea field with a default number of rows and columns (these can be set in the annotation as well). The @FieldOrder annotation will decide in what order the fields will be on the form. Now let's move on to actually putting the form on the page, the code for the actual Wicket web-page might look something like this: public class MyCreatePage extends WebPage{
public MyCreatePage(){ BeanForm form = new DefaultCreateBeanForm("form", new MyBean()){
protected void afterSubmit(){ this.setResponsePage(SomeOtherPage.class); } }; add(form); }
That's it! The "form" id of the BeanForm will attach the form to the markup we defined earlier and the new MyBean() passes in a new bean into the form. The form itself will handle the generation of the form and the persisting of the bean into the database once the form is submitted, the only thing the developer needs to do is implement the afterSubmit() method to define what the form should do after the submit is finished (like in this case: go to another page). That is about it in terms of a very simple first example of the power of Wicket RAD. |