DWR 配置与参数
WEB.XML
<servlet>
<servlet-name>dwr-invoker</servlet-name>
<servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>activeReverseAjaxEnabled</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>initApplicationScopeCreatorsAtStartup</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>maxWaitAfterWrite</param-name>
<param-value>500</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dwr-invoker</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>dwr-invoker</servlet-name>
<servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>activeReverseAjaxEnabled</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>initApplicationScopeCreatorsAtStartup</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>maxWaitAfterWrite</param-name>
<param-value>500</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dwr-invoker</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
多dwr.xml文件的配置
可能有几种情况,我们一一列举。 一个servlet,多个dwr.xml配置文件;多个servlet,每个servlet对应一个或多个dwr.xml.
一个Servlet 多个dwr.xml配置文件:
<servlet>
<servlet-name>dwr-invoker</servlet-name>
<servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>
<init-param>
<param-name>config-1</param-name>
<param-value>WEB-INF/dwr1.xml</param-value>
</init-param>
<init-param>
<param-name>config-2</param-name>
<param-value>WEB-INF/dwr2.xml</param-value>
</init-param>
</servlet>
在这种配置下,param-name的值必须以config开头。param-name可以有>=0个。如果没有param-name,那么将会读取WEB-INF/dwr.xml。如果有大于零个param-name,那么WEB-INF/dwr.xml文件将不会被读取。
多个servlet,每个servlet对应一个或多个dwr.xml :
<servlet>
<servlet-name>dwr-invoker</servlet-name>
<servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>dwr-invoker1</servlet-name>
<servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>
<init-param>
<param-name>config-admin</param-name>
<param-value>WEB-INF/dwr1.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dwr-invoker</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>dwr-invoker1</servlet-name>
<url-pattern>/dwr1/*</url-pattern>
</servlet-mapping>
在这种情况下,我们可以根据J2EE security来控制权限,针对不同url,加不同的角色。
页面引用
<script type='text/javascript' src='<%=path %>/dwr/interface/loginService.js'></script>
<script type='text/javascript' src='<%=path %>/dwr/engine.js'></script>
<script type='text/javascript' src='<%=path %>/dwr/util.js'></script>
loginService 是dwr.xml中定义的 javaScript名称
<script type='text/javascript' src='<%=path %>/dwr/interface/loginService.js'></script>
<script type='text/javascript' src='<%=path %>/dwr/engine.js'></script>
<script type='text/javascript' src='<%=path %>/dwr/util.js'></script>
测试路径:
http://localhost:8080/项目名称/dwr
dwr.xml是DWR的配置文件。默认情况下,应该把它放到WEB-INF目录下
new: 用Java的new关键字创造对象。
static: 使用“getInstance()的方法(默认情况下)来获得类的一个实例.
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+)
常用的3种Creator:
Creator | Param | User |
new | class | 类的全名称(包括包路径) |
scripted | language | BSF框架支持的脚本语言名称(BSF为apache项目) |
script | 返回远程对象的脚本,脚本可以指定一些属性,多数情况下一般只设置param节点配置.属性很少设置. | |
spring | Location* | 任何以location开头的参数,每个参数都是指定一个spring的配置文件,在参数没有设置的情况下DWR会去读取spring的全局的配置文件. |
beanName | 从配置文件中读取的bean的名称 |
new:
<allow>
<create creator="new" javascript="Blah">
<param name="class" value="java.util.Date"/>
</create>
...
</allow>
static: 看官网文档 http://directwebremoting.org/dwr/documentation/server/configuration/dwrxml/creators/index.html#none
none:看官网文档 http://directwebremoting.org/dwr/documentation/server/configuration/dwrxml/creators/index.html#none
scripted:
<allow>
...
<create creator="script" javascript="EmailValidator">
<param name="class" value="org.apache.commons.validator.EmailValidator"/>
<param name="language" value="beanshell"/>
<param name="script">
import org.apache.commons.validator.EmailValidator;
return EmailValidator.getInstance();
</param>
</create>
...
</allow>
Parameter | DWR Version | Description |
---|---|---|
language | 1.0 | The language that we are scripting in. This string is passed to BSF. For example 'beanshell'. (required) |
class | 1.0 | The type of object that is created. This is required for security reasons. |
script | 1.0 | The script to execute which will return a Java Object to remote. (required unless scriptPath is used) |
scriptPath | 1.1 | The path to a script stored in a file. (required unless script is used) |
reloadable | 1.1 | Should the ScriptedCreator check for changes to scripts loaded using scriptPath? (optional, defaults to true) |
spring:
<allow>
...
<create creator="spring" javascript="Fred">
<param name="beanName" value="Shiela"/>
</create>
...
</allow>
使用location*参数
如果你要在dwr.xml中指定使用哪些bean,你可以使用location*参数。你可以指定任意多个文件,只要参数以location开始 并且唯一即可。例如:location-1, location-2。这些location被用做Spring的ClassPathXmlApplicationContext的参数:
<allow>
<create creator="spring" javascript="Fred">
<param name="beanName" value="Shiela"/>
<param name="location" value="beans.xml"/>
</create>
</allow>
直接设置BeanFactory
SpringCreator有一个静态方法 setOverrideBeanFactory(BeanFactory) 用来通过编程的方式直接设置BeanFactory。
JSF:看官网文档 http://directwebremoting.org/dwr/documentation/server/configuration/dwrxml/creators/index.html#none
Struts:看官网文档 http://directwebremoting.org/dwr/documentation/server/configuration/dwrxml/creators/index.html#none
pageflow:看官网文档 http://directwebremoting.org/dwr/documentation/server/configuration/dwrxml/creators/index.html#none
Converters:
http://directwebremoting.org/dwr/documentation/server/configuration/dwrxml/converters/index.html