Spring+dwr
Spring配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="empService" class="com.hibernate.dao.EmpService" autowire="byName"></bean>
</beans>
dwr.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC
"-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN"
"http://getahead.org/dwr/dwr20.dtd">
<dwr>
<allow>
<create creator="new" javascript="EmpService">
<param name="class" value="com.hibernate.dao.EmpService" ></param>
<include method="checkName" /> <!-- 只公布该方法 -->
</create>
<!-- 与Spring框架集成 -->
<create creator="spring" javascript="empService">
<param name="beanName" value="empService" />
<include method="checkName" />
</create>
</allow>
</dwr>
web.xml
<!-- DWR -->
<servlet>
<servlet-name>dwr-invoker</servlet-name>
<servlet-class>
org.directwebremoting.servlet.DwrServlet
</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dwr-invoker</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/classes/applicationContext.xml
</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
DAO:EmpService.java
public boolean checkName(String userName) {
String hql = "from ZzEmployee e where e.empName='"+userName+"'";
List list_emp = getHibernateTemplate().find(hql);
if (list_emp != null & !list_emp.isEmpty()) {
return true;
} else {
return false;
}
}
页面
<script type="text/javascript" src="prototype.js"></script>
<script type="text/javascript" src="dwr/engine.js"></script>
<script type="text/javascript" src="dwr/util.js"></script>
<script type="text/javascript" src="dwr/interface/EmpService.js"></script>
<script type="text/javascript">
function callback(str){
if(str==true){
$('msg').innerHTML = "用户名已存在!";
}else{
$('msg').innerHTML = "用户名可用!";
}
}
function callService(){
var fmsg = $('empName').value;
EmpService.checkName(fmsg,callback);
}
</script>
<html:text property="empName" onblur="callService()"/><div id="msg"></div>