input_user.jsp <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <form action="reg.action" method="post"> 用户编号: <input name="user.uid" /> <br/> 用户名: <input name="user.uname" /><br/> 密码: <input name="user.password" /><br/> 年龄: <input name="user.age" /><br/> 出生日期: <input name="user.birthday"/><br/> 邮箱: <input name="user.email"/><br/> <input type="submit" value="注册"/> </form> </body> </html>
struts.xml <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <constant name="struts.devMode" value="true" /> <package name="p" namespace="/" extends="struts-default"> <action name="reg" class="com.etc.action.RegAction"> <result> /showuser.jsp </result> <result name="input"> /showerror.jsp </result> </action> </package> </struts>
showuser.jsp <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'show.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> 注册成功!用户信息如下: ${user } </body> </html>
showerror.jsp <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib uri="/struts-tags" prefix="s"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'showerror.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> 出错信息如下:<s:fielderror/> </body> </html>
package com.etc.entity; import java.util.Date; public class User { private Integer uid; private String uname; private String password; private Integer age; private Date birthday; private String email; public Integer getUid() { return uid; } public void setUid(Integer uid) { this.uid = uid; } public String getUname() { return uname; } public void setUname(String uname) { this.uname = uname; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } @Override public String toString() { return "User [age=" + age + ", birthday=" + birthday + ", email=" + email + ", password=" + password + ", uid=" + uid + ", uname=" + uname + "]"; } }
RegAction : package com.etc.action; import java.util.Date; import com.etc.entity.User; import com.opensymphony.xwork2.ActionSupport; public class RegAction extends ActionSupport { private User user; public User getUser() { return user; } public void setUser(User user) { this.user = user; } public String execute() { return "success"; } }
RegAction-validation.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.2//EN" "http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd"> <validators> <validator type="required"><!-- 必填字段 --> <param name="fieldName">user.uid</param> <message>uid不能为空</message> </validator> <validator type="required"> <param name="fieldName">user.age</param> <message>年龄不能为空</message> </validator> <validator type="requiredstring"> <!-- 必填字符串校验 --> <param name="fieldName">user.uname</param> <message>用户名不能为空</message> </validator> <validator type="requiredstring"> <!-- 必填字符串校验 --> <param name="fieldName">user.password</param> <message>密码不能为空</message> </validator> <validator type="int"> <!-- 整数校验器 --> <param name="fieldName">user.age</param> <param name="max">120</param> <param name="min">0</param> <message>年龄必须在${min}-${max}之间!</message> </validator> <validator type="stringlength"> <!--字符串长度校验器,根据字符数 --> <param name="fieldName">user.password</param> <param name="maxLength">20</param> <param name="minLength">6</param> <message>密码长度必须在${minLength}-${maxLength}之间!</message> </validator> <validator type="date"> <!--字符串长度校验器,根据字符数 --> <param name="fieldName">user.birthday</param> <param name="max">2016-1-1</param> <param name="min">1980-1-1</param> <message>生日必须在${min}-${max}之间!</message> </validator> <validator type="email"> <!--字符串长度校验器,根据字符数 --> <param name="fieldName">user.email</param> <message>邮箱非法!</message> </validator> <validator type="myusername"> <param name="fieldName">user.uname</param> <message>用户名不能含有中文!</message> </validator> </validators>