代码改变世界

Wicket框架基础(二)

2009-04-22 09:36  乱世文章  阅读(330)  评论(0编辑  收藏  举报

 

Wicket框架基础(二)

pizza Application Sample


本节采用David R. Heffelfinger 撰写的 A First Look at the Wicket Framework一文做一个实际的例子。

本文档需要的环境

  • NetBeans IDE 6.5
  • Java Standard Development Kit (JDK™) 5.0 版或 6.0
  • NetBeans IDE Wicket Support 插件

 

一、netbeans6.5中建立wickt应用程序

1、      建立一个“web应用程序”。在选择服务器(如tomcat6.0)和javaEE版本(如5.0规范)之后,选择框架“wicket1.4

 

Wicket Name Page修改为:WicketTestPage,点击“完成”。

2、      编辑文件WicketTestPage.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

 

<html>

  <head>

    <title></title>

    <link wicket:id='stylesheet'/>

  </head>

  <body>

    <form wicket:id="pizzaForm">

<div style="font-weight: bold;" font-weight="" bold=""><big>Online Pizza Builder</big></div>

<table style="text-align: left; width: 427px; height: 112px;" border="1"

    cellpadding="0" cellspacing="0">

    <tbody>

        <tr>

            <td style="font-weight: bold; text-align: right;">Crust:</td>

            <td><select wicket:id="crust">

                <option>option 1</option>

                <option>option 2</option>

                <option>option 3</option>

            </select></td>

        </tr>

        <tr>

            <td style="font-weight: bold; text-align: right;">Toppings:</td>

            <td>Pepperoni<input wicket:id="pepperoni" type="checkbox" /> Sausage<input

                wicket:id="sausage" type="checkbox" />&nbsp;Onions<input

                wicket:id="onions" type="checkbox" />&nbsp;Green Peppers<input

                wicket:id="greenPeppers" type="checkbox" /></td>

        </tr>

        <tr>

            <td style="font-weight: bold; text-align: right;">Comments:</td>

            <td><input maxlength="50" size="50" wicket:id="comments" type="text" /></td>

        </tr>

        <tr>

            <td style="text-align: center;" colspan="2"><input value="Submit"

                name="Submit" type="submit" /></td>

        </tr>

    </tbody>

</table>

</form>

 

  </body>

</html>

3、  编辑文件WicketTestPage.java

 

public class WicketTestPage extends WebPage

{

  PizzaForm pizzaForm = new PizzaForm("pizzaForm");

 

  public WicketTestPage()

  {

    super();

    add(pizzaForm);

  }

}

 

4、新建java类,命名为PizzaForm

5、  编辑文件PizzaForm.java

public class PizzaForm extends Form

{

  private DropDownChoice crustDropDown;

  private CheckBox pepperoniCheckBox = new CheckBox("pepperoni");

  private CheckBox sausageCheckBox = new CheckBox("sausage");

  private CheckBox onionsCheckBox = new CheckBox("onions");

  private CheckBox greenPeppersCheckBox = new CheckBox("greenPeppers");

  private TextField commentsTextField = new TextField("comments");

 

  public PizzaForm(String id)

  {

    super(id);

    PizzaModel pizzaModel = new PizzaModel();

    setModel(new CompoundPropertyModel(pizzaModel));

    crustDropDown = new DropDownChoice(

        "crust",new PropertyModel(pizzaModel, "crust"), Arrays

        .asList(new CrustType[]

                              { new CrustType("Thin & Crispy"),

                                new CrustType("Hand Tossed"),

                                new CrustType("Pan Pizza") }),

                                new ChoiceRenderer("text", "id"));

 

    add(crustDropDown);

    add(pepperoniCheckBox);

    add(sausageCheckBox);

    add(onionsCheckBox);

    add(greenPeppersCheckBox);

    add(commentsTextField);

  }

}

6、新建java 类,命名PizzaModel

7、  编辑PizzaModel.java

public class PizzaModel implements Serializable

{

  private String crust;

  private boolean pepperoni;

  private boolean sausage;

  private boolean onions;

  private boolean greenPeppers;

  private String comments;

 

  public String getComments()

  {

    return comments;

  }

  public void setComments(String comments)

  {

    this.comments = comments;

  }

  public String getCrust()

  {

    return crust;

  }

  public void setCrust(String crust)

  {

    this.crust = crust;

  }

  public boolean getGreenPeppers()

  {

    return greenPeppers;

  }

  public void setGreenPeppers(boolean greenPeppers)

  {

    this.greenPeppers = greenPeppers;

  }

  public boolean getOnions()

  {

    return onions;

  }

  public void setOnions(boolean onions)

  {

    this.onions = onions;

  }

  public boolean getPepperoni()

  {

    return pepperoni;

  }

  public void setPepperoni(boolean pepperoni)

  {

    this.pepperoni = pepperoni;

  }

  public boolean getSausage()

  {

    return sausage;

  }

  public void setSausage(boolean sausage)

  {

    this.sausage = sausage;

  } 

}

 

8、新建javaCrustType,编辑CrustType.java如下:

/*

 * To change this template, choose Tools | Templates

 * and open the template in the editor.

 */

 

package com.myapp.wicket;

 

import java.io.Serializable;

 

public class CrustType implements Serializable

{

  String id;

  String text;

 

  public CrustType()

  {

    super();

  }

 

  public CrustType(String crustName)

  {

    setId(crustName);

    setText(crustName);

  }

  public String getId()

  {

    return id;

  }

  public void setId(String id)

  {

    this.id = id;

  }

  public String getText()

  {

    return text;

  }

  public void setText(String value)

  {

    this.text = value;

  }

  @Override

  public String toString()

  {

    return getText();

  }

}

9、修改PizzaForm.java,添加方法onSubmit()

@Override

  protected void onSubmit()

  {

    PizzaModel pizzaModel = (PizzaModel) getModelObject();

 

    setResponsePage(new WicketTestConfPage(pizzaModel));

  }

9、  新建wicket page,命名为WicketTestConfPage

10、              编辑WicketTestConfPage.html文件如下:

<table style="text-align: left; width: 427px; height: 112px;" border="1"
    cellpadding="0" cellspacing="0">
    <tbody>
        <tr>
            <td style="font-weight: bold; text-align: right;">Crust:</td>
            <td><span wicket:id="crust">Hello</span></td>
        </tr>
        <tr>
            <td style="font-weight: bold; text-align: right;">Toppings:</td>
            <td>Pepperoni:&nbsp; <span wicket:id="pepperoni"></span>
            Sausage:&nbsp;<span wicket:id="sausage"></span>&nbsp;Onions: &nbsp;<span
                wicket:id="onions"></span>&nbsp;Green Peppers:&nbsp;<span
                wicket:id="greenPeppers"></span></td>
        </tr>
        <tr>
            <td style="font-weight: bold; text-align: right;">Comments:</td>
            <td><span wicket:id="comments"></span></td>
        </tr>
    </tbody>
</table>

 

11、              编辑WicketTestConfPage.java文件如下:

public class WicketTestConfPage extends WebPage

{

 

  public WicketTestConfPage(PizzaModel pizzaModel)

  {

    super();

    add(new Label("crust", pizzaModel.getCrust()));

    add(new Label("pepperoni", new Boolean(pizzaModel.getPepperoni())

        .toString()));

    add(new Label("sausage", new Boolean(pizzaModel.getSausage()).toString()));

    add(new Label("onions", new Boolean(pizzaModel.getOnions()).toString()));

    add(new Label("greenPeppers", new Boolean(pizzaModel.getGreenPeppers())

        .toString()));

    add(new Label("comments", pizzaModel.getComments()));

  }

}

12、              F6,运行主项目,WicketTestPage页面如下:

 

13、              在表单中输入任意内容,点submit提交,页面转至: