文章內容摘抄至http://www.cnblogs.com/linjiqin/archive/2011/03/28/1998125.html
?
?
?
<?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"> <!-- Spring上下文 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:resource/app*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 配置DWR前端控制器 --> <servlet> <servlet-name>dwrServlet</servlet-name> <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class> <!-- 指定配置文件 --> <init-param> <param-name>config</param-name> <!-- 如果有多個用","分開 --> <param-value> /WEB-INF/classes/config/dwr.xml </param-value> </init-param> <init-param> <param-name>debug</param-name> <param-value>true</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>dwrServlet</servlet-name> <url-pattern>/dwr/*</url-pattern> </servlet-mapping> </web-app>
?
?
?
?
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN" "http://getahead.org/dwr/dwr20.dtd"> <!-- 通用dwr配置 --> <dwr> <allow> <!-- 建立JS對象,將目標對象的方法轉換成JS對象的方法 --> <create javascript="helloSrv" creator="new"> <param name="class" value="services.HelloServices"></param> </create> <!-- 從Spring中獲取Java對象 --> <create javascript="deptSrv" creator="spring"> <param name="beanName" value="deptServices"></param> <!-- 禁止執行 --> <exclude method="deleteDept" /> </create> <create javascript="loginSrv" creator="spring"> <param name="beanName" value="loginSrv"></param> </create> <!-- 指定針對于特定對象的轉換器 --> <convert match="entity.*" converter="bean"></convert> <convert match="java.lang.Throwable" converter="bean"> <param name="include" value="message"></param> </convert> </allow> </dwr>
?
?
creator="spring"說明創建對象的方式是交由“spring”來管理的
<param name="beanName" value="deptServices"></param>
表示要由spring創建的類的名字根據“deptServices”唯一標識去spring的配置文件中查找,形成對應的關系
<convert match="entity.*" converter="bean"></convert>
其中convert="bean"是說明傳遞的對象和返回的對象的轉換是使用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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd" > <bean id="deptServices" class="services.DeptServices"></bean> <bean id="loginSrv" class="services.LoginService"></bean> </beans>
?
?
?
package entity; public class Dept { private Long id; private String name; public Dept() { } public Dept(Long id, String name) { this.id = id; this.name = name; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
?
?
?
package services; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import entity.Dept; @SuppressWarnings("unchecked") public class DeptServices { public List findDept() { throw new RuntimeException("查找失敗!"); } public void deleteDept(Long id) { System.out.println("Delete dept " + id); } public List getDeptsForPo() { List depts = new ArrayList(); depts.add(new Dept(1l, "教質部")); depts.add(new Dept(2l, "學術部")); depts.add(new Dept(3l, "就業部")); depts.add(new Dept(4l, "咨詢部")); return depts; } public void saveDept(List<Dept> depts) { // System.out.println(dept.getId() + ":" + dept.getName()); System.out.println(depts); } public List getDepts() { List depts = new ArrayList(); Map map = new HashMap(); map.put("id", "01"); map.put("name", "教質部"); depts.add(map); map = new HashMap(); map.put("id", "02"); map.put("name", "學術部"); depts.add(map); map = new HashMap(); map.put("id", "03"); map.put("name", "就業部"); depts.add(map); map = new HashMap(); map.put("id", "04"); map.put("name", "咨詢部"); depts.add(map); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } return depts; } public void test(){ System.out.println("tests"); } public void testPar(String s){ System.out.println("testPar " + s); } public void testObj(Dept d){ System.out.println(d.getId()); System.out.println(d.getName()); } }
?
?
?
package services; /** * @author dlwu * */ public class HelloServices { public String sayHello(String name){ System.out.println("Hello now!"); return "Hello " + name + "!"; } }
?
?
?
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'hello.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> </head> <!-- 記得引入js,測試地址: http://localhost:8083/dwrweb/dwr/ --> <script type="text/javascript" src="dwr/engine.js"></script> <script type="text/javascript" src="dwr/interface/deptSrv.js"></script> <script type="text/javascript" src="dwr/util.js"></script> <script type="text/javascript"> function test(){ //聲明dept對象 deptSrv.test(); } function test1(){ //傳遞普通的字符 var temp = "test parameter String"; deptSrv.testPar(temp); } function test2(){ //傳遞對象 var obj = {id:"1234",name:"huangbiao"}; deptSrv.testObj(obj); } function test3(){ //得到對象 deptSrv.getDepts(function(data){ for(var i = 0; i < data.length; i++ ){ alert(data[i].id); } }); } </script> <body> <input value="spring測試" type="button" onclick="test();"/> <input value="傳入一個字符串" type="button" onclick="test1();"/> <input value="傳入一個對象" type="button" onclick="test2();"/> <input value="接受一個對象" type="button" onclick="test3();"/> </body> </html>
?
?
?
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'hello.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> </head> <!-- 記得引入js,測試地址: http://localhost:8083/dwrweb/dwr/ --> <script type="text/javascript" src="dwr/engine.js"></script> <script type="text/javascript" src="dwr/interface/helloSrv.js"></script> <script type="text/javascript" src="dwr/util.js"></script> <script type="text/javascript"> function hello(){ //方法一 //返回處理后的結果信息 /*var fn = function(result){ $("msg").innerHTML = result; } helloSrv.sayHello($("name").value, fn);*/ //方法二 helloSrv.sayHello($("name").value, function(result){ $("msg").innerHTML=result; }); //方法三 //使用如下的好處為:不用導入如上三個js //第一個參數: dwr訪問路徑,在web.xml中配置,如: <url-pattern>/dwr/*</url-pattern> //第二個參數: dwr與java服務器通信變量,在dwr.xml中聲明 //第三個參數: 服務器方法名 //第四個參數: 頁面請求參數,即服務器方法名得參數 //第五個參數: 回調函數 //dwr.engine._execute("dwr", 'helloSrv', 'sayHello', $("name").value, fn); } </script> <body> <div id="msg"></div> <input type="text" id="name" /> <input type="button" value="Hello" onclick="hello();" /> </body> </html>
?
?將請求設置為同步的方法 —— DWREngine.setAsync(true);//異步,false為同步
?
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
