DWR: 远程调用java代码 dwr是ajax调用应用的一种技术 使用步骤: 1.将dwr包拷到lib包中 2.配置web.xml <listener> <listener-class>org.directwebremoting.servlet.dwrlistener</listener-class> </listener> <!-- DWRServlet→来之与dwr.jar --> <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>crossDomainSessionSecurity</param-name> <param-value>false</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>dwr-invoker</servlet-name> <url-pattern>/dwr/*</url-pattern> </servlet-mapping> 3.配置dwr.xml,该文件放置到WEB-INF中 package com.dwr.demo; public class MyDwr { public String hello(String world) { System.out.println("hello" + world); return "hello" + world; } } dwr.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 3.0//EN" "http://getahead.org/dwr/dwr30.dtd"> <dwr> <allow> <!-- javascript的对象是Demo,create="new"是DWR自己创建类 --> <create creator="new" javascript="MyDwr"> <!-- value是java中的类名 :将一个java类共享为一个javascript的对象--> <param name="class" value="com.dwr.demo.MyDwr" /> </create> </allow> </dwr> 4.dwr.jsp <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> <!-- 必要 导入js --> <script src="<%=request.getContextPath()%>/dwr/engine.js"></script> <!-- 如果共享的类是自己所定义的,需要使用/dwr/interface/xxx.js,这里的js名为dwr中javascript的名称 --> <script src="<%=request.getContextPath()%>/dwr/interface/MyDwr.js"></script> </head> <script type="text/javascript"> function showMessage(){ //可以调用java对象的方法 MyDwr.hello("world",function(data){ alert(data) }) } </script> </head> <body onload="showMessage()"> </body> </html>
dwr01工程:
访问dwr:效果
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-11-10 19:07:34