首页 > 代码库 > Struts2+Jquery+实现Ajax无刷新验证用户名是否存在

Struts2+Jquery+实现Ajax无刷新验证用户名是否存在

小弟之前一直搞不明白Struts2+Jquery+实现Ajax无刷新验证用户名该怎么弄,后来经过努力,终于搞定了,应注意struts配置文件的内容,好了直接上代码了

页面代码

<body>      <s:form action="registAction" method="post" namespace="/user">          <table>              <tr>                  <td>UserName</td>                              <td id="t1"><input type="text" name="userName" id="userName"  /></td>                             <td><input type="button" id="test" value="Test" /><div id="tip1"></div></td>                         </tr>              <tr>                  <td>Password</td>                              <td id="t2" colspan="2"><input type="password" name="userPwd1" id="userPwd1" /></td>                         </tr>              <tr>                  <td>Password AG</td>                               <td id="t3" colspan="2"><input type="password" name="userPwd2" id="userPwd2" /></td>                         </tr>              <tr>                  <td colspan="3">                      <input type="submit" value="submit" />                      <input type="reset" value="reset" />                  </td>              </tr>          </table>      </s:form>  </body>

javaScript代码:

$(document).ready(function(){       var inputUserNameObj = $("#userName");       var inputUserPwd1Obj = $("#userPwd1");       var inputUserPwd2Obj = $("#userPwd2");            $("#userName").blur(function(){           var text = inputUserNameObj.val();           $.post("testAction.action?userName="+text,null,function(response){                if(response=="用户名已经存在"){             document.getElementById("tip1").innerHTML="<font color=‘red‘>"+response+"</font>";            }else{             document.getElementById("tip1").innerHTML="<font color=‘blue‘>"+response+"</font>";            }        });       });          });

Action代码:

public class TestAction extends ActionSupport{         HttpServletRequest hsr;         String result;              public HttpServletRequest getHsr() {           return hsr;       }         public void setHsr(HttpServletRequest hsr) {           this.hsr = hsr;       }         public String getResult() {           return result;       }         public void setResult(String result) {           this.result = result;       }         public String test() throws IOException{                 Map<String, String> map = new HashMap<String, String>();                      ActionContext ac = ActionContext.getContext();           HttpServletRequest req = (HttpServletRequest)ac.get(ServletActionContext.HTTP_REQUEST);           HttpServletResponse res = (HttpServletResponse)ac.get(ServletActionContext.HTTP_RESPONSE);           String userName = req.getParameter("userName");           map.put("userName", userName);                         res.setContentType("text/html;charset=UTF-8");                   PrintWriter w = res.getWriter();                  if("jack".equals(userName)){                // 将要返回的map对象进行json处理                  JSONObject jo = JSONObject.fromObject(map);                  System.out.println("进入了.....");            // 调用json对象的toString方法转换为字符串然后赋值给result                  this.result = jo.toString();                  w.println("用户名已经存在");           }else{               w.println("用户名不存在");           }           w.close();                   return "success";       }       }

Struts配置:

<package name="ajax" extends="json-default" >          <!-- 检验用户名是否存在 -->          <action name="testAction" class="com.hisoft.jqt.action.TestAction" method="test">              <!-- 返回类型为json 在sjon-default中定义 -->              <result type="json" name="success">                  <!-- root的值对应要返回的值的属性 -->                  <!-- 这里的result值即是 对应action中的 result -->                  <param name="root">result</param>              </result>          </action>      </package>