例举一个例子:
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>anareport</display-name>
<!-- Desc: DwrServlet Mapping Config -->
<servlet>
<servlet-name>DWRServlet</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>
<init-param>
<param-name>crossDomainSessionSecurity</param-name>
<param-value>false</param-value>
</init-param>
<init-param>
<param-name>allowScriptTagRemoting</param-name>
<param-value>true</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>DWRServlet</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
</web-app>
首先dwr.xml文件中的配置
<dwr>
<allow>
<create creator="new" javascript="carCmainUnderWriteList">
<param name="class" value="com.sinosoft.anareport.dwr.DWRAction.carUnderWriterListAction" />
</create>
<convert match="com.sinosoft.anareport.dwr.DWRAction.Com" converter="bean" javascript="com"/>
</allow>
</dwr>
Com类:
package com.sinosoft.anareport.dwr.DWRAction; //( 为了有说服力 把Com类与carUnderWriterListAction都放在 DWRAction包下)
public class Com {
private String comCode ;
public String getComCode() {
return comCode;
}
public void setComCode(String comCode) {
this.comCode = comCode;
}
}
dwr实例Action:carUnderWriterListAction如下:
package com.sinosoft.anareport.dwr.DWRAction;
public class carUnderWriterListAction {
public String str2 (String name, Com com) {
System.out.println("com.comCode = " + com.getComCode());
System.out.print("name = " + name);
return name;
}
}
jsp页面:
<script type='text/javascript' src='/anareport/dwr/interface/carCmainUnderWriteList.js'></script>
<script type='text/javascript' src='/anareport/dwr/engine.js'></script>
<script type='text/javascript' src='/anareport/dwr/util.js'></script>
<script type="text/javascript">
function test() {
var c = new com(); // 在此处就可以直接new javascript属性的值(在dwr中<convert ...... javascript="com"/> )
c.comCode = "06320003"; // 这里就可以设置Com类的属性值
carCmainUnderWriteList.str2("candy",c,function(data){ // 这里的类Com的实例就可以直接传进去
alert("data="+data);
});
}
</script>
<input type=button value="button" οnclick="test();">
访问jsp页面弹出来的内容为 data=candy,后台打印的com.comCode = 06320003 name = candy
希望能够帮助一些求知的朋友。