欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

struts2+jquery组合验证注册用户是否存在

程序员文章站 2022-05-14 18:24:01
注册界面 register. . 代码如下: <%@ page language="java" contenttype="text/ht...

注册界面 register.

. 代码如下:


<%@ page language="java" contenttype="text/html; charset=utf-8"%>
<html>
<head>
<title>注册界面</title>
<script type="text/javascript" src="js/jquery-1.6.js">
</script>
<script type="text/javascript">
function findbyname() {
$.ajax( {
url : 'login!findbyname',
data : {
name : $("#name").val()
},
type : 'post',
datatype : 'text',
success : function(data) {
if ('exist' === data) {
$('#nametip').text('存在该用户');
} else {
$('#nametip').text('不存在该用户');
}
},
error : function() {
alert("异常!");
}
});
}
</script>
</head>
<body>
<form action="login!register" method="post">
<table align="center">
<caption>
<h3>
用户注册
</h3>
</caption>
<tr>
<td>
用户名:
<input type="text" id="name" name="name" onblur="findbyname()" />
</td>
<td>
<p id="nametip">

</p>
</td>
</tr>

<tr>
<td>
密 码:
<input type="text" name="password" />
</td>
</tr>
<tr>
<td>
重复密码:
<input type="text" name="password2" />
</td>
</tr>
<tr align="center">
<td colspan="2">
<input type="submit" value="注册" />
<input type="reset" value="重填" />
</td>
</tr>
</table>
</form>
</body>
</html>


action方法

. 代码如下:


/**
* 查找用户是否存在
*
* @return
* @throws ioexception
*/
public string findbyname() throws ioexception {
list<person> listperson = ms.findbyname(name);
string findbynametip;
if (listperson.size() > 0) {
findbynametip = "exist"; // 存在用户
} else {
findbynametip = "noexist"; // 不存在用户
}
servletactioncontext.getresponse().getwriter().print(findbynametip);
return null;
}