1:下载dwr.jar放在 WebRoot\WEB-INF\lib
2:页面
<%@ page language="java" pageEncoding="UTF-8"%>
<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/
checkuser.js'></script>
<script type='text/javascript'>
function check(form)
{
var id=document.getElementById("id").value;
if(id=="new")
{
regUserChked(form);
}
else
{
RegUserChked(form);
}
}
function regUserChked(form){
var username= document.getElementById("username").value;
if(username=="")
{
alert("用户名不能为空!")
return false;
}
checkuser.isExist(username,showData);//showData回调函数
}
function RegUserChked(form){
var id=document.getElementById("id").value;
var username= document.getElementById("username").value;
if(username=="")
{
alert("用户名不能为空!")
return false;
}
checkuser.IsExistUpdate(username,id,ShowData); //showData回调函数
}
function showData(data){
if(data){
alert('该用户已被注册了!');
}else{
alert('该用户未被注册了!');
}
}
function ShowData(data){
if(data){
alert('该用户已被注册了!');
}else{
alert('可以更新!');
}
}
</script>
</head>
<body>
<html:form action="/acctount" onsubmit="javascript:return Juge(this);">
<input type="hidden" name="id" value="<%=id%>">
username : <html:text property="username" value="<%=Username %>" onblur="check(this)" />
<html:errors property="username"/><br/>
password : <html:text property="password" value="<%=password %>"/>
<html:errors property="password"/><br/>
<html:submit/><html:cancel/>
</html:form>
</body>
</html>
3:
dwr.xml配置
<dwr>
<allow>
<!-- 直接取操作类 <create creator="new" javascript="checkuser" scope="application">
<param name="class" value="com.henry.dao.daospring"/>
<include method="isExist"/>
</create>
-->
<!-- 间接取bean -->
<create creator="spring" javascript="
checkuser">
<param name="beanName" value="
accountDAO" />
<include method="isExist"/>
<include method="IsExistUpdate"/>
</create>
</allow>
</dwr>
4:
applicationContext.xml
<bean id="
accountDAO" class="com.henry.dao.AccountDao">
<property name="dataSource">
<ref local="dataSource"/>
</property>
<property name="sqlMapClient">
<ref bean="sqlMapClient"/>
</property>
</bean>
5:
web.xml
<servlet>
<servlet-name>dwr-invoker</servlet-name>
<display-name>DWR Servlet</display-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/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
6:1)以上dwr通过间接
spring-> applicationContext.xml的bean
<!-- 间接取bean -->
<create creator="spring" javascript="
checkuser">
<param name="beanName" value="
accountDAO" />
<include method="isExist"/>
<include method="IsExistUpdate"/>
</create>
2)如果直接取
spring-> applicationContext.xml的bean
<!-- 直接取操作类 <create creator="new" javascript="checkuser" scope="application">
<param name="class" value="com.henry.dao.daospring"/>
<include method="isExist"/>
</create>
-->
com.henry.dao.daospring:如下
package com.henry.dao;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import com.henry.dto.accountDto;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class daospring {
ApplicationContext context=new ClassPathXmlApplicationContext("com/henry/dbxml/applicationContext.xml");
AccountDao testDAOImpl=(AccountDao)context.getBean("AccountDAO");
public List getAccountsByName(String username)
{
return testDAOImpl.getListByName(username);
}
/*
* update check
*/
public boolean
IsExistUpdate(String username)
{
accountDto account=new accountDto();
List list=this.getAccountsByName(username);
if(list.size()==0)
{
return false;
}
else
{
//if(account.getId()==Integer.parseInt(id))
//{
// return false;
//}
//else
//{
return true;
//}
}
}
public String getIdByName(String username,String id)
{
accountDto account=new accountDto();
List list=this.getAccountsByName(username);
if(list.size()==0)
{
return null;
}
else
{
return Integer.toString(((accountDto)list.get(0)).getId()) ;
}
}
public boolean
IsExist(String username) {
if(testDAOImpl.isExist(username))
{
return true;
}
else
{
return false;
}
}
//new or add check
public boolean isExist(String username) {
if(testDAOImpl.isExist(username))
{
return true;
}
else
{
return false;
}
}
public accountDto getAccountById(String id)
{
return testDAOImpl.getById(id);
}
}
7:出现问题struts---->action--->findforward跳转页面------该页面dwr取spring-->
applicationContext.xml的bean变成无效