接下來的這個例子將會教會大家使用Wicket開發一個能夠處理用戶輸入的程序,這個程序雖然簡單,但也基本上描述瞭如何使用Wicket處理用戶的輸入。
我們會修改上面的的例子,增加處理用戶輸入的功能。也就是在頁面上顯示一個文本框,以用戶 輸入數據,並提交到服務器,再將用戶 輸入的信息顯示在頁面上。
這是HTML模板文件
<html> <body> <span wicket:id="message">Message goes here</span> <form wicket:id="messageInputForm"> <input type="text" wicket:id="messageInput"/> <input type="submit" value="update"/> </form> </body></html>
相應的Java代碼
package mypackage;import wicket.markup.html.WebPage;import wicket.markup.html.basic.Label;import wicket.markup.html.form.Form;import wicket.markup.html.form.TextField;import wicket.model.IModel;import wicket.model.Model;public class HelloWorld extends WebPage{ public HelloWorld() { IModel messageModel = new Model("Hello World!"); add(new Label("message", messageModel)); add(new MessageForm("messageInputForm", messageModel)); } private final class MessageForm extends Form { public MessageForm(String id, IModel model) { super(id); add(new TextField("messageInput", model)); } protected void onSubmit() { // nothing to do here as the model is automatically updated } }}