struts2 + ajax 用户名登录验证(struts2+spring+ibatis)

来源:互联网 发布:淘宝欧美风店铺 编辑:程序博客网 时间:2024/05/16 06:20
js代码:
<!-- 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>";  return false; }else{   document.all.unameMsg.innerHTML="<font color='red'>用户名检测中...</font>"; if(window.ActiveXObject){   xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");  }else if(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>



<s:form action="NewHouseUserAction!developRegister.oj" onsubmit="return validateForm(this);" method="post" enctype="multipart/form-data"><ul><li><label class="span_item">用户名:</label><input type="text" name="userName" value="${developUser.username}"class="input_text" id="userName" onblur="checkuid();" /><div id="unameMsg"></div><img src="../images/check_right.gif" class="li_imgtip" /><span class="li_tip">4-15个字符(包括英文字母、数字、下划线,区分大小写) </span><div class="clear"></div></li>    其他注册代码省略。。。。。</s:form>



action 代码:
public 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 blocke.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注入这一块就不贴了,直接贴页面跳转代码):

<!-- 开发商新房发布登录页面 -->

<action name="NewHouseUserAction"         class="com.onejia.www.web.action.DevelopAction">                <result name="index">/index2.jsp</result>                <result name="succeed">/jsp/registerSucceed.jsp</result>                <result name="register">/jsp/developRegister.jsp</result>                <result name="login">/jsp/login.jsp</result>                <result name="success">/jsp/developRegister.jsp</result>         </action>



已经测试成功!!