Blog信息 |
blog名称: 日志总数:1304 评论数量:2242 留言数量:5 访问次数:7584520 建立时间:2006年5月29日 |

| |
[DWR(Ajax)]DWR源码学习(一) 软件技术
lhwork 发表于 2006/7/17 14:56:04 |
DWR一个外国人实现的很有前途的AJAX框架。多余的话就不说了,请看DWR的例子程序:web.xml 1500)this.width=500'><?xml version="1.0" encoding="ISO-8859-1"?> 2500)this.width=500'><!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> 3500)this.width=500'> 4500)this.width=500'><web-app id="dwr"> 5500)this.width=500'> 6500)this.width=500'> <display-name>DWR (Direct Web Remoting)</display-name> 7500)this.width=500'> <description>A demo of how to call Java on the server directly from Javascript on the client</description> 8500)this.width=500'> 9500)this.width=500'> <servlet>10500)this.width=500'> <servlet-name>dwr-invoker</servlet-name>11500)this.width=500'> <display-name>DWR Servlet</display-name>12500)this.width=500'> <description>Direct Web Remoter Servlet</description>13500)this.width=500'> <servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>14500)this.width=500'> <!--15500)this.width=500'> <init-param>16500)this.width=500'> <param-name>config</param-name>17500)this.width=500'> <param-value>WEB-INF/dwr.xml</param-value>18500)this.width=500'> </init-param>19500)this.width=500'> -->20500)this.width=500'> <init-param>21500)this.width=500'> <param-name>debug</param-name>22500)this.width=500'> <param-value>true</param-value>23500)this.width=500'> </init-param>24500)this.width=500'> <init-param>25500)this.width=500'> <param-name>scriptCompressed</param-name>26500)this.width=500'> <param-value>false</param-value>27500)this.width=500'> </init-param>28500)this.width=500'> <load-on-startup>1</load-on-startup>29500)this.width=500'> </servlet>30500)this.width=500'>31500)this.width=500'> <servlet-mapping>32500)this.width=500'> <servlet-name>dwr-invoker</servlet-name>33500)this.width=500'> <url-pattern>/dwr/*</url-pattern>34500)this.width=500'> </servlet-mapping>35500)this.width=500'>36500)this.width=500'></web-app>servlet(uk.ltd.getahead.dwr.DWRServlet)里: 1 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException 2 { 3 try 4 { 5 builder.set(req, resp, getServletConfig(), getServletContext(), container); 6 ServletLoggingOutput.setExecutionContext(this); 7 8 processor.handle(req, resp);//该方法对所有request路径/dwr/*有效,在引用JS的时候,使用这个路径执行dwr生成的javascript代码 finally11 {12 builder.unset();13 ServletLoggingOutput.unsetExecutionContext();14 }15 }index.html 1 <?xml version="1.0" encoding="ISO-8859-1" ?> 2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> 3 <html xmlns="http://www.w3.org/1999/xhtml"> 4 <head> 5 <title>DWR - Test Home</title> 6 <script type='text/javascript' src='dwr/interface/Test.js'></script> 7 <script type='text/javascript' src='dwr/engine.js'></script> 8 <script type='text/javascript' src='dwr/util.js'></script> 9 <script>10 function init() {11 // This turns off the no-javascript message12 document.getElementById("start").style.display = "none";13 // This checks for file: URLs and loading problems14 if (window.DWREngine == null || window.DWRUtil == null) {15 document.getElementById("file").style.display = "block";16 return;17 }18 // DWR setup19 DWREngine.setErrorHandler(function(message) { alert(message); });20 DWREngine.setWarningHandler(function(message) { alert(message); });21 DWRUtil.useLoadingMessage();22 // Turn on the generic error div23 $("error").style.display = "block";24 // Load a message from the server25 Test.getInsert(load);26 }27 28 function load(data) {29 $("error").style.display = "none";30 DWRUtil.setValue("reply", data);31 $("reply").style.display = "block";32 }33 </script>34 </head>35 36 <body onload="init()">37 500)this.width=500'>500)this.width=500'>.这一部分经过了SERVLET处理:500)this.width=500'> <script type='text/javascript' src='dwr/interface/Test.js'></script>500)this.width=500'> <script type='text/javascript' src='dwr/engine.js'></script>500)this.width=500'> <script type='text/javascript' src='dwr/util.js'></script>dwrservlet.doPost方法内processor.handle(req, resp)这个方法如下: 1 public void handle(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException 2 { 3 String pathInfo = req.getPathInfo(); 4 String servletPath = req.getServletPath(); 5 6 if (nullPathInfoWorkaround && pathInfo == null) 7 { 8 pathInfo = req.getServletPath(); 9 servletPath = HtmlConstants.PATH_ROOT;10 log.debug("Default servlet suspected. pathInfo=" + pathInfo + "; contextPath=" + req.getContextPath() + "; servletPath=" + servletPath); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$11 }12 13 if (pathInfo == null ||14 pathInfo.length() == 0 ||15 pathInfo.equals(HtmlConstants.PATH_ROOT))16 {17 resp.sendRedirect(req.getContextPath() + servletPath + HtmlConstants.FILE_INDEX);18 }19 else if (pathInfo.startsWith(HtmlConstants.FILE_INDEX))20 {21 index.handle(req, resp);22 }23 else if (pathInfo.startsWith(HtmlConstants.PATH_TEST))24 {25 test.handle(req, resp);26 }27 else if (pathInfo.startsWith(HtmlConstants.PATH_INTERFACE))28 {29 iface.handle(req, resp);//这个方法是我们要关注的 } 。。。。。。。 } iface.handle(req, resp);//这个方法是我们要关注的,来自DefaultInterfaceProcessor 1 public void handle(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException 2 { 3 String pathinfo = req.getPathInfo(); 4 String servletpath = req.getServletPath(); 5 if (pathinfo == null) 6 { 7 pathinfo = req.getServletPath(); 8 servletpath = HtmlConstants.PATH_ROOT; 9 }10 String scriptname = pathinfo;11 scriptname = LocalUtil.replace(scriptname, HtmlConstants.PATH_INTERFACE, HtmlConstants.BLANK);12 scriptname = LocalUtil.replace(scriptname, HtmlConstants.EXTENSION_JS, HtmlConstants.BLANK);13 Creator creator = creatorManager.getCreator(scriptname);14 15 //resp.setContentType("text/javascript");16 PrintWriter out = resp.getWriter();17 out.println();18 19 out.println("function " + scriptname + "() { }"); //从这里开始DWR自动生成javascript String path = overridePath;22 if (path == null)23 {24 path = req.getContextPath() + servletpath;25 }26 out.println(scriptname + "._path = '" + path + "';"); //$NON-NLS-1$ //$NON-NLS-2$27 28 Method[] methods = creator.getType().getMethods();29 for (int i = 0; i < methods.length; i++)30 {31 Method method = methods[i];32 String methodName = method.getName();33 34 // We don't need to check accessControl.getReasonToNotExecute()35 // because the checks are made by the doExec method, but we do check36 // if we can display it37 String reason = accessControl.getReasonToNotDisplay(req, creator, scriptname, method);38 if (reason != null && !allowImpossibleTests)39 {40 continue;41 }42 43 // Is it on the list of banned names44 if (jsutil.isReservedWord(methodName))45 {46 continue;47 }48 49 out.print('\n');50 out.print(scriptname + '.' + methodName + " = function("); //$NON-NLS-1$51 Class[] paramTypes = method.getParameterTypes();52 for (int j = 0; j < paramTypes.length; j++)53 {54 if (!LocalUtil.isServletClass(paramTypes[j]))55 {56 out.print("p" + j + ", "); //$NON-NLS-1$ //$NON-NLS-2$57 }58 }59 out.println("callback) {"); //$NON-NLS-1$60 61 out.print(" DWREngine._execute(" + scriptname + "._path, '" + scriptname + "', '" + methodName + "\', "); //实现javascript调用java内的方法 for (int j = 0; j < paramTypes.length; j++)63 {64 if (LocalUtil.isServletClass(paramTypes[j]))65 {66 out.print("false, "); //$NON-NLS-1$67 }68 else69 {70 out.print("p" + j + ", "); //$NON-NLS-1$ //$NON-NLS-2$71 }72 }73 out.println("callback);"); //$NON-NLS-1$74 75 out.println('}');76 }77 78 out.flush();79 }DWR例子程序下载地址:http://www.blogjava.net/Files/javajohn/dwr.rar |
|
回复:DWR源码学习(一) 软件技术
ajie(游客)发表评论于2007/4/19 15:23:36 |
so good a girl!i like her very much! |
|
» 1 »
|