利用Struts2实现简单的用户注册功能,并具备输入字符的校验功能。
以下index.jsp为首页,显示注册表单:
<%--
首页,显示注册表单
--%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Java web实验五</title>
</head>
<body>
<form action="register.action" method="post">
<table>
<tr>
<td>用户名:</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="upassword"></td>
</tr>
<tr>
<td>确认密码:</td>
<td><input type="password" name="repassword"></td>
</tr>
<tr>
<td>姓名:</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>年龄:</td>
<td><input type="text" name="age"></td>
</tr>
<tr>
<td>出生日期:</td>
<td><input type="text" name="birth"></td>
</tr>
<tr>
<td>邮箱地址:</td>
<td><input type="text" name="email"></td>
</tr>
<tr>
<td><input type="submit" value="提交"></td>
<td><input type="reset" value="重置"></td>
</tr>
</table>
<s:actionerror/>
</form>
</body>
</html>
以下部分为ShowUserInfo.jsp,显示用户注册后的信息:
<%--
Created by IntelliJ IDEA.
User: 23832
Date: 2017/5/15
Time: 19:33
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Java web实验五</title>
</head>
<body>
用户名:${username}<br>
密码:${upassword}<br>
确认密码:${repassword}<br>
姓名:${name}<br>
年龄:${age}<br>
出生日期:${birth}<br>
邮箱地址:${email}<br>
</body>
</html>
以下为RegisterAction.java,注册的业务逻辑部分:
package com;
import com.opensymphony.xwork2.ActionSupport;
import java.util.regex.Pattern;
public class RegisterAction extends ActionSupport {
private String username;
private String upassword;
private String repassword;
private String name;
private String age;
private String birth;
private String email;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getUpassword() {
return upassword;
}
public void setUpassword(String upassword) {
this.upassword = upassword;
}
public String getRepassword() {
return repassword;
}
public void setRepassword(String repassword) {
this.repassword = repassword;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getBirth() {
return birth;
}
public void setBirth(String birth) {
this.birth = birth;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public void validate() {//字符校验
if (upassword == null || "".equals(upassword)) {//判断输入密码是否为空
this.addActionError("密码必须输入");
}
if (repassword == null || "".equals(repassword)) {//判断确认密码是否为空
this.addActionError("确认密码必须输入");
}
if (upassword != null && repassword != null && !repassword.equals(upassword)) {//判断密码和确认密码是否相同
this.addActionError("密码必须和确认密码相同");
}
Pattern pattern = Pattern.compile("[0-9]*");
if (age==null||"".equals(age)){//判断年龄是否为空
this.addActionError("年龄不能为空");
}
if (age!=null&&!pattern.matcher(age).matches()) {//利用正则表达式判断年龄是否为数字
this.addActionError("年龄必须为数字");
}
if (email==null||"".equals(email)){//判断邮箱是否为空
this.addActionError("邮箱不能为空");
}
if (email!=null&&!Pattern.matches("[a-zA-Z0-9_-]*@([a-zA-Z0-9-_]+"+"\\.)+(com|gov|net|com\\.cn|edu\\.cn)",email)) {//利用正则表达式判断邮箱格式是否正确
this.addActionError("邮箱格式不正确");
}
}
public String execute() throws Exception {
return "success";
}
}
以下为Struts2的配置部分:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="struts2" extends="struts-default">
<action name="register" class="com.RegisterAction">
<!--定义处理结果与试图资源之间的关系-->
<result name="success">/ShowUserInfo.jsp</result>
<result name="input">/index.jsp</result>
</action>
</package>
</struts>
web.xml文件配置就不列出了。