`

DWR 之 org.directwebremoting.create包

阅读更多

dwr.xml中有,<creator>标签负责公开用于Web远程的类和类的方法,实际上最后调用的就是本包下面的script创建类。
creator
类型在1.1版本的时候有如下几种,现在是3.0版本了,我查了下源码,种类好像比下面要多(查XXXCreator有多少个)。
    new:
Javanew关键字创造对象。 
    none:
它不创建对象,看下面的原因。
(v1.1+) 
    scripted:
通过BSF使用脚本语言创建对象,例如BeanShellGroovy
 
    spring:
通过Spring框架访问Bean
 
    jsf:
使用JSFBean
(v1.1+) 
    struts:
使用StrutsFormBean
(v1.1+) 
    pageflow:
访问BeehiveWeblogicPageFlow
(v1.1+) 
   
    creator
是在什么时候调用的呢?

     
实际上,在Serlvet加载的时候有个doPost方法,doPost方法调用handlehandle再调用remotorremotor最终调用相应的creator,比如NewCreatorcreator实际上是执行创建script字符串的工作

 

举例来说,我们想在html中某个地方直接显示当前时间的long值,那么我们就可以调用java.util.Date类的getTime()方法。dwr.xml中写法如下:

<dwr> 

   <allow>

      <create creator="new" javascript="JDate">

            <param name="class" value="java.util.Date"/>

      </create>

        <create creator="new" javascript="Demo">

            <param name="class" value="your.java.Bean"/>

        </create>

</allow> 
</dwr>

 

 

我们知道这样所有的/dwr/*所有请求都由这个servlet来处理,那么实际上,浏览器加载
<script type='text/javascript' src='dwr/interface/JDate.js'></script>
时,实际上是在触发servlet,这次触发属于系统触发,不做事的,只有执行javascript调用方法时如:

 

function getServerDateTime() {

   JDate.getTime(handleGetTime);
}
function handleGetTime(dateTime) {

    DWRUtil.setValue("date", dateTime);
}

 

才会触发下面

 

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException

  {

        doPost(req, resp);    

}

 

 

doGet会调用到doPost

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException     {

        try

         {

            webContextBuilder.set(request, response, getServletConfig(), getServletContext(), container);                 UrlProcessor processor = container.getBean(UrlProcessor.class);

            processor.handle(request, response);

        }        finally

         {            webContextBuilder.unset();        }

    }

 

 

proccessor中我们看到如下代码

 

public void handle(HttpServletRequest request, HttpServletResponse response) throws IOException

    {

        try

        {

            String pathInfo = request.getPathInfo();

            contextPath = request.getContextPath();

 

            if (pathInfo == null || pathInfo.length() == 0 || "/".equals(pathInfo))

            {

                response.sendRedirect(contextPath + request.getServletPath() + indexHandlerUrl);

            }

            else

            {

                // Loop through all the known URLs

                for (Entry<String, Object> entry : urlMapping.entrySet())

                {

                    String url = entry.getKey();

 

                    // If this URL matches, call the handler

                    if (pathInfo.startsWith(url))

                    {

                        Handler handler = (Handler) entry.getValue();

                        handler.handle(request, response);

                        return;

                    }

                }

 

                notFoundHandler.handle(request, response);

            }

        }

        catch (Exception ex)

        {

            exceptionHandler.setException(ex);

            exceptionHandler.handle(request, response);

        }

}

 

这些handle有多种
1

/**
 * A Handler that supports requests for auth.js
  */
public class AuthHandler extends JavaScriptHandler
2

/**
 * A Handler that supports requests for engine.js
 * @author Joe Walker [joe at getahead dot ltd dot uk]
 */
public class EngineHandler extends JavaScriptHandler
3

/**
 * A Handler that supports requests for util.js
 * @author Joe Walker [joe at getahead dot ltd dot uk]
 */
public class GiHandler extends JavaScriptHandler
4

/**
 * A handler for interface generation requests
 * @author Joe Walker [joe at getahead dot ltd dot uk]
 */
public class InterfaceHandler extends JavaScriptHandler
上面三种都是系统范围的handle,对于我们自己编写的类,应该是触发InterfaceHandler

我们再看如下关系
public abstract class CachingFileHandler implements Handler
public abstract class TemplateHandler extends CachingFileHandler
public abstract class JavaScriptHandler extends TemplateHandler
public class InterfaceHandler extends JavaScriptHandler
逐级往上继承

InterfaceHandler
,我们看到如下代码执行顺序-4,里面调用了远程remoter

 

protected String generateTemplate(HttpServletRequest request, HttpServletResponse response) throws IOException

    {

        String scriptName = request.getPathInfo();

        scriptName = scriptName.replace(interfaceHandlerUrl, "");

 

        String contextServletPath = request.getContextPath() + request.getServletPath();

 

        if (scriptName.endsWith(PathConstants.EXTENSION_JS))

        {

            scriptName = scriptName.replace(PathConstants.EXTENSION_JS, "");

            if (!LocalUtil.isJavaIdentifier(scriptName))

            {

                log.debug("Throwing at request for script with name: '" + scriptName + "'");

                throw new SecurityException("Script names may only contain Java Identifiers");

            }

 

            return remoter.generateInterfaceScript(scriptName, contextServletPath);

        }

        else if (scriptName.endsWith(PathConstants.EXTENSION_SDOC))

        {

            scriptName = scriptName.replace(PathConstants.EXTENSION_SDOC, "");

            if (!LocalUtil.isJavaIdentifier(scriptName))

            {

                log.debug("Throwing at request for script with name: '" + scriptName + "'");

                throw new SecurityException("Script names may only contain Java Identifiers");

            }

 

            return remoter.generateInterfaceSDoc(scriptName, contextServletPath);

        }

        else

        {

            log.debug("Throwing at request for script with unknown extension: '" + scriptName + "'");

            throw new SecurityException("Unknown extension");

        }

    }

 

 

JavaScriptHandler代码如下,调用了父类TemplateHandlergenerateCachableContent方法执行顺序-2

 

 

protected String generateCachableContent(HttpServletRequest request, HttpServletResponse response) throws IOException

    {

        String output = super.generateCachableContent(request, response);

 

        if (debug || compressor == null)

        {

            return output;

        }

 

        try

        {

            return compressor.compressJavaScript(output);

        }

        catch (Exception ex)

        {

            log.warn("Compression system (" + compressor.getClass().getSimpleName() +") failed to compress script", ex);

            return output;

        }

    }

 

 

 

 

TemplateHandler类代码如下执行顺序-3,里面调用了generateTemplate方法

 

protected String generateCachableContent(HttpServletRequest request, HttpServletResponse response) throws IOException

    {

        String template = generateTemplate(request, response);

 

        Map<String, String> replace = getSearchReplacePairs();

分享到:
评论

相关推荐

    AJAX框架DWR简单应用.pdf

    可以在[DWR官方网站](https://getahead.org/dwr/)找到最新的版本并下载。 2. **下载Log4j支持**: 为了记录日志,需要额外下载Log4j的支持库。这可以通过访问[Apache Log4j官网]...

    Dwr配置详解.doc

    &lt;servlet-class&gt;org.directwebremoting.servlet.DwrServlet &lt;param-name&gt;debug &lt;param-value&gt;true ``` 同时,需要配置Servlet的URL映射。 ```xml &lt;servlet-name&gt;dwr-invoker &lt;url-pattern&gt;/dwr/* ``` ...

    dwr消息推送功能

    DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD DirectWebRemoting 2.0//EN" "http://getahead.org/dwr//dwr20.dtd"&gt; &lt;dwr&gt; &lt;create javascript="myrevsrse" creator="new"&gt; &lt;param name="class" value="com....

    dwr.xml配置描述及JSP上的引用_1

    &lt;%@ page import="org.directwebremoting.HttpServletResponse"%&gt; &lt;%@ page import="org.directwebremoting.ScriptTagHelper"%&gt; &lt;script type="text/javascript" src="/dwr/interface/MyService.js"&gt; ...

    dwr spring服务器主动推送示例代码

    &lt;bean id="dwrConfig" class="org.directwebremoting.config.DwrConfiguration"&gt; &lt;!-- 配置其他属性 --&gt; &lt;filter-name&gt;DWR Filter &lt;filter-class&gt;org.directwebremoting.servlet.DwrFilter &lt;param-name&gt;...

    DWR实现Ajax的配置过程

    如果手动添加jar包,则需要下载DWR的相关jar包并将其放置在项目的`WEB-INF/lib`目录下。 ##### 2. 创建并配置`dwr.xml` 接着,在`WEB-INF`目录下创建一个名为`dwr.xml`的文件,用于指定哪些Java类可以被远程调用,...

    将dwr集成到spring mvc(dwr的配置是基于xml)

    &lt;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 ...

    DWR入门教程.pdf

    DOCTYPE dwr PUBLIC "-//GetAheadLimited//DTD DirectWebRemoting 1.0//EN" "***"&gt; &lt;dwr&gt; &lt;create creator="new" javascript="JDate"&gt; &lt;param name="class" value="java.util.Date"/&gt; &lt;/create&gt; &lt;/dwr&gt; ``` ...

    DWR联动刷select

    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"&gt; &lt;servlet-name&gt;DWR &lt;servlet-class&gt;org....

    springboot整合dwr实现js调用java方法

    &lt;groupId&gt;org.directwebremoting &lt;artifactId&gt;dwr 版本号 ``` 2. **配置DWR**:在SpringBoot的主配置类(通常继承`SpringBootServletInitializer`)中,配置DWR的Servlet。这一步会告诉SpringBoot如何启动和...

    DWR-Getting Started.pdf

    - 访问DWR官方网站(http://getahead.org/dwr/getstarted)下载`dwr.jar`。 - 确保下载的是适合当前项目版本的DWR JAR文件。 2. **放置JAR文件**: - 将下载好的`dwr.jar`文件放入项目的`WEB-INF/lib`目录下。 ...

    dwr.xml

    - `&lt;servlet&gt;` 定义了名为 `dwr-invoker` 的Servlet,其类路径为 `org.directwebremoting.servlet.DwrServlet`。 - `&lt;init-param&gt;` 设置了调试模式为开启状态 (`true`),便于开发过程中查看日志。 - `...

    ajax dwr

    而DWR则是实现Ajax功能的一种方式之一,它通过简化远程调用的过程来提高开发效率。 ### DWR的特性 #### 1. 直接调用Java方法 DWR的核心功能是允许JavaScript代码直接调用服务器端的Java方法。这种机制极大地简化了...

    DWR使用方法-step by step

    "http://getahead.org/dwr/dwr20.dtd"&gt; &lt;dwr&gt; &lt;create creator="new" javascript="JDate"&gt; &lt;param name="class" value="java.util.Date"/&gt; &lt;/create&gt; &lt;create creator="new" javascript="Demo"&gt; ...

    Struts1.x Spring2.x Hibernate3.x DWR2.x整合工具文档v1.00

    &lt;filter-class&gt;org.directwebremoting.servlet.DwrServlet &lt;param-name&gt;debug &lt;param-value&gt;true &lt;param-name&gt;servletPath &lt;param-value&gt;/dwr/ &lt;filter-name&gt;dwr-invoker &lt;url-pattern&gt;/dwr/* ```...

    ajax--dwr测试

    &lt;listener-class&gt;org.directwebremoting.servlet.EfficientShutdownServletContextAttributeListener &lt;listener-class&gt;org.directwebremoting.servlet.EfficientShutdownServletContextListener ...

    dwr相关文件实用指南,请仔细查看

    DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD DirectWebRemoting 2.0//EN" "http://getahead.org/dwr/dwr20.dtd"&gt; &lt;dwr&gt; &lt;create creator="new" javascript="JDate"&gt; &lt;param name="class" value="java.util....

    DWR配置及实例

    &lt;groupId&gt;org.directwebremoting &lt;artifactId&gt;dwr 版本号 ``` 2. 配置Servlet:在web.xml文件中配置DWRServlet,它负责处理来自客户端的Ajax请求。例如: ```xml &lt;servlet-name&gt;DWRServer &lt;servlet-class&gt;...

    dwr3.0的HELLOWORLD创建过程

    &lt;servlet-class&gt;org.directwebremoting.servlet.DwrServlet &lt;servlet-name&gt;dwr-invoker-servlet &lt;url-pattern&gt;/dwr/* ``` #### 二、编写JavaBean - **步骤一**:编写JavaBean类。确保包含一个公共无参构造...

Global site tag (gtag.js) - Google Analytics