(1)web.xml与DWR结合
//web.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<display-name>Ajax</display-name>
<servlet>
<servlet-name>dwr-invoker</servlet-name>
<servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>
<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>
</web-app>
(2)dwr.xml
//dwr.xml(和web.xml同一目录)
<?xml version="1.0" encoding="UTF-8"?>
<dwr>
<init>
<converter id="person" class="dwr.java.Person2Converter"/>
</init>
<allow>
<create creator="new" javascript="JDate">
<param name="class" value="java.util.Date"/>
</create>
<create creator="new" javascript="Demo" scope="session">
<param name="class" value="dwr.java.Test"/>
</create>
<create creator="new" javascript="Table" scope="session">
<param name="class" value="dwr.java.Table"/>
</create>
<convert converter="person" match="dwr.java.*"/>
</allow>
<signatures>
<![CDATA[
import java.util.*;
import dwr.java.*;
Table.getArgs(String,Map<String, String>);
Table.getArgo(String,Map<String, Person>);
]]>
</signatures>
</dwr>
(3) 配置说明
3.1我们可能需要的:new和spring
<!--new-->
<create creator="new" javascript="JDate">
<param name="class" value="java.util.Date"/>
<include method="getItem"/>
<include method="findItems"/>
</create>
<!--Spring-->
<create creator="spring" javascript="spring">
<param name="beanName" value="******"/>
</create>
如果<include method=""/>没有配置,那class内的所有方法都是被允许调用的
3.2 自定义creator
构造一个extends AbstractCreator implements Creator的class。
在dwr.xml配置如下:
<init>
<creator id="struts" class="*.*.*Creator"/>
</init>
<allow>
<create creator="struts" javascript="FormName">
<param name="formBean" value=" FormName "/>
</create>
</allow>
3.3 signatures 为了识别Collections 里数据类型
No entry required in signatures section - no generic type parameters:
public void method (String p);
public void method (String[] p);
Entry required - DWR can't work it out from reflection:
public void method (List<Date> p);
public void method (Map<String, Bean> p);
No entry required - DWR guesses correctly:
public void method (List<String> p);
public void method (Map<String, String> p);
No entry required - DWR works it out from runtime types:
public List<Date> method (String p);
3.4对于不能自动识别的数据类型应在dwr.xml配置
<signatures>
<![CDATA[
import java .util.*;
import dwr .java.*;
Table. getArg (String ,Map<String, String>);
Table. getArgo (String,Map<String, Person>);
]]>
</signatures>
建议除基本类型外,都在这里配置。比如不配置Map,dwr可以自动识别,但会有警告
(4) javascript 异步调用服务端已注册类方法
<script type='text/javascript' src='dwr/interface/JDate.js'>
</script>
对应
<create creator="new" javascript="JDate">
<param name="class" value="java.util.Date"/>
</create>
dwr的基本函数
<script type='text/javascript' src='dwr/engine.js'> </script>
<script type=‘text/javascript’ src=‘dwr/util.js’> </script>
用户java类 -> .js
<script type=‘text/javascript’ src=‘dwr/deprecated.js’> </script>
不建议使用的函数
http://*****:**/****/dwr 调试界面