Ajax+Struts2用户注册功能实现
详细请参考源码(Github):https://github.com/QQ3330447288/ajaxRegister
1、目录结构
2、截图
3、核心代码:
register.jsp
<script>
var code = "";
var nameIsOk = false;
var codeIsOK = false;
//检查账号是否已经存在
function checkUserName(input) {
//alert(input.value);
var name = input.value;
$.post("<%=path%>/checkUserName.action", {
userName : name
}, function(data) {
//alert(data);
if (data == 1) {
$('#tip').html("当前账号已经存在!");
nameIsOk = false;
} else {
$('#tip').html("");
nameIsOk = true;
}
});
}
//生成随机验证码
function createCode(sum) {
code = "";
for (var i = 0; i <sum; i++) {
var num = parseInt(Math.random() * 10) % 10;
code += num;
}
return code;
}
//发送随机验证码给短信服务商
function sendCode(input) {
var phone = $("#phone").val();
var pattern = /^1[34578]\d{9}$/;
if(phone==""||phone==null||!pattern.test(phone)){
alert("手机号为空或格式错误!");
return;
}else{
input.setAttribute("disabled", "disabled");
var count = 60;
var time = setInterval(function(){
input.value=count+"s";
count--;
if(count<0){
input.removeAttribute("disabled");
clearInterval(time);
input.value="发送";
}
},1000);
//生成6位验证码
var code = createCode(6);
//alert(code);
$.post("<%=path%>/sendCode.action", {
phone : phone,
code : code
}, function(data) {
});
}
}
//注册功能实现
function register() {
var userName = $("#userName").val();
var pwd = $("#pwd").val();
var pwd_sure = $("#pwd_sure").val();
var phone = $("#phone").val();
var codeInput = $("#codeInput").val();
var pattern = /^1[34578]\d{9}$/;
if($.trim(userName)==""||userName==null){
alert("用户名不能为空!");
return;
}else if(pwd==""||pwd==null){
alert("密码不能为空!");
return;
}else if(pwd.length<6){
alert("密码至少6位!");
return;
}
else if(pwd_sure==""){
alert("确认密码不能为空!");
return;
}
else if(pwd != pwd_sure){
alert("两次输入密码不一致!");
return;
}else if(phone==""||phone==null){
alert("手机号码不能为空!");
return;
}
else if(!pattern.test(phone)){
alert("手机号格式不正确!");
return;
}else if(codeInput==""||codeInput==null){
alert("验证码不能为空!");
return;
}
if(code == codeInput){
//alert("验证码输入正确!");
codeIsOK = true;
//alert(codeIsOK);
}else{
alert("验证码输入错误!");
codeIsOK = false;
}
//alert(nameIsOk);
//alert(codeIsOK);
if(nameIsOk==true&&codeIsOK==true){
//alert(1);
$.post("<%=path%>/addUser.action", {
userName:userName,
pwd : pwd,
phone : phone
},function(data){
//alert(data);
if(data==1){
alert("注册成功!");
window.location.href="<%=path%>/page/home/login.jsp";
}else{
alert("注册失败!");
}
});
}
}
//键盘回车提交表单
/*
$(document).ready(function() {
$(document).keydown(function(event) {
if (event.keycode == 13) {
register();
}
})
})
*/
</script>
CheckUserAction.java:
public class CheckUserAction extends ActionSupport {
/**
* 查看是否已经存在
* @throws IOException
*/
public void checkUserName() throws IOException {
// ServletActionContext类来自strut2-core-2.1.6.jar
// 获取request对象
HttpServletRequest request = ServletActionContext.getRequest();
String userName = request.getParameter("userName");
// System.out.println(userName);
// 获取response对象
HttpServletResponse response = ServletActionContext.getResponse();
// 设置response返回数据内容的编码
response.setContentType("text/html,charset=utf-8");
PrintWriter out = response.getWriter();
// out.print(111);
if (isExists(userName)) {
out.print("1");
} else {
out.print("0");
}
out.flush();
out.close();
}
public Boolean isExists(String userName) {
String sql = "SELECT *FROM user WHERE username=?";
BaseDao baseDao = new BaseDao();
List<Object> param = new ArrayList<Object>();
param.add(userName);
try {
List<User> list = baseDao.getModelsWidthSqlAndParam(sql, param, new User());
if (list.size() > 0) {
return true;
} else {
return false;
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
}
SendCodeAction.java:
public class SendCodeAction extends ActionSupport {
public void sendCode() {
HttpServletRequest request = ServletActionContext.getRequest();
String code = request.getParameter("code");
String phone = request.getParameter("phone");
// System.out.println(code);
// System.out.println(phone);
SDKDemo.sendCodeWithTel(phone, code);
}
}
AddUserAction.java:
public class AddUserAction extends ActionSupport {
public void addUser() throws IOException {
HttpServletRequest request = ServletActionContext.getRequest();
String userName = request.getParameter("userName");
String pwd = Md5.md5(request.getParameter("pwd"), "Thanlon");
// System.out.println(pwd);
String phone = Md5.md5(request.getParameter("phone"), "Thanlon");
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
// System.out.println(userName+phone+pwd);
if (iSaddUser(userName, pwd, phone)) {
out.print(1);
} else {
out.print(0);
}
out.flush();
out.close();
}
private Boolean iSaddUser(String userName, String pwd, String phone) {
String sql = "INSERT user(username,pwd,phone) VALUES(?,?,?)";
List<Object> parames = new ArrayList<Object>();
parames.add(userName);
parames.add(pwd);
parames.add(phone);
BaseDao dao = new BaseDao();
try {
int count = dao.executeUpdate(sql, parames);
return count > 0;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
}