1 Web.xml 配置
<
servlet
>
<
servlet-name
>
dwr-invoker
</
servlet-name
>
<
servlet-class
>
org.directwebremoting.spring.DwrSpringServlet
</
servlet-class
>
<
init-param
>
<
param-name
>
debug
</
param-name
>
<
param-value
>
true
</
param-value
>
</
init-param
>
<
load-on-startup
>
3
</
load-on-startup
>
</
servlet
>
<servlet-mapping>
<servlet-name>dwr-invoker</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
这里有个小技巧.查看Dwr源码,在
DwrSpringServlet中它会查找Spring的WebApplicationContext,如下:
WebApplicationContext webappContext = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
container = new SpringContainer();
container.setBeanFactory(webappContext);
ContainerUtil.setupDefaults(container, servletConfig);
ContainerUtil.setupFromServletConfig(container, servletConfig);
所以,你必须在Web.xml中加上Spring的ContextLinstener才能正常使用Dwr:
<!--Configurate Spring first!-->
<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>
<servlet>
<servlet-name>dwr-invoker</servlet-name>
<servlet-class>
org.directwebremoting.spring.DwrSpringServlet
</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>3</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dwr-invoker</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
2 Bean 配置文件写法
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns ="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.directwebremoting.org/schema/spring-dwr
http://www.directwebremoting.org/schema/spring-dwr-2.0.xsd">
<!--<dwr:controllerid="dwrController"debug="true"/>-->
<dwr:configuration>
<dwr:convert
class="ww.support.web.dwr.validate.FieldValidateInfo" type="bean">
</dwr:convert>
</dwr:configuration>
<bean id="genericValidatorDwr"
class="ww.support.web.dwr.validate.GenericValidator">
<dwr:remote javascript="genericValidatorDwr">
<dwr:include method="isEmpty" />
<dwr:include method="isNotNumeric" />
<dwr:include method="isNumberNotInRange" />
<dwr:include method="isNumberGreaterThan" />
</dwr:remote>
</bean>
</beans>
3 jsp的调用
<script type='text/javascript'
src='<%=request.getContextPath()%>/dwr/engine.js'>
</script>
<script type='text/javascript'
src='<%=request.getContextPath()%>/dwr/util.js'></script>
<script type='text/javascript'src='<%=request.getContextPath()%>/dwr/interface/genericValidatorDwr.js'>
</script>
<script type='text/javascript'>
genericValidatorDwr.isNotNumeric(name,value,message, {
callback:function(dataFromServer) {
yourFunction(dataFromServer);
}
});
function yourFunction(data){
alert(data.message);
}
</script>