仅限记录!wicket对所有控件设定都存放与Server,不知好是不好,感觉有些不靠谱!
package com.wicket.page;
import java.awt.Graphics2D;
import java.awt.image.renderable.RenderableImage;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.wicket.Component;
import org.apache.wicket.Page;
import org.apache.wicket.PageParameters;
import org.apache.wicket.behavior.IBehavior;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.WebMarkupContainerWithAssociatedMarkup;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.basic.MultiLineLabel;
import org.apache.wicket.markup.html.border.Border;
import org.apache.wicket.markup.html.border.BoxBorder;
import org.apache.wicket.markup.html.border.Border.BorderBodyContainer;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Check;
import org.apache.wicket.markup.html.form.CheckBox;
import org.apache.wicket.markup.html.form.CheckBoxMultipleChoice;
import org.apache.wicket.markup.html.form.CheckGroup;
import org.apache.wicket.markup.html.form.CheckGroupSelector;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.IChoiceRenderer;
import org.apache.wicket.markup.html.form.ListChoice;
import org.apache.wicket.markup.html.form.ListMultipleChoice;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.Radio;
import org.apache.wicket.markup.html.form.RadioChoice;
import org.apache.wicket.markup.html.form.RadioGroup;
import org.apache.wicket.markup.html.form.SubmitLink;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.form.upload.FileUpload;
import org.apache.wicket.markup.html.form.upload.FileUploadField;
import org.apache.wicket.markup.html.form.validation.IFormValidator;
import org.apache.wicket.markup.html.image.Image;
import org.apache.wicket.markup.html.image.resource.RenderedDynamicImageResource;
import org.apache.wicket.markup.html.include.Include;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.link.ExternalLink;
import org.apache.wicket.markup.html.link.IPageLink;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.link.PageLink;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.list.PageableListView;
import org.apache.wicket.markup.html.navigation.paging.PagingNavigator;
import org.apache.wicket.markup.html.panel.ComponentFeedbackPanel;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.util.file.File;
import org.apache.wicket.util.lang.Bytes;
import com.wicket.model.BookModel;
public class HelloWorldPage extends WebPage {
private static final long SVU = 1L;
private int count = 0;
public HelloWorldPage(){
//信息输出
Label label = new Label("message","Hello world!");
this.add(label);
MultiLineLabel linelabel = new MultiLineLabel("multimessage","Hello \nworld");
this.add(linelabel);
//容器
WebMarkupContainerWithAssociatedMarkup panel =
new WebMarkupContainerWithAssociatedMarkup("panel");
Label label1 = new Label("panel_label","这是被panel容纳进来的Label!");
panel.add(label1);
this.add(panel);
WebMarkupContainer webpanel =
new WebMarkupContainer("webpanel");
Label label2 = new Label("webpanel_label","这是被webpanel容纳进来的Label!");
webpanel.add(label2);
this.add(webpanel);
Border border = new BoxBorder("border");
Label label3 = new Label("border_label","这是被border容纳的label");
border.add(label3);
this.add(border);
this.add(new Include("include","inc/include.html"));
//超级链接控件
Link link = new Link("link") {@Override
public void onClick() {
count++;
}}; link.add(new Label("count",new Model(){
public String getObject(){
return count+"";
}
}));
this.add(link);
ExternalLink externallink = new ExternalLink
("externallink","http://www.baidu.com","百度");
this.add(externallink);
PageLink pagelink = new PageLink("pagelink",new IPageLink(){
public Class extends Page> getPageIdentity() {
return TestPageLink.class;
}
public Page getPage(){
return new TestPageLink();
}
});
this.add(pagelink);
PageParameters pps = new PageParameters();
pps.put("id", 1);
BookmarkablePageLink bmplink =
new BookmarkablePageLink("bookmarkablepagelink",TestPageLink.class,pps);
this.add(bmplink);
//表单输入控件
final FeedbackPanel feed = new FeedbackPanel("errmsg");
//form表单的button组件
Form form = new Form("myform"){
protected void onSubmit(){
feed.info(loadonSubmit("你提交了button表单"));
}
};
Button button1 = new Button("button1"){
public void onSubmit(){
feed.info(loadonSubmit("你按下了button1"));
}
};
Button button2 = new Button("button2"){
public void onSubmit(){
feed.info(loadonSubmit("你按下了button2"));
}
};
form.add(button1);
form.add(button2);
this.add(feed);
this.add(form);
//form表单的Link组件
final FeedbackPanel feed1 = new FeedbackPanel("errmsg1");
Form linkform = new Form("myform1"){
protected void onSubmit(){
feed1.info(loadonSubmit("你提交了linkform表单"));
}
};
SubmitLink submitlink = new SubmitLink("submit_form_link"){
public void onSubmit(){
feed1.info(loadonSubmit("你按下了linkform的内部提交"));
}
};
SubmitLink submitlink1 = new SubmitLink("submit_link",linkform){
public void onSubmit(){
feed1.info(loadonSubmit("你按下了linkform的外部提交"));
}
};
linkform.add(submitlink);
this.add(submitlink1);
this.add(feed1);
this.add(linkform);
//文本输入控件
//只允许输入数字,且不能为空,默认值为"请输入测试字符"
TextField text = new TextField("textload",Integer.class);
text.setLabel(new Model("请输入测试字符"));
text.setRequired(true);
final ComponentFeedbackPanel feed2 = new ComponentFeedbackPanel("errmsg2",text);
Form textform = new Form("textform"){
protected void onSubmit(){
feed2.info(loadonSubmit("你提交了textform表单"));
}
};
textform.add(text);
this.add(feed2);
this.add(textform);
//密码输入控件
//只允许输入数字,且不能为空,默认值为"请输入测试字符"
PasswordTextField textpass = new PasswordTextField("text_pass");
final FeedbackPanel feed3 = new FeedbackPanel("errmsg3");
Form passform = new Form("passform"){
protected void onSubmit(){
feed3.info(loadonSubmit("你提交了passform表单"));
}
};
passform.add(textpass);
this.add(feed3);
this.add(passform);
//多行文本输入
TextArea textarea = new TextArea("textarea");
this.add(textarea);
//选择框
CheckBox checkbox = new CheckBox("checkboxbool");
this.add(checkbox);
//多项选择框
Listlist = new ArrayList();
list.add("1");
list.add("2");
list.add("3");
final HashMapmap = new HashMap();
map.put("1","语文");
map.put("2","数学");
map.put("3","英语");
//实现IChoiceRenderer接口以保存checkbox的显示文字与值 IChoiceRenderer renderer = new IChoiceRenderer(){
public Object getDisplayValue(Object object){
return map.get(object);
}
public String getIdValue(Object arg0, int arg1) {
return null;
}
};
CheckBoxMultipleChoice Onlysites=
new CheckBoxMultipleChoice("sites",list,renderer);
List ALLSITES = new ArrayList();
final Map ALLNAMES = new HashMap();
ALLSITES.add("1");
ALLSITES.add("2");
ALLSITES.add("3");
ALLNAMES.put("1", "语文");
ALLNAMES.put("2", "数学");
ALLNAMES.put("3", "英语");
Form allcheckform = new Form("allcheckform");
CheckGroup group = new CheckGroup("allcheckgroup",new ArrayList());
group.add(new CheckGroupSelector("allcheckgroupselector"));
ListView allsites = new ListView("allsites", ALLSITES) {
protected void populateItem(ListItem item) {
Object object=item.getModelObject();
item.add(new Check("allcheck", new
Model(Integer.toString(item.getIndex()))));
item.add(new Label("allcheckname",new Model((Serializable) ALLNAMES.get(object))));
}
};
this.add(onlysites);
group.add(allsites);
allcheckform.add(group);
this.add(allcheckform);
//下拉框
DropDownChoice dropdownchoice =
new DropDownChoice("dropsites",ALLSITES,renderer);
this.add(dropdownchoice);
//单选列表框
ListChoice listchoice =
new ListChoice("multiple",ALLSITES,renderer);
this.add(listchoice);
//单选组合框
ListMultipleChoice alllistchoice =
new ListMultipleChoice("allmultiple",ALLSITES,renderer);
this.add(alllistchoice);
RadioChoice radio =
new RadioChoice("onlyradio",ALLSITES,renderer);
this.add(radio);
//单选增强组合框
Form radioform = new Form("radioform");
add(radioform);
RadioGroup radiogroup=new RadioGroup("radiogroup");
radioform.add(radiogroup);
ListView radiosites = new ListView("radiosites", ALLSITES) {
protected void populateItem(ListItem item) {
Object object=item.getModelObject();
item.add(new Radio("radioradio", new Model(Integer.toString(item.getIndex()))));
item.add(new Label("radioname",new Model((Serializable) ALLNAMES.get(object))));
};
};
radiogroup.add(radiosites);
//静态图像
Image staticimg = new Image("staticimg","img/mks.jpg");
this.add(staticimg);
//动态图像
Image dynameicimg = new Image("dynameicimg",
new RenderedDynamicImageResource(200,100){
protected boolean render(Graphics2D graphics) {
graphics.drawString("看看生成以后的样子", 50, 50);
return true;
}
}
);
this.add(dynameicimg);
//文件上传
final FileUploadField fileup = new FileUploadField("upfileload");
Form upfileform = new Form("upfileform"){
protected void onSubmit(){
final FileUpload upload = fileup.getFileUpload();
if(upload!=null){
try {
upload.writeTo(new File("d:\\load\\1.txt"));
} catch (IOException e) {
e.printStackTrace();
}
}
super.onSubmit();
}
};
upfileform.add(fileup);
upfileform.setMultiPart(true);
upfileform.setMaxSize(Bytes.kilobytes(1000));
this.add(upfileform);
//数据列表
ListView listView = new ListView("listView", ALLSITES) {
protected void populateItem(ListItem item) {
String name = (String) ALLNAMES.get(item.getModelObject());
item.add(new Label("name", name));
item.add(new ExternalLink("url", (String) item.getModelObject()));
}
};
this.add(listView);
}
public final String loadonSubmit(String str){
String backstr = "";
if(this.getRequest().getParameter("uname") == null)
backstr = str + ";" + "请输入字符";
else
backstr = str + ";" + "你输入的字符为:" + this.getRequest().getParameter("uname");
return backstr;
}
}
JAVA技巧:wicket一些基础控件代码.doc
下载Word文档到电脑,方便收藏和打印[全文共6395字]
编辑推荐:
下载Word文档