日期:2014-05-16 浏览次数:20413 次
<li> <div class="inputInfo"> <span>帐号</span> <input type="text" value="" class="inputText" id="name"/> </div> </li> <li> <div class="inputInfo"> <span>密码</span> <input type="password" class="inputText" id="password"/> </div> </li> <!-- 显示返回文本字符串 --> <div id="showResponseText"></div>
// JavaScript Document utils={ createRequest:function(){ var request ; try{ request = new XMLHttpRequest(); }catch(tryMS){ try{ request = new ActiveXObject("Msxml2.XMLHTTP"); }catch(otherMS){ try{ request = new ActiveXObject("Microsoft.XMLHTTP"); }catch(failed){ request = null; } } } return request; } };
// JavaScript Document register={ init:function(){ document.getElementById('name').onblur = register.checkUserName; }, checkUserName:function(){ request = utils.createRequest(); if(request == null){ alert("create request failed"); } else{ var nameValue = document.getElementById('name').value; var url = "ajax_register!checkUserName?userName="+nameValue; request.onreadystatechange = register.showUserNameStatus; request.open("post",url,true); request.send(null); } }, showUserNameStatus:function(){ if(request.readyState == 4){ if(request.status ==200){ document.getElementById("ppt").innerHTML=request.responseText; } } } }; window.onload = register.init;
<action name="ajax_register" class="customer.action.Ajax_RegisterAction"> <result type="stream"> <param name="contentType">text/plain</param> <param name="inputName">inputStream</param> </result> </action>
import java.io.InputStream; import java.io.StringBufferInputStream; import com.opensymphony.xwork2.ActionSupport; import customer.service.Register; import customer.service.RegisterImpl; public class Ajax_RegisterAction extends ActionSupport { /** * */ private static final long serialVersionUID = 1L; private InputStream inputStream; public InputStream getInputStream() { return inputStream; } public void setInputStream(InputStream inputStream) { this.inputStream = inputStream; } // service类 private Register register = new RegisterImpl(); private String userName; public String getUserName() { return userName; } // 检查userName是否重复 public String checkUserName(){ String text =""; if(!register.checkNameExist(userName)){ text = "yes"; }else{ text = "no"; } getResponseText(text); return SUCCESS; } // 获取返回文本 public void getResponseText(String text){ inputStream = new StringBufferInputStream(text); }?