The struts2+jquery combination verifies the existence of a registered user
- 2020-03-30 02:47:07
- OfStack
Register interface register.jsp
The action method
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<html>
<head>
<title> The registration screen </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(' The user exists ');
} else {
$('#nametip').text(' This user does not exist ');
}
},
error : function() {
alert(" Abnormal! ");
}
});
}
</script>
</head>
<body>
<form action="login!register" method="post">
<table align="center">
<caption>
<h3>
User registration
</h3>
</caption>
<tr>
<td>
User name:
<input type="text" id="name" name="name" onblur="findByName()" />
</td>
<td>
<div id="nametip">
</div>
</td>
</tr>
<tr>
<td>
The secret Code:
<input type="text" name="password" />
</td>
</tr>
<tr>
<td>
Duplicate password:
<input type="text" name="password2" />
</td>
</tr>
<tr align="center">
<td colspan="2">
<input type="submit" value=" registered " />
<input type="reset" value=" refill " />
</td>
</tr>
</table>
</form>
</body>
</html>
The action method
public String findByName() throws IOException {
List<Person> listPerson = ms.findByName(name);
String findByNameTip;
if (listPerson.size() > 0) {
findByNameTip = "exist"; //There are users
} else {
findByNameTip = "noexist"; //No users
}
ServletActionContext.getResponse().getWriter().print(findByNameTip);
return null;
}