日期:2014-05-17  浏览次数:20738 次

struts2中如何实现ajax的验证功能?
我的action是这样写的  之前没看过struts2  所以是在以前的代码基础上修改的  看着有点乱

前面引用包啥的我就不贴啦  下面是代码

public class UserNameCheck extends ActionSupport implements ServletRequestAware,ServletResponseAware{

 private HttpServletResponse response;      

 private HttpServletRequest request;

 public String execute() throws IOException {

   

  System.out.println("进入ajax调用");

  StudentDaoEntity studentDaoEntity = new StudentDaoEntity();

  response.setContentType("text/xml; charset=UTF-8");//设置响应文本格式

        response.setHeader("Cache-Control", "no-cache"); //设置没有缓存

        PrintWriter out = response.getWriter();    //获得输出流

        String stuNo = request.getParameter("stuNo");  

        StudentBean student= new StudentBean();

        boolean f = false;

        

        student = studentDaoEntity.findStuByID(stuNo);//这个是调用DAO

  if(student.getStuNo().equals(null) ){

   f = true;

   System.out.println(stuNo+"daixiaoyu");

  }

  out.print(f) ;

  out.flush();        //刷新输出流

  out.close();        //关闭输出流

 return "success";

 }

 public HttpServletResponse getResponse() {

  return response;

 }

 public void setResponse(HttpServletResponse response) {

  this.response = response;

 }

 public HttpServletRequest getRequest() {

  return request;

 }

 public void setRequest(HttpServletRequest request) {

  this.request = request;

 }

 public void setServletRequest(HttpServletRequest request) {

  this.request = request;

 }

 public void setServletResponse(HttpServletResponse response) {

  this.response = response;  

 } 

}

前台的ajax是这样

function checkStuNo(stuNo){

   createXMLHttp();

   

   xmlHttp.onreadystatechange = checkStuNoCallback;

   xmlHttp.open("post","baidu/UserNameCheck.action?stuNo=" + stuNo,true);

   

   xmlHttp.send(null);

  }

  function checkStuNoCallback(){

  

   if(xmlHttp.readyState == 4){

   

    var text = xmlHttp.respons