创建基于wicket的web项目(二)-常用控件

wicket是一个java web框架,用html描述ui,并将具有特殊标记的html元素定义为ui控件,在java类中通过操作这些ui控件以控制其输出行为和样式等.
1.wicket框架是一个遵循标准MVC结构的web框架.
  model:使用IModel来存取数据.
  view:使用标准的html模板.
  control:使用控件Component来处理各种业务,并负责将model层的数据映射到View层.
  这里的IModel其实并不代表真正的数据模型,它只是提供了对外接口,需要通过getDefaultModelObject()来取得真正的数据.
2.wicket项目部署.
  wicket项目的部署与通常的Web程序区别并不大,稍微有一点不同之处是它的运行状态分为开发模式和部署模式.
  这两种模式的区别是:在开发模式下,wicket会不断的监听资源文件的改变,如html文件或者properties文件,以便在资源改变后能够及时
  载入相关的资源.但是这种对资源的监听会降低系统的性能.所以,项目在开发阶段使用开发模式,在真正部署的时候使用部署模式.
3.下面通过一个项目实例来简单认识一下wicket中常用的控件.
  a.创建名称为wicket-demo的web项目,并导入必须的jar包.

  b.创建java类,直接看代码MyResume.java.

   

<span style="font-size:12px;">package com.ilucky.wicket;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.CheckBoxMultipleChoice;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.RadioChoice;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.Model;

/**
 * @author IluckySi
 * @date 20140401
 */
public class MyResume extends WebPage {

	private static final long serialVersionUID = 1L;

	@SuppressWarnings({ "rawtypes", "unchecked", "unused" })
	public MyResume() {
		Form form = new Form("form");
		Label title = new Label("title", "简历");
		Label usernameLabel = new Label("usernameLabel", "姓名");
		Label sexLabel = new Label("sexLabel", "性别");
		Label ageLabel = new Label("ageLabel", "年龄" );
		Label areaLabel = new Label("areaLabel", "地区");
		Label emailLabel = new Label("emailLabel", "邮箱");
		Label phoneLabel = new Label("phoneLabel", "电话");
		Label hobbyLabel = new Label("hobbyLabel", "爱好");
		Label profileLabel = new Label("profileLabel", "简介");
		Label registerLabel = new Label("registerLabel", "提交");
		final TextField usernameTextField = new TextField("usernameTextField", Model.of());
		List<String> sexList = new ArrayList<String>();
		sexList.add("男");
		sexList.add("女");
		final RadioChoice sexRadioChoice = new RadioChoice("sexRadioChoice", Model.of(), sexList);
		final TextField ageTextField = new TextField("ageTextField", Model.of());
		List<String> provienceList = new ArrayList<String>();
		provienceList.add("河北省");
		provienceList.add("河南省");
		List<String> cityList = new ArrayList<String>();
		cityList.add("石家庄");
		cityList.add("郑州市");
		cityList.add("保定市");
		cityList.add("洛阳市");
		final DropDownChoice provienceDropDownChoice = new DropDownChoice("provienceDropDownChoice", Model.of(), provienceList);
		final DropDownChoice cityDropDownChoice = new DropDownChoice("cityDropDownChoice", Model.of(), cityList);
		final TextField phoneTextField = new TextField("phoneTextField", Model.of());
		final TextField emailTextField = new TextField("emailTextField", Model.of());
		List<String> hobbyList = new ArrayList<String>();
		hobbyList.add("歌曲");
		hobbyList.add("电影");
		hobbyList.add("游泳");
		hobbyList.add("乒乓球");
		final CheckBoxMultipleChoice hobbyCheckBoxMultipleChoice = new CheckBoxMultipleChoice("hobbyCheckBoxMultipleChoice", Model.of(), hobbyList);
		final TextArea profileTextArea = new TextArea("profileTextArea", Model.of());
		Button save = new Button("save") {
			private static final long serialVersionUID = 1L;

			public void onSubmit() {
				User user = new User();
				if(usernameTextField.getDefaultModelObject() != null) {
					user.setUsername(usernameTextField.getDefaultModelObject().toString());
				}
				if(sexRadioChoice.getDefaultModelObject() != null) {
					user.setSex(sexRadioChoice.getDefaultModelObject().toString());
				}
				if(ageTextField.getDefaultModelObject() != null) {
					user.setAge(Integer.parseInt(ageTextField.getDefaultModelObject().toString()));
				}
				if(provienceDropDownChoice.getDefaultModelObject() != null) {
					String area = provienceDropDownChoice.getDefaultModelObject().toString();
					if(cityDropDownChoice.getDefaultModelObject() != null) {
						area += "," + cityDropDownChoice.getDefaultModelObject().toString();
					}
					user.setArea(area);
				}
				if(phoneTextField.getDefaultModelObject() != null) {
					user.setPhone(phoneTextField.getDefaultModelObject().toString());
				}
				if(emailTextField.getDefaultModelObject() != null) {
					user.setEmail(emailTextField.getDefaultModelObject().toString());
				}
				if(hobbyCheckBoxMultipleChoice.getDefaultModelObject() != null) {
					user.setHobby(hobbyCheckBoxMultipleChoice.getDefaultModelObject().toString());
				}
				if(profileTextArea.getDefaultModelObject() != null) {
					user.setProfile(profileTextArea.getDefaultModelObject().toString());
				}
				System.out.println("服务器收到用户简历信息(" + user.toString() +")");
			}
		};
		form.add(title, usernameLabel, sexLabel, ageLabel, areaLabel, phoneLabel, emailLabel, hobbyLabel, profileLabel);
		form.add(usernameTextField, sexRadioChoice, ageTextField, provienceDropDownChoice, cityDropDownChoice,
				phoneTextField, emailTextField, hobbyCheckBoxMultipleChoice, profileTextArea);
		form.add(save);
		this.add(form);
	}
}
/**
输出结果:
服务器收到用户简历信息(用户名:1*性别:男*年龄:2*地区:河南省,洛阳市*电话:3*邮箱:4*爱好:[歌曲, 电影]*简介:5)
**/
</span>
<span style="font-size:12px;">package com.ilucky.wicket;

/**
 * @author IluckySi
 * @date 20140401
 */
public class User {

	private String username;
	
	private String sex;
	
	private int age;
	
	private String area;
	
	private String phone;
	
	private String email;
	
	private String hobby;
	
	private String profile;
	
	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getArea() {
		return area;
	}

	public void setArea(String area) {
		this.area = area;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}
	
	public String getHobby() {
		return hobby;
	}

	public void setHobby(String hobby) {
		this.hobby = hobby;
	}

	public String getProfile() {
		return profile;
	}

	public void setProfile(String profile) {
		this.profile = profile;
	}

	public String toString() {
		return "用户名:" + username + "*性别:"  + sex + "*年龄:" + age +"*地区:" + area +"*电话:" + phone +"*邮箱:" + email
				+ "*爱好:" + hobby + "*简介:" + profile;
	}
}
</span>

    在java类中,每个控件中都有一个Model.of(),意思是此控件的数据源是此控件本身,当然也可以通过Model.of("test")为其初始化默认值.
    与Model.of()对应的是通过给控件绑定Model提供数据源,后面会讲到,这里先不做介绍.

  c.创建html模板,直接看代码MyResume.html.

<span style="font-size:12px;"><html>
	<head>
		<title>MyResume.html</title>
	</head>
<body>
	<form wicket:id="form">
		<table width="60%" align="center">
			<tr align="center">
				<td><h4 wicket:id="title"></h4></td>
			</tr>
		</table>
		<table align="center" width="60%" border="1">
			<tr align="center">
				<td><div wicket:id="usernameLabel"></div></td>
				<td><input wicket:id="usernameTextField" type="text"></input></td>
				<td><div wicket:id="sexLabel"></div></td>
				<td><div wicket:id="sexRadioChoice"></div></td>
			</tr>
			<tr align="center">
				<td><div wicket:id="ageLabel"></div></td>
				<td><input wicket:id="ageTextField" type="text"></input></td>
				<td><div wicket:id="areaLabel"></div></td>
				<td><select wicket:id="provienceDropDownChoice"></select>
						<select wicket:id="cityDropDownChoice"></select>
				</td>
			</tr>
			<tr align="center">
				<td><div wicket:id="phoneLabel"></div></td>
				<td><input wicket:id="phoneTextField" type="text"></input></td>
				<td><div wicket:id="emailLabel"></div></td>
				<td><input wicket:id="emailTextField" type="text"></input></td>
			</tr>
			<tr align="center">
				<td><div wicket:id="hobbyLabel"></div></td>
				<td><div wicket:id="hobbyCheckBoxMultipleChoice" type="checkbox"></div></td>
				<td><div wicket:id="profileLabel"></div></td>
				<td><textarea wicket:id="profileTextArea"  cols="20"  rows="4"></textarea></td>
			</tr>
		</table>
		<table align="center">
			<tr align="center">
				<td><input type="submit"  wicket:id="save"  value="save"></input></td>
			</tr>
		</table>
	</form>
</body>
</html>
</span>

  d.创建Application的实现类.

    通过Application类中的getHomePage方法可以定义用户请求时的默认页面,直接看代码WicketApplication.

<span style="font-size:12px;">package com.ilucky.wicket;

import org.apache.wicket.Page;
import org.apache.wicket.RuntimeConfigurationType;
import org.apache.wicket.protocol.http.WebApplication;

/**
 * @author IluckySi
 * @date 20140401
 */
public class WicketApplication extends WebApplication {

	public Class<? extends Page> getHomePage() {
		return MyResume.class;
	}
	
	// wicket项目运行状态分为开发模式和部署模式.
	@Override
	public RuntimeConfigurationType getConfigurationType() {
		return RuntimeConfigurationType.DEVELOPMENT;
		//return RuntimeConfigurationType.DEPLOYMENT; 
	}
}
</span>

    在WicketApplication类中,还可以通过getConfigurationType方法设置了项目的运行状态模式.
  e.配置web.xml,在web.xml文件中为wicket的入口配置映射,其实wicket就像struts一样,通过filter截获请求,

    然后将截获的请求交给Application的实现类处理,从而跳转到Application实现类里配置的默认页面.

<span style="font-size:12px;"><?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
	<display-name>wicket-demo</display-name>
	 
	 <filter>
        <filter-name>wicketFilter</filter-name>
        <filter-class>org.apache.wicket.protocol.http.WicketFilter</filter-class>
        <init-param>
            <param-name>applicationClassName</param-name>
            <param-value>com.ilucky.wicket.WicketApplication</param-value>
        </init-param>
    </filter>
    
    <filter-mapping>
        <filter-name>wicketFilter</filter-name>
        <url-pattern>/w/*</url-pattern>
    </filter-mapping>
</web-app>
</span>

  f.完成上面的步骤后,部署wicket-demo项目并启动,就可以通过http://127.0.0.1:8080/wicket-demo/w/址访问了.


注:本实例用到的控件有:Label,TextField,RadioChoice,DropDownChoice,CheckBoxMultipleChoice,TextArea和Button.




  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值