引入包dwr.jar
a)web.xml配置:
<servlet>
<servlet-name>dwr-invoke</servlet-name>
<servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>dwr-invoke</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
b)dwr.xml配置(文件放入WEB-INF目录下):
<allow>标签中包括可以暴露给javascript访问的东西
<create>标签中指定javascript中可以访问的java类,并定义DWR应当如何获得要进行远程的类的实例。creator="new"属性指定java类实例的生成方式,new意味着DWR应当调用类的默认构造函数来获得实例,其他的还有spring方式,通过与IOC容器Spring进行集成来获得实例等等
name这里一般用class指一个类,beanName指spring配置里的bean
<allow>
<create creator="spring" javascript="testDWR">
<param name="beanName" value="/testController.htm"/>
<!—Controller类里的方法 -->
<include method="getString" />
</create>
<convert converter="bean" match="com.isoftstone.cm.cr.dto.PointDataDto">
<param name="include"
value="pointNo,pointNme,subDeptCde,subDeptNme" />
</convert>
</allow>
c)Jsp页面:
引入以下三个文件,testDWR.js指在dwr.xml配置里的javascript:
<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/testDWR.js"></script>
<script type="text/javascript">
function testDwr(){
testDWR.getString('123',setValue);
}
function setValue(args){
alert(args);
}
</script>
设置dwr的dwr.engine.setAsync(false);将的通信方式改成同步的.(既会等待服务器.将数据接受完毕后再继续进行.)
dwr.engine.setAsync(true)设置为异步