Wicket 9: Setup a form


This is part 9 of a series about Wicket.

We simply want to pass the value of a text field to another page, so we’ll build two pages: Input that accepts user input and Output that displays the result.

Input.html

Consider this simple form. It consists of a text field and a submit button.

<form>
  <input type="text" /><input type="submit" value="OK" />
</form>

Let’s add wicket ids.

<form wicket:id="form">
  <input type="text" wicket:id="name" /><input type="submit" value="OK" />
</form>

Output.html

Output.html simply says hello to whatever is the value of the <span>.

Hello <span></span> !!

Let’s give it a wicket id.

Hello <span wicket:id="output"></span> !!

Output.java

There is a single element in the HTML markup that needs a value.

add(new Label("output", new Model()));

So we can pass it through the constructor.

public Output(Model model) {
   add(new Label("output", model));
}

Input.java

Here’s the form and the field.

final Form form = new Form("form");
final TextField field = new TextField("name");

The form contains the field and the page contains the form.

form.add(field);
add(form);

We’d like to specify the behavior of the form when it is submitted.

final Form form = new Form("form") {
   protected void onSubmit() {
   }
};

Actually, the request should be received by an Output page.

final Form form = new Form("form") {
   protected void onSubmit() {
      setResponsePage(new Output(null));
   }
};

The trick is to send the model of the field to the constructor of that page.

private Model model = new Model();
public Input() {
   final Form form = new Form("form") {
      protected void onSubmit() {
         setResponsePage(new Output(model));
      }
   };
   final TextField field = new TextField("name", model);
   form.add(field);
   add(form);
}

Complete code

Input.html

<html>
  <body>
    <form wicket:id="form">
      Please enter your name : 
      <input type="text" wicket:id="name" /><input type="submit" value="OK" />
    </form>
  </body>
</html>

Input.java

public class Input extends WebPage {
   private Model model = new Model();
   public Input() {
      final Form form = new Form("form") {
         protected void onSubmit() {
            setResponsePage(new Output(model));
         }
      };
      final TextField field = new TextField("name", model);
      form.add(field);
      add(form);
   }
}

Output.html

<html>
  <body>
    Hello <span wicket:id="output" style="font-size:x-large;"></span> !!
  </body>
</html>

Output.java

public class Output extends WebPage {
   public Output(Model model) {
      add(new Label("output", model));
   }
}

Result

A simple form

A simple form

Let’s type something,

The user enters data

The user enters data

and submit.

The result

The result

Review

  • To specify the behavior of a Form during submission, just implement its onSubmit() method.
  • You may pass anything that IS-A Serializable between two pages.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: