怎么用WEBWORK写一个注册功能的页面(1)?
要求:
在一个jsp页面注册用户:
字段包括用户名、用户年龄、用户性别(下拉菜单或者单选框)、Email地址
提交到action,在另外的jsp页面显示用户注册的详细信息。
同时获取到用户的ip等信息
1. 新建一个tomcat工程,参考:《怎么用WebWork来实现HelloWorld?》一文的建工程的流程,工程名为WebWorkRegist,导入需要的JAR包。
2. 在WEB-INF/src目录下面建一个Package,名字为example.register,建一个两个类RegisterAction.java和User.java
RegisterAction.java
/**
* @author shawn
* 校验输入的用户信息是否合法
*/
package example.register;
import com.opensymphony.xwork.Action;
public class RegisterAction implements Action {
private User user ;
public User getUser(){
return this.user;
}
public void setUser(User user) {
this.user = user;
}
public String execute(){
//在这里调用用户注册的业务逻辑,比如:将注册信息存储到数据库
if(user.getSexy().equals("1"))
{
user.setSexy("man");
}
else{user.setSexy("woman");}
return SUCCESS;
}
}
User.java
/**
* @author shawn
* 定义一个用户的类
*/
package example.register;
public class User {
private String username;
private String password;
private String email;
private String sexy;
private int age;
public String getUsername() {
return username;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void setUsername(String username) {
this.username = username;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getSexy() {
return sexy;
}
public void setSexy(String sexy) {
this.sexy = sexy;
}
}
3.在工程WebWorkRegist下建两个jsp页面,register.jsp,register-result.jsp
register.jsp
<%@ page language="java" contentType="text/html; charset=GBK" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>Register Example</title>
</head>
<body>
<table border=0 width=97%>
<tr><td align="left">
<form name="register" action="register.action" method="post">
用户名:<input id="username"type="text" name="user.username"><br>
密 码:<input id="pwd"type="password" name="user.password">大于四位<br>
邮箱 :<input id="email"type="text" name="user.email"><br>
年龄:<input id="age" type="text" name="user.age"><br>
性别:<SELECT id="sexy"type="text" NAME="user.sexy" SIZE="1" >
<OPTION VALUE="1" SELECTED>男
<OPTION VALUE="2">女
</SELECT>
<input type="button" name="Submit" value="提交" οnclick="check()"><br>
</form>
</td></tr>
</table>
<script language="javascript">
function check()
{
var t = document.getElementById("age").value;
if(t<0 || t>100){
alert("您输入在年龄不符合要求!");
return false;
}
//姓名校验
if(document.register.email.value.length!=0)
{
if (document.register.email.value.charAt(0)=="." ||
document.register.email.value.charAt(0)=="@"||
document.register.email.value.indexOf('@', 0) == -1 ||
document.register.email.value.indexOf('.', 0) == -1 ||
document.register.email.value.lastIndexOf("@")==document.register.email.value.length-1 ||
document.register.email.value.lastIndexOf(".")==document.register.email.value.length-1)
{
alert("Email的格式不正确!");
return false;
}
}
else
{
alert("Email的不能为空!");
return false;
}//邮箱格式校验
if(document.register.pwd.value.length<4)
{
alert("密码长度太短!请重新设置~");
return false;
}
document.register.submit();//通过校验后可以进行提交.
}
</script>
</body>
</html>
register-result.jsp
<%@ page language="java" contentType="text/html; charset=GBK"
pageEncoding="GBK"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib prefix="ww" uri="/webwork" %>
<html>
<head><title>Register result</title></head>
<body>
<table border=1 width=87%>
<tr>
<td align="left">
恭喜注册成功……<p>
用户名:<ww:property value="user.username"/><br>
邮 箱:<ww:property value="user.email"/><br>
年 龄:<ww:property value="user.age"/><br>
本地IP:<%=request.getRemoteAddr()%><br>
性 别:<ww:property value="user.sexy"/><br>
</td>
</tr>
</table>
</body>
</html>
4.xwork.xml和web.xml
xwork.xml
<!DOCTYPE xwork PUBLIC "-//OpenSymphony Group//XWork 1.1.1//EN"
"http://www.opensymphony.com/xwork/xwork-1.1.1.dtd">
<xwork>
<include file="webwork-default.xml"/>
<package name="helloWorld" extends="webwork-default">
<action name="register" class="example.register.RegisterAction" >
<result name="success" type="dispatcher">register-result.jsp</result>
<!-- <result name="error">error.jsp</result>-->
<interceptor-ref name="params"/>
</action>
</package>
</xwork>
web.xml
同HelloWorld里面的配置一样(略去)
5.打开浏览器在地址栏里面输入地址:http://regist/register.jsp
就可以看到运行成功的界面,
下一节会详细解释一下这些代码的功能。