一个DWR与Spring的简单整合例子
1.首先需要一个DWR.jar包
2.在web.xml中配置:
<servlet>
<servlet-name>dwr-invoker</servlet-name>
<display-name>DWR Servlet</display-name>
<servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>dwr-invoker</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
这句话告诉容器,要启用dwr.
3.创建dwr.xml文件内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 1.0//EN" "http://www.getahead.ltd.uk/dwr/dwr10.dtd">
<dwr>
<allow>
<create creator="spring" javascript="groupSmsAction">
<param name="beanName" value="GroupSendActionBean"/>
<include method="sendSms"/>
<include method="smsGroupSendInfo"/>
</create>
</allow>
</dwr>
注意: creator="spring" 告诉容器,由spring来管理, GroupSendActionBean之前applicationContext.xml中定义的bean.
javascript="groupSmsAction" 页面要调用的对象.
4.页面引用JS:
<script src='$../dwr/interface/groupSmsAction.js' type='text/javascript'></script>
<script src='../dwr/engine.js' type='text/javascript'></script>
<script src='../dwr/util.js' type='text/javascript'></script>
之前一直在纳闷, engine.js 和 util.js这两个文件哪里的呢, 后来查看dwr.jar才发现这两个文件在dwr.jar文件中.
错误提示:
有时候在页面会遇到这样的问题: groupSmsAction 未定义, 有可能就是你忘记在web.xml中忘写了第二个步骤.