使用jquery验证用户名称是否重复(jquery+sturts)

jsp页面为:

<html>
<head>
<script src="js/jquery-1.2.js" type="text/javascript"></script> 
var flag="true"; //判断用户名称是否重复 

function validatorloginName(){
 
  var loginName=document.getElementById("loginName").value;
  $j.ajax({ type: "POST", 
          url: "${pageContext.request.contextPath}/validatorLoginName.do", 
          data: "loginName="+loginName, 
          async: false ,
          success: function(data){  
    if(data=="true"){
     errormessage.innerHTML="用户名可用"
    flag=true;
    }else{
     errormessage.innerHTML="用户名已存在";
     flag=false;
    }
   }       
        });
}

function mysubmit(){

    if(flag=="true"){
      alert("用户名已经存在或输入格式不正确"); 
 }else{
    document.getElementById("myform").submit();
 }

}

</head>

<body>

    <FORM id="myform" action="registerManager.do" method="post">

            <INPUT class=text id="loginName" title=通行证用户名 size=18
          name="loginName" onblur="validatorloginName()">
         <SPAN id="errormessage"></SPAN>

        <INPUT id=confirm type="button" value=注册帐号 onclick="mysubmit()"/>

    <FORM>

</body>

</html>



struts的action为:

public class ValidatorLoginNameAction extends org.springframework.web.struts.ActionSupport {
 /*
  * Generated Methods
  */

 /** 
  * Method execute
  * @param mapping
  * @param form
  * @param request
  * @param response
  * @return ActionForward
  */
 public ActionForward execute(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response) throws Exception{
  // TODO Auto-generated method stub
  System.out.println("-------");
  IRegisterManagerService registerManager = (IRegisterManagerService) this.getService("registerManagerServiceImpl");  
  String loginName=request.getParameter("loginName");
  
  boolean bool=registerManager.validatorName(loginName);//通过Service验证该用户是否存在
  
  if(bool==true){
   response.getWriter().write("true");//此值jquery可以接收到
  }else{
   
   response.getWriter().write("false");
  }
  return null;
 }
 /**
  * <p>
  * 获取业务服务对象;
  * </p>
  * 
  * @param serviceKey
  *            服务对象ID;
  * @return
  */
 public  Object getService(String serviceKey) {
  return this.getWebApplicationContext().getBean(serviceKey);
 }
 
 protected static Logger getLog() {
  return Logger.getInstance();
 }
}

你可能感兴趣的:(jquery,Ajax,Web,jsp,struts)