在dwr.xml中有,<creator>标签负责公开用于Web远程的类和类的方法,实际上最后调用的就是本包下面的script创建类。
creator类型在1.1版本的时候有如下几种,现在是3.0版本了,我查了下源码,种类好像比下面要多(查XXXCreator有多少个)。
new: 用Java的new关键字创造对象。
none: 它不创建对象,看下面的原因。 (v1.1+)
scripted: 通过BSF使用脚本语言创建对象,例如BeanShell或Groovy。
spring: 通过Spring框架访问Bean。
jsf: 使用JSF的Bean。 (v1.1+)
struts: 使用Struts的FormBean。 (v1.1+)
pageflow: 访问Beehive或Weblogic的PageFlow。 (v1.1+)
creator是在什么时候调用的呢?
实际上,在Serlvet加载的时候有个doPost方法,doPost方法调用handle,handle再调用remotor,remotor最终调用相应的creator,比如NewCreator,creator实际上是执行创建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代码如下,调用了父类TemplateHandler的generateCachableContent方法执行顺序-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();
分享到:
相关推荐
可以在[DWR官方网站](https://getahead.org/dwr/)找到最新的版本并下载。 2. **下载Log4j支持**: 为了记录日志,需要额外下载Log4j的支持库。这可以通过访问[Apache Log4j官网]...
<servlet-class>org.directwebremoting.servlet.DwrServlet <param-name>debug <param-value>true ``` 同时,需要配置Servlet的URL映射。 ```xml <servlet-name>dwr-invoker <url-pattern>/dwr/* ``` ...
DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD DirectWebRemoting 2.0//EN" "http://getahead.org/dwr//dwr20.dtd"> <dwr> <create javascript="myrevsrse" creator="new"> <param name="class" value="com....
<%@ page import="org.directwebremoting.HttpServletResponse"%> <%@ page import="org.directwebremoting.ScriptTagHelper"%> <script type="text/javascript" src="/dwr/interface/MyService.js"> ...
<bean id="dwrConfig" class="org.directwebremoting.config.DwrConfiguration"> <!-- 配置其他属性 --> <filter-name>DWR Filter <filter-class>org.directwebremoting.servlet.DwrFilter <param-name>...
如果手动添加jar包,则需要下载DWR的相关jar包并将其放置在项目的`WEB-INF/lib`目录下。 ##### 2. 创建并配置`dwr.xml` 接着,在`WEB-INF`目录下创建一个名为`dwr.xml`的文件,用于指定哪些Java类可以被远程调用,...
<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 ...
DOCTYPE dwr PUBLIC "-//GetAheadLimited//DTD DirectWebRemoting 1.0//EN" "***"> <dwr> <create creator="new" javascript="JDate"> <param name="class" value="java.util.Date"/> </create> </dwr> ``` ...
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"> <servlet-name>DWR <servlet-class>org....
<groupId>org.directwebremoting <artifactId>dwr 版本号 ``` 2. **配置DWR**:在SpringBoot的主配置类(通常继承`SpringBootServletInitializer`)中,配置DWR的Servlet。这一步会告诉SpringBoot如何启动和...
- 访问DWR官方网站(http://getahead.org/dwr/getstarted)下载`dwr.jar`。 - 确保下载的是适合当前项目版本的DWR JAR文件。 2. **放置JAR文件**: - 将下载好的`dwr.jar`文件放入项目的`WEB-INF/lib`目录下。 ...
- `<servlet>` 定义了名为 `dwr-invoker` 的Servlet,其类路径为 `org.directwebremoting.servlet.DwrServlet`。 - `<init-param>` 设置了调试模式为开启状态 (`true`),便于开发过程中查看日志。 - `...
而DWR则是实现Ajax功能的一种方式之一,它通过简化远程调用的过程来提高开发效率。 ### DWR的特性 #### 1. 直接调用Java方法 DWR的核心功能是允许JavaScript代码直接调用服务器端的Java方法。这种机制极大地简化了...
"http://getahead.org/dwr/dwr20.dtd"> <dwr> <create creator="new" javascript="JDate"> <param name="class" value="java.util.Date"/> </create> <create creator="new" javascript="Demo"> ...
<filter-class>org.directwebremoting.servlet.DwrServlet <param-name>debug <param-value>true <param-name>servletPath <param-value>/dwr/ <filter-name>dwr-invoker <url-pattern>/dwr/* ```...
<listener-class>org.directwebremoting.servlet.EfficientShutdownServletContextAttributeListener <listener-class>org.directwebremoting.servlet.EfficientShutdownServletContextListener ...
DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD DirectWebRemoting 2.0//EN" "http://getahead.org/dwr/dwr20.dtd"> <dwr> <create creator="new" javascript="JDate"> <param name="class" value="java.util....
<groupId>org.directwebremoting <artifactId>dwr 版本号 ``` 2. 配置Servlet:在web.xml文件中配置DWRServlet,它负责处理来自客户端的Ajax请求。例如: ```xml <servlet-name>DWRServer <servlet-class>...
<servlet-class>org.directwebremoting.servlet.DwrServlet <servlet-name>dwr-invoker-servlet <url-pattern>/dwr/* ``` #### 二、编写JavaBean - **步骤一**:编写JavaBean类。确保包含一个公共无参构造...