直接上代码,请下载源代码[在最后],有问题发我邮件lyk_52199@163.com
<==jsp/javascript================================>
-->main.jsp
-->upload.jsp
-->dodnload.jsp
<==java类================================>
TestBean.java
UploadFile.java
DwrDemo.java
DownFile.java
----------------------------------------------------by justin Lee @2011-03-13 PM
<==jsp/javascript================================>
-->main.jsp
<%@ 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> <title>DWR3.0DEMO--BY JUSTIN LEE @ 2011-03-13 PM</title> <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/Demo.js'></script> <script type='text/javascript'> //==================================================================================================== //无参,有简单返回值,只传一个回调参数过去 function test1(){ Demo.test1(callbackTest1); } /** *test1()回调函数 **/ function callbackTest1(data){ alert(data); } //==================================================================================================== /*****************************************************************************************************/ //==================================================================================================== //有简单参数,无返回值 function test2(){ Demo.test2("简单参数"); //Demo.test2("简单参数",callbackTest2);//可以不用传回调函数,如果传了会得到NULL } /** *test2()回调函数 **/ function callbackTest2(data){ alert(data); } //==================================================================================================== /*****************************************************************************************************/ //==================================================================================================== //有简单参数,有简单返回值 function test3(){ Demo.test3("有简单参数,有简单返回值",callbackTest3); } /** *test3()回调函数 **/ function callbackTest3(data){ alert(data); } //==================================================================================================== /*****************************************************************************************************/ //==================================================================================================== //有简单参数,返回Bean类 function test4(){ Demo.test4(new Date(),callbackTest4);//这里参数是一个日期,就直接用JS的日期就可以了 } /** *test4()回调函数 **/ function callbackTest4(data){ //对于返回实体类的JS处理方法 if(data){ for(var property in data){ alert("property:"+property); alert(property+":"+data[property]); } } //如果知道属性,则可以用下面的方法来获取值 alert(data["propA"]); alert(data.propD[0]);//这种方式处理也可以,不过这里propD是一个数组,因此可以用数组的处理方法 } //==================================================================================================== /*****************************************************************************************************/ //==================================================================================================== //有Bean参数,有简单返回值 function test5(){ var pa = "a String";//private String propA; var pb = 23;//private Integer propB; var pc = new Date();//private Date propC; var pd = ["a","b","c"];//private String[] propD; var obja = {propA:pa, propB:pb,propC:pc, propD:pd};//JSON方式构造一个类对象 Demo.test5(obja,callbackTest5); } /** *test5()回调函数 **/ function callbackTest5(data){ alert(data); } //==================================================================================================== /*****************************************************************************************************/ //==================================================================================================== //有简单参数,返回LIST MAP OR SET集合类 function test6(){ Demo.test6("有简单参数,返回LIST MAP OR SET集合类",callbackTest6); } /** *test6()回调函数 **/ function callbackTest6(data){ //不知道属性名称时,使用如下方法,最好少一点alert,很烦的 for(var i=0;i<data.length;i++){ for(var property in data[i]){ //alert("property:"+property); alert("property:"+property+"--value:"+data[i][property]); } } //知道属性名称时,使用如下方法 for(var i=0;i<data.length;i++){ alert(data[i].propA); alert(data[i].propB); alert(data[i].propD[0]); } } //==================================================================================================== /*****************************************************************************************************/ //==================================================================================================== //有List、Set或者Map等集合参数,返回LIST MAP OR SET集合类 function test7(){ var pa = "1a String for test7";//private String propA; var pb = 11;//private Integer propB; var pc = new Date();//private Date propC; var pd = ["aforTest7","bforTest7","cforTest7"];//private String[] propD; var obja = {propA:pa, propB:pb,propC:pc, propD:pd}; pa = "2a String for test7";//private String propA; pb = 22;//private Integer propB; pc = new Date();//private Date propC; pd = ["2aforTest7","2bforTest7","2cforTest7"];//private String[] propD; var objb = {propA:pa, propB:pb,propC:pc, propD:pd}; var list = [obja,objb]; Demo.test7(list,callbackTest7); } /** *test7()回调函数 **/ function callbackTest7(data){ //不知道属性名称时,使用如下方法 for(var i=0;i<data.length;i++){ for(var property in data[i]){ alert("--data["+i+"]--property:"+property+"----value:"+data[i][property]); } } } //==================================================================================================== </script> </head> <body onload="test7()"> </body> </html>
-->upload.jsp
<%@ 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> <title>DWR3.0DEMO--BY JUSTIN LEE @ 2011-03-13 PM - file upload</title> <script type='text/javascript' src='<%=request.getContextPath()%>/dwr/interface/UploadFile.js'></script> <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"> /* *文件上传Dwr */ function uploadFiles() { if(document.getElementById("uploadFile").value==""){ alert("请先选择要上传的文件"); document.getElementById("uploadFile").click(); return; } var file = dwr.util.getValue('uploadFile'); UploadFile.upload(file,function(data){ alert(data); document.getElementById("uploadFile").value=""; //for(var property in data){ //alert("property:"+property); //alert(property+":"+data[property]); //} }); } </script> </head> <body> <table><tr> <td>File</td> <td><input type="file" id="uploadFile" /></td> <td id="file.container"> </td> </tr> <tr> <td colspan="3"> <button onclick="uploadFiles()">upload</button> </td> </tr> </table> </body> </html>
-->dodnload.jsp
<%@ 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> <title>DWR3.0DEMO--BY JUSTIN LEE @ 2011-03-13 PM</title> <script type='text/javascript' src='<%=request.getContextPath()%>/dwr/interface/DownloadFile.js'></script> <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"> /* *文件下载Dwr */ function downloadPdfFile() { var pdftext = dwr.util.getValue('pdftext'); DownloadFile.downloadPdfFile(pdftext, function(data) { dwr.engine.openInDownload(data); }); } </script> </head> <body> <input type="text" id="pdftext" value="Hello, justin Lee,这是一个PDF文档内容" /> <input type="button" onclick="downloadPdfFile()" value="DownLoadIt" /> </body> </html>
<==java类================================>
TestBean.java
/** * 测试Bean */ package com.justin.dwrdemo; import java.util.Date; /** * @author Administrator * */ public class TestBean { private String propA; private Integer propB; private Date propC; private String[] propD; public String getPropA() { return propA; } public void setPropA(String propA) { this.propA = propA; } public Integer getPropB() { return propB; } public void setPropB(Integer propB) { this.propB = propB; } public Date getPropC() { return propC; } public void setPropC(Date propC) { this.propC = propC; } public String[] getPropD() { return propD; } public void setPropD(String[] propD) { this.propD = propD; } }
UploadFile.java
/** * */ package com.justin.dwrdemo; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import org.directwebremoting.WebContext; import org.directwebremoting.WebContextFactory; import org.directwebremoting.io.FileTransfer; /** * @author justin * */ public class UploadFile { /** * 文件上传 * * @return */ public String upload(FileTransfer fileTransfer) { if(fileTransfer!=null){ System.out.println("---fileTransfer.getFilename()\t"+fileTransfer.getFilename()); System.out.println("---fileTransfer.getSize()\t"+fileTransfer.getSize()); System.out.println("---fileTransfer.getMimeType()\t"+fileTransfer.getMimeType()); } String s = "{文件上传失败}"; try { WebContext webContext = WebContextFactory.get(); webContext.getHttpServletResponse().setCharacterEncoding("UTF-8"); String saveurl = webContext.getHttpServletRequest().getSession().getServletContext().getRealPath("/upload"); System.out.println("---saveurl\t"+saveurl); String fn = fileTransfer.getFilename(); fn = new String(fn.getBytes("ISO-8859-1"), "UTF-8"); File saveDir = new File("F://flexJavaDev/tomcat6_20110114/webapps/dwrDemo/uploadFiles"); if(!saveDir.exists()){ saveDir.mkdir(); } File file = new File(saveDir.getAbsolutePath()+"/"+fn); System.out.println("fileAbsPath:"+file.getAbsolutePath()); file.setReadable(true); file.setWritable(true); if (!file.exists()) { file.createNewFile(); System.out.println("new File:"+file.getAbsolutePath()); } InputStream inputStream = fileTransfer.getInputStream(); int available = inputStream.available(); byte[] b = new byte[available]; FileOutputStream foutput = new FileOutputStream(file); inputStream.read(b); foutput.write(b); foutput.flush(); foutput.close(); inputStream.close(); s="{文件名:'"+fn+",文件类型:'"+fileTransfer.getMimeType()+"',文件大小:'"+fileTransfer.getSize()+"',文件存储地址:'"+file.getAbsolutePath()+"'}"; System.out.println("===s===\n"+s); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch(Exception e){ e.printStackTrace(); } return s; } }
DwrDemo.java
package com.justin.dwrdemo; import java.util.*; /** * @author justin * 功能:dwr3功能性测试类 * 日期:2011-03-13 PM */ public class DwrDemo { /** * 无参,有简单返回值 * @return */ public String test1(){ System.out.println("test1:无参,有简单返回值\t"); return "test1:无参,有简单返回值"; } /** * 有简单参数,无返回值 * @param s */ public void test2(String s){ System.out.println("test2:有简单参数,无返回值\t"+s); } /** * 有简单参数,有简单返回值 * @param s * @return */ public String test3(String s){ System.out.println("test3:有简单参数,有简单返回值\t"+s); return "test3:"+s; } /** * 有简单参数,返回Bean类 * @param date * @return */ public TestBean test4(Date date){ System.out.println("test4:有简单参数,返回一般实体类\t"+date); TestBean tb = new TestBean(); tb.setPropA("string prop a"); tb.setPropB(2); Calendar c = Calendar.getInstance(); c.add(Calendar.YEAR, 200); tb.setPropC(c.getTime()); String[] a = new String[2]; a[0]="arrValue_0"; a[1]="数组值_1"; tb.setPropD(a); return tb; } /** * 有Bean参数,有简单返回值 * @param tb * @return */ public String test5(TestBean tb){ System.out.println("test5:"+tb); System.out.println(tb.getPropA()); System.out.println(tb.getPropB()); System.out.println(tb.getPropC()); System.out.println(tb.getPropD()[0]); return "OK"; } /** * 有简单参数,返回LIST MAP OR SET集合类 * @param a * @return */ public List<TestBean> test6(String a){ System.out.println("test6:有简单参数,返回LIST MAP OR SET集合类\t"+a); TestBean tb = null; List<TestBean> list = new ArrayList<TestBean>(); for(int i=0;i<10;i++){ tb = new TestBean(); tb.setPropA("propA__"+i); tb.setPropB(i); tb.setPropC(new Date()); String[] arr = new String[2]; arr[0]="arr0__"+i; arr[1]="arr1__"+i; tb.setPropD(arr); list.add(tb); } return list; } /** * 有List、Set或者Map等集合参数,返回LIST MAP OR SET集合类 * @param a * @return */ public List<TestBean> test7(List<TestBean> list){ System.out.println("test7:有List、Set或者Map等集合参数,返回LIST MAP OR SET集合类\t"+list); TestBean tb = null; List<TestBean> listRs = new ArrayList<TestBean>(); if(list!=null){ for(int i=0;i<list.size();i++){ tb = list.get(i); tb.setPropA(tb.getPropA()+"forReturn!"); tb.setPropB(tb.getPropB()+1000); tb.setPropC(new Date()); String[] arr = tb.getPropD(); for(int j=0;j<arr.length;j++){ arr[j] = arr[j]+"updatedByTest7()"; } tb.setPropD(arr); listRs.add(tb); } } return list; } }
DownFile.java
/** * */ package com.justin.dwrdemo; import java.io.ByteArrayOutputStream; import org.directwebremoting.io.FileTransfer; import com.lowagie.text.Document; import com.lowagie.text.Paragraph; import com.lowagie.text.pdf.PdfWriter; /** * @author justin * */ public class DownloadFile { public FileTransfer downloadPdfFile(String contents) throws Exception { if (contents == null || contents.length() == 0) { contents = "[BLANK]"; } ByteArrayOutputStream buffer = new ByteArrayOutputStream(); Document document = new Document(); PdfWriter.getInstance(document, buffer); document.addCreator("created by justin Lee with iText"); document.open(); document.add(new Paragraph(contents)); document.close(); return new FileTransfer("justinLeePdfDemo.pdf", "application/pdf", buffer.toByteArray()); } }
----------------------------------------------------by justin Lee @2011-03-13 PM
- dwrDemo.zip (17 KB)
- 下载次数: 22
- dwrDemoJars.rar (4.4 MB)
- 下载次数: 20
相关推荐
DWR.xml配置文件说明书.doc<br>DWR笔记.doc<br>DWR技术分析.doc<br>DWR开发培训.ppt<br>DWR学习.doc<br>WEB界面开发规范.doc<br>树控件.rar
<artifactId>dwr</artifactId> <version>3.0.4</version> </dependency> ``` 如果手动添加jar包,则需要下载DWR的相关jar包并将其放置在项目的`WEB-INF/lib`目录下。 ##### 2. 创建并配置`dwr.xml` 接着,在`...
### dwr学习笔记和总结 #### 一、DWR简介 DWR (Direct Web Remoting) 是一个简化Ajax开发的框架,它使得JavaScript能够直接调用服务器端的Java方法成为可能,无需编写复杂的Ajax代码。这极大地提高了开发效率,并...
【笔记jsp/js/ajax/dwr/servlet/html】笔记主要涵盖了初学者在学习Web开发时需要掌握的基础知识,包括HTML、SERVLET、JSP、JS、AJAX和DWR等技术。以下是对这些知识点的详细说明: 1. **HTML**: - HTML(HyperText...
阅读"DWR学习笔记.doc"文件将有助于系统地掌握这些知识点,通过实践操作加深理解,从而更好地利用DWR构建交互性强、用户体验良好的Web应用。在学习过程中,结合源码分析可以更深入地理解DWR的工作机制,提升自己的...
技术分享:DWR实战学习笔记
`<create>`标签创建一个JavaScript对象,`javascript`属性定义了JavaScript中引用Java对象的名称,`creator`属性指定了实例化Java对象的方式,如`new`表示使用默认构造函数。`<param>`标签用于指定Java类的全限定名...
dwr3框架学习笔记–第一个dwr3程序sayhello 博文:http://blog.csdn.net/yuchen837295036/article/details/52682417
**DWR框架详解** DWR (Direct Web Remoting) 是一个强大的AJAX框架,它...尝试创建一个简单的DWR应用,实现用户登录功能,使用DWR检查用户名是否已存在,同时学习如何在DWR中集成其他Java框架,如Hibernate或MyBatis。
4. **暴露Java对象**:在`dwr.xml`中,使用`<allow>`标签来指定可从JavaScript访问的Java对象和方法。例如: ```xml <allow> <create creator="new" javascript="MyService"> <param name="class" value=...
首先,"DWR学习笔记"提供了对DWR基础概念、核心功能以及实际应用的概述。这些笔记可能包含了DWR的基本架构,如它如何通过AJAX技术实现实时的Web交互,以及如何创建和调用服务器端的Java方法。 "DWR中文API"是DWR库...
DWR学习资料 :DWR 3.0 上传文件.txt DWR3.0反向Ajax示例.txt DWR3.0学习笔记.txt DWR3.0学习网址.txt dwr分页.doc DWR分页代码.doc DWR中文文档.doc DWR中文文档.pdf dwr做comet的完整实现.doc Spring整合DWR comet ...
DWR使用笔记.chm:这可能是一份详细的DWR使用手册,包含了如何集成DWR到项目中、配置DWR引擎、创建远程接口以及处理各种交互的实例。通过阅读这份笔记,你可以了解到DWR的基本概念、配置步骤以及实际应用技巧。 dwr...