一个wicket 综合应用实例-在线匹萨店
这个例子对初学wicket很有帮助,可以帮助你很好理解wicket开发框架的基本概念,希望能使初学wicket的兄弟快速入门。
实现的业务流程如下图:
选择好自己喜欢的匹萨后点击提交,将信息带到下个页面显示出来
Pizza.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Pizza</title>
</head>
<body>
<form wicket:id="pizzaForm">
<div style="font-weight: bold;" font-weight="" bold="">
<big>在线比萨饼店</big>
</div>
<table style="text-align: left; width: 600px; height: 200px;"
border="1" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td style="font-weight: bold; text-align: right;">
比萨饼面皮:
</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;">
配料:
</td>
<td>
意大利辣香肠
<input wicket:id="pepperoni" type="checkbox" />
香肠
<input wicket:id="sausage" type="checkbox" />
洋葱
<input wicket:id="onions" type="checkbox" />
绿胡椒
<input wicket:id="greenPeppers" type="checkbox" />
</td>
</tr>
<tr>
<td style="font-weight: bold; text-align: right;">
备注:
</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>
Pizza.java
public class Pizza extends WebPage
{
public Pizza()
{
super();
PizzaForm pizzaForm = new PizzaForm("pizzaForm");
add(pizzaForm);
}
}
PizzaForm.java
public class PizzaForm extends Form
{
private static final long serialVersionUID = 1L;
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);
}
@Override
protected void onSubmit()
{
PizzaModel pizzaModel = (PizzaModel) getModelObject();
setResponsePage(new PizzaList(pizzaModel));
}
}
CrustType.java
public class CrustType implements Serializable
{
private static final long serialVersionUID = 1L;
private String id;
private 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 text)
{
this.text = text;
}
@Override
public String toString()
{
return getText();
}
}
PizzaModel.java
public class PizzaModel implements Serializable
{
private static final long serialVersionUID = 1L;
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;
}
}
PizzaList.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>PizzaList.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=gbk">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
</head>
<body>
<table style="text-align: left; width: 600px; height: 200px;"
border="1" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td style="font-weight: bold; text-align: right;">
比萨饼面皮:
</td>
<td>
<span wicket:id="crust">Hello</span>
</td>
</tr>
<tr>
<td style="font-weight: bold; text-align: right;">
配料:
</td>
<td>
意大利辣香肠:
<span wicket:id="pepperoni"></span> 香肠:
<span wicket:id="sausage"></span> 洋葱:
<span wicket:id="onions"></span> 绿胡椒:
<span wicket:id="greenPeppers"></span>
</td>
</tr>
<tr>
<td style="font-weight: bold; text-align: right;">
备注:
</td>
<td>
<span wicket:id="comments"></span>
</td>
</tr>
</tbody>
</table>
</body>
</html>
PizzaList.java
public class PizzaList extends WebPage
{
/**
*
*/
public PizzaList()
{
// TODO Auto-generated constructor stub
}
/**
* @param model
*/
public PizzaList(IModel<?> model)
{
super(model);
// TODO Auto-generated constructor stub
}
/**
* @param pageMap
*/
public PizzaList(IPageMap pageMap)
{
super(pageMap);
// TODO Auto-generated constructor stub
}
/**
* @param parameters
*/
public PizzaList(PageParameters parameters)
{
super(parameters);
// TODO Auto-generated constructor stub
}
/**
* @param pageMap
* @param model
*/
public PizzaList(IPageMap pageMap, IModel<?> model)
{
super(pageMap, model);
// TODO Auto-generated constructor stub
}
/**
* @param pageMap
* @param parameters
*/
public PizzaList(IPageMap pageMap, PageParameters parameters)
{
super(pageMap, parameters);
// TODO Auto-generated constructor stub
}
public PizzaList(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()));
}
}
页面的跳转、Model的运用、页面元素的构造都运用到了,是个很不错的wicket教程,希望大家喜欢!