I use dwr2.0.1 and spring2.5, the dwr remote method calling is ok for me.
1. web.xml
Code:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>mynode</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mynode</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<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>
<init-param>
<param-name>classes</param-name>
<param-value>
cn.mynode.validation.AjaxFormValidator
</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dwr-invoker</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<jsp-config>
<taglib>
<taglib-uri>http://java.sun.com/jsp/jstl/core</taglib-uri>
<taglib-location>/WEB-INF/tld/c.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>http://java.sun.com/jsp/jstl/fmt</taglib-uri>
<taglib-location>/WEB-INF/tld/fmt.tld</taglib-location>
</taglib>
</jsp-config>
</web-app>
cn.mynode.validation.AjaxFormValidator is my java class used for dwr service.
2. I use annotation configure instead of dwr.xml, Here is my Java class
Code:
@RemoteProxy(name = "AjaxFormValidatorJS", creator = SpringCreator.class, creatorParams = @Param(name = "beanName", value = "ajaxFormValidator"))
public class AjaxFormValidator implements ApplicationContextAware {
//@RemoteMethod
public String sayHello(String msg) {
return "FormValidator:Hello world " + msg + "!";
}
}
3. in spring configurateion a bean is defined like this,
applicationContext.xml
Code:
<bean id="ajaxFormValidator" class="cn.mynode.validation.AjaxFormValidator" >
</bean>
4. use javascirpt in my jsp file like this:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title><spring:message code="form.title"/></title>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<link href="<c:url value="/css/style.css"/>" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="<c:url value="/dwr/util.js"/>"></script>
<script type="text/javascript" src="<c:url value="/dwr/engine.js"/>"></script>
<script type="text/javascript" src="<c:url value="/dwr/interface/AjaxFormValidatorJS.js"/>"></script>
<script type="text/javascript" src="<c:url value="/js/ajaxValidate.js"/>"></script>
</head>
<body>
<div id="main">
<div id="topmenu">
</div>
<div id="content">
<form:form acceptCharset="UTF-8">
<div>
<form:input path="userId" onchange="validate(this)" />
</div>
<div>
<form:label path="email">email:</form:label>
<form:input path="email" size="40"/>
<form:errors path="email" cssClass="error" />
</div>
<div>
<input type="submit" value="创建用户" />
</div>
</form:form>
</div>
</div>
</body>
</html>
5. a ajaxValidate.js file like this:
Code:
function validate(inputArray) {
AjaxFormValidatorJS.sayHello("sammi", handleValidationResponse);
}
function handleValidationResponse(response) {
alert(response);
}