struts2 + ajax 用户名登录验证(struts2+spring+ibatis)
2013-05-15 08:53
239 查看
js代码:
[javascript]view plaincopy<!-- ajax 验证 -->
<script type="text/javascript">
var xmlHttp;
var result = false;
function checkuid(){
var uname = document.all.userName.value;
if(uname.length<3){
document.all.unameMsg.innerHTML="<font color='red'>用户名太短,请重新输入!</font>";
returnfalse;
}else{
document.all.unameMsg.innerHTML="<font color='red'>用户名检测中...</font>";
if(window.ActiveXObject){
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}elseif(window.XMLHttpRequest){
xmlHttp = new XMLHttpRequest();
}
var uri = "NewHouseUserAction!execute.oj?user="+uname;
uri = encodeURI(uri);
uri = encodeURI(uri);
xmlHttp.open("post",uri,true);
xmlHttp.onreadystatechange = function(){
if(xmlHttp.readyState==4){
if(xmlHttp.status==200){
var flag = xmlHttp.responseText;
if(flag=="true"){
document.all.unameMsg.innerHTML="<font color='red'>该用户名已被使用,请您重新选择用户名!</font>";
result = false;
}else{
document.all.unameMsg.innerHTML="<font color='red'>恭喜您,该用户名可用!</font>";
result = true ;
}
}
}
}
xmlHttp.send(null);
}
}
</script>
[html]view plaincopy<s:formaction="NewHouseUserAction!developRegister.oj"onsubmit="return validateForm(this);"method="post"enctype="multipart/form-data">
<ul>
<spanstyle="white-space:pre"></span><li>
<labelclass="span_item">用户名:</label>
<inputtype="text"name="userName"value="${developUser.username}"
class="input_text"id="userName"onblur="checkuid();"/>
<divid="unameMsg"></div>
<imgsrc="../images/check_right.gif"class="li_imgtip"/>
<spanclass="li_tip">4-15个字符(包括英文字母、数字、下划线,区分大小写) </span>
<divclass="clear"></div>
</li>
其他注册代码省略。。。。。
</s:form>
action 代码:
[java]view plaincopypublic String checkUser() {
developService = (DevelopService) this.getBean("developService");
String userName = this.request().getParameter("user");
PrintWriter pw = null;
try {
pw = this.response().getWriter();
response().setContentType("html/text");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Map map = new HashMap();
map.put("username", userName.trim());
Object object = developService.getObject(
"develop.abatorgenerated_ByUser", map);
if (object!=null) {
pw.write("true");
}else {
pw.write("false");
}
pw.flush();
pw.close();
return SUCCESS;
}
struts.xml 代码(spring bean注入这一块就不贴了,直接贴页面跳转代码):
<!-- 开发商新房发布登录页面 -->
[html]view plaincopy<actionname="NewHouseUserAction"
class="com.onejia.www.web.action.DevelopAction">
<resultname="index">/index2.jsp</result>
<resultname="succeed">/jsp/registerSucceed.jsp</result>
<resultname="register">/jsp/developRegister.jsp</result>
<resultname="login">/jsp/login.jsp</result>
<resultname="success">/jsp/developRegister.jsp</result>
</action>
已经测试成功!!
[javascript]view plaincopy<!-- ajax 验证 -->
<script type="text/javascript">
var xmlHttp;
var result = false;
function checkuid(){
var uname = document.all.userName.value;
if(uname.length<3){
document.all.unameMsg.innerHTML="<font color='red'>用户名太短,请重新输入!</font>";
returnfalse;
}else{
document.all.unameMsg.innerHTML="<font color='red'>用户名检测中...</font>";
if(window.ActiveXObject){
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}elseif(window.XMLHttpRequest){
xmlHttp = new XMLHttpRequest();
}
var uri = "NewHouseUserAction!execute.oj?user="+uname;
uri = encodeURI(uri);
uri = encodeURI(uri);
xmlHttp.open("post",uri,true);
xmlHttp.onreadystatechange = function(){
if(xmlHttp.readyState==4){
if(xmlHttp.status==200){
var flag = xmlHttp.responseText;
if(flag=="true"){
document.all.unameMsg.innerHTML="<font color='red'>该用户名已被使用,请您重新选择用户名!</font>";
result = false;
}else{
document.all.unameMsg.innerHTML="<font color='red'>恭喜您,该用户名可用!</font>";
result = true ;
}
}
}
}
xmlHttp.send(null);
}
}
</script>
[html]view plaincopy<s:formaction="NewHouseUserAction!developRegister.oj"onsubmit="return validateForm(this);"method="post"enctype="multipart/form-data">
<ul>
<spanstyle="white-space:pre"></span><li>
<labelclass="span_item">用户名:</label>
<inputtype="text"name="userName"value="${developUser.username}"
class="input_text"id="userName"onblur="checkuid();"/>
<divid="unameMsg"></div>
<imgsrc="../images/check_right.gif"class="li_imgtip"/>
<spanclass="li_tip">4-15个字符(包括英文字母、数字、下划线,区分大小写) </span>
<divclass="clear"></div>
</li>
其他注册代码省略。。。。。
</s:form>
action 代码:
[java]view plaincopypublic String checkUser() {
developService = (DevelopService) this.getBean("developService");
String userName = this.request().getParameter("user");
PrintWriter pw = null;
try {
pw = this.response().getWriter();
response().setContentType("html/text");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Map map = new HashMap();
map.put("username", userName.trim());
Object object = developService.getObject(
"develop.abatorgenerated_ByUser", map);
if (object!=null) {
pw.write("true");
}else {
pw.write("false");
}
pw.flush();
pw.close();
return SUCCESS;
}
struts.xml 代码(spring bean注入这一块就不贴了,直接贴页面跳转代码):
<!-- 开发商新房发布登录页面 -->
[html]view plaincopy<actionname="NewHouseUserAction"
class="com.onejia.www.web.action.DevelopAction">
<resultname="index">/index2.jsp</result>
<resultname="succeed">/jsp/registerSucceed.jsp</result>
<resultname="register">/jsp/developRegister.jsp</result>
<resultname="login">/jsp/login.jsp</result>
<resultname="success">/jsp/developRegister.jsp</result>
</action>
已经测试成功!!
相关文章推荐
- struts2 + ajax 用户名登录验证(struts2+spring+ibatis)
- Extjs与struts2整合,ajax请求登录验证
- ajax用户名验证和登录方法的整合
- ajax配合struts2(ssh2)实现无刷新验证用户名是否唯一
- ajax实现验证用户名是否已经存在(struts2)
- struts2中使用ajax验证邮箱(用户名)是否被注册
- ajax实现验证用户名是否已经存在(struts2)
- Ajax实现登录时的用户名验证(Servlet 响应)
- jquery的ajax登录/注册验证用户名是否被占用
- 学习struts2建bbs总结五:使用jquery+ajax验证用户名是否存在以及struts效验信息不断重复的问题
- 关于Struts2和ajax的登录验证问题
- struts2基于ajax的验证用户名是否被注册示例
- struts2中使用ajax验证邮箱(用户名)是否被注册
- ajax,jsp,struts2验证用户名是否存在
- struts2 ajax验证用户名是否存在
- struts2中使用ajax验证邮箱(用户名)是否被注册
- AJAX 验证 用户名验证 手机号码正则验证
- JQuery遮罩层登录界面实现(AJAX实现登录验证,源码下载)
- Ajax 用户登录验证
- Ajax注册表单用户名实时验证