使用Struts2 spring hibernate框架開發項目---------驗證用戶名是否可用
使用三大框架開發項目中,當我們注冊用戶時,往往要求數據庫中的用戶名是唯一的,所以我們需要在注冊時進行用戶名驗證操作,該操作是如何實現的呢?下面的案例詳細介紹此內容。壞帳
Insert.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'inseret.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">淘寶女裝夏裝新款
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript"
src="${pageContext.request.contextPath}/js/jquery-1.6.js"></script>
<script type="text/javascript">
function checkUserName(){
var name=$("#uname").val();
$.ajax({
type:"post",
url:"csdn/user_checkUserName",
data:"user.name="+name,
success:function(data){
if(data.user==null){
$("#checkname").html("<font color='red'>用戶名可用</font>");
}else{
$("#checkname").html("<font color='red'>用戶名不可用</font>");
}
},
error:function(jsonmsg){
},
dataType:"json"
});
}
</script>
</head>
<body>
<h2>
用戶登錄
</h2>
<s:form action="user_insert.action" namespace="/csdn">
<s:textfield label="用戶名" name="user.name" id="uname" ></s:textfield>
<span id="checkname"></span>
<input type="button" onclick="checkUserName()" value="驗證用戶名"/>
<s:textfield label="密碼" name="user.pass"></s:textfield>
<s:submit value="添加"></s:submit>
</s:form>
</body>
</html>