作者:々上善若水々 UserAction: package com.vo; import org.hibernate.Query; import org.hibernate.Session; import com.HibernateSessionFactory; public class UserAction { private Session session; public UserAction() { session = HibernateSessionFactory.getSession(); } //判断用户是否存在方法的方法 public User exists(String username) { User entity = null; Query q = session.createQuery("from User u where u.username = ? "); q.setString(0, username); if(q.list().size() > 0) { entity = (User) q.list().get(0); } return entity; } } index.jsp: <% @ page pageEncoding="GB18030" %> < html > < head > < title > test </ title > < script type ='text/javascript' src ='/ajax01/dwr/interface/service.js' ></ script > < script type ='text/javascript' src ='/ajax01/dwr/engine.js' ></ script > < script type ="text/javascript" > //定义一个方法,用于前台调用 function exists() { var name = document.getElementById("username"); //action的exists方法,可以直接调用,第一参数是需要传入的值 //第二参数是回调函数(异步处理嘛) service.exists(name.value,callback); } //回调函数(形参是service.exists方法的返回值,这里是vo function callback(user) { if(user == null) { alert('y'); } else { alert('n'); } } </ script > < body > < form action ="#" method ="post" > < input type ="text" name ="username" id ="username" onblur ="exists();" />< div id ="username_info" ></ div > < input type ="submit" /> </ form > </ body > </ html > 源码下载