RegisterAction代码:package com.mr.jsjwl.action;import java.util.Map;import com.mr.jsjwl.dao.UserDao;
import com.mr.jsjwl.dao.UserDaoImpl;
import com.mr.jsjwl.model.User;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;public class RegisterAction extends ActionSupport{
private UserDao userDao = new UserDaoImpl();
private User user = new User(); //注册提交的用户对象
private String repwd; //重复密码
private String inputCertCode; //验证码
public UserDao getUserDao() {
return userDao;
}
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String getRepwd() {
return repwd;
}
public void setRepwd(String repwd) {
this.repwd = repwd;
}
public String getInputCertCode() {
return inputCertCode;
}
public void setInputCertCode(String inputCertCode) {
this.inputCertCode = inputCertCode;
}
@Override
public String execute() throws Exception{
userDao.add(user);//保存注册的对象
return SUCCESS;
}
@Override
public void validate(){
//验证码验证
if(null == inputCertCode || "".equals(inputCertCode)){
this.addFieldError("inputCertCode", "验证码不能为空!");
}else{
Map session = ActionContext.getContext().getSession();
String certCode = session.get("certCode").toString();
if( ! certCode.equals(this.inputCertCode)){
this.addFieldError("inputCertCode", "验证码不正确!");
}
}
}
}
import com.mr.jsjwl.dao.UserDaoImpl;
import com.mr.jsjwl.model.User;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;public class RegisterAction extends ActionSupport{
private UserDao userDao = new UserDaoImpl();
private User user = new User(); //注册提交的用户对象
private String repwd; //重复密码
private String inputCertCode; //验证码
public UserDao getUserDao() {
return userDao;
}
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String getRepwd() {
return repwd;
}
public void setRepwd(String repwd) {
this.repwd = repwd;
}
public String getInputCertCode() {
return inputCertCode;
}
public void setInputCertCode(String inputCertCode) {
this.inputCertCode = inputCertCode;
}
@Override
public String execute() throws Exception{
userDao.add(user);//保存注册的对象
return SUCCESS;
}
@Override
public void validate(){
//验证码验证
if(null == inputCertCode || "".equals(inputCertCode)){
this.addFieldError("inputCertCode", "验证码不能为空!");
}else{
Map session = ActionContext.getContext().getSession();
String certCode = session.get("certCode").toString();
if( ! certCode.equals(this.inputCertCode)){
this.addFieldError("inputCertCode", "验证码不正确!");
}
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//EN"
"http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd"> <validators>
<!-- 用户名验证 -->
<field name="user.userName">
<field-validator type="requiredstring">
<param name="trim">true</param>
<message>用户名不能为空!</message>
</field-validator>
<field-validator type="regex">
<param name="expression"><![CDATA[(\w{4,8})]]></param>
<message>账号必须是长度为4-8的数字或字母!</message>
</field-validator>
</field>
<!-- 密码验证 -->
<field name="user.password">
<field-validator type="requiredstring">
<param name="trim">true</param>
<message>密码不能为空!</message>
</field-validator>
<field-validator type="regex">
<param name="expression"><![CDATA[(\w{4,8})]]></param>
<message>密码必须长度为4-8的数字或字母!</message>
</field-validator>
</field>
<field name="repwd">
<field-validator type="requiredstring" short-circuit="true">
<param name="trim">true</param>
<message>重复密码不能为空!</message>
</field-validator>
<field-validator type="fieldexpression">
<param name="expression"><![CDATA[(repwd==user.pwd)]]></param>
<message>两次输入的密码要一致!</message>
</field-validator>
</field>
</validators>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd"><struts>
<package name="admin" namespace="/admin" extends="struts-default">
<action name="LoginAction" class="com.mr.jsjwl.action.LoginAction">
<result name="success" type="redirect">/admin/admin_frame.jsp</result>
<result name="input">/admin/login.html</result>
</action>
</package>
<package name="front" namespace="/" extends="struts-default">
<default-action-ref name="index" />
<action name="index">
<result>/index.jsp</result>
</action>
<!-- 用户注册 -->
<action name="RegisterAction" class="com.mr.jsjwl.action.RegisterAction">
<result name="success" type="redirect">/admin/admin_frame.jsp</result>
<result name="input">/admin/login.html</result>
</action>
</package>
</struts>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030"/>
<title>计算机网络在线课堂</title>
<s:head />
<link href="res/Theme/CSS/Theme.css" rel="stylesheet" type="text/css" />
<script src="res/Js/Hidden.js" type="text/javascript"></script>
</head>
<body>
<p><a href="login.jsp">用户登录</a>|用户注册</p>
<s:form action="RegisterAction" method="post" namespace="/" theme="simple">
<I><font face="楷体_GB2312" color="#FF0000"><s:fielderror />
</I>
<table>
<tr>
<td style="text-align:right">
请输入账号
</td>
<td>
<s:textfield name="user.userName" label="账号" />
</td>
</tr>
<tr>
<td style="text-align:right">
请输入密码
</td>
<td>
<s:password name="user.password" label="密码" />
</td>
</tr>
<tr>
<td style="text-align:right">
请再次输入密码
</td>
<td>
<s:password name="repwd" label="重复密码" />
</td>
</tr>
<tr>
<td style="text-align:right">
请输入验证码
</td>
<td>
<s:textfield name="inputCertCode" label="验证码" cssStyle="width:100px"/>
<img src="other/makeCertPic.jsp">
</td>
</tr>
<tr>
<td style="text-align:right">
<s:submit value="注册" />
</td>
<td style="text-align:left">
<s:reset value="取消"/>
</td>
</tr>
</table>
</s:form>
<s:fielderror cssStyle="color:red"/>
</body>
</html>