1.dwr在spring配置文件的配置:
<!-- 注意這里新增加的dwr tag, 為使其生效,文件頭中要聲明namespace -->
<dwr:configuration />
<!-- 要求DWR在Spring容器中檢查擁有@RemoteProxy 和 @RemoteMethod注解的類。注意它不會去檢查Spring容器之外的類。 -->
<dwr:annotation-config id="dwr_as" />
<!-- 指定dwr所調用的后台方法的路徑 -->
<dwr:annotation-scan base-package="com.boco.dcn.report.dwrremote" />
<!-- 要求DWR將util.js和engine.js映射到dwrController -->
<dwr:url-mapping />
<!-- 定義dwrController 部署項目時, 請把debug設為false -->
<dwr:controller id="dwrController" debug="false" />
2.web.xml的配置
<!-- ajax工程dwr的配置 -->
<servlet>
<servlet-name>dwr</servlet-name>
<servlet-class>org.directwebremoting.spring.DwrSpringServlet</servlet-class>
<!-- 是否允許調試,如果要在瀏覽器中調試則必須設置為true -->
<init-param>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
<!-- 如果允許跨域請求,則必須將此值設置為false,默認值為true -->
<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>dwr</servlet-name>
<!-- 這里指定的路徑是前台引入時的路徑 -->
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
3.后端java代碼
@RemoteProxy注解該類,將該類暴露出來
@RemoteProxy(creator=SpringCreator.class)
public class ReportRemote {
public void test(String name){
System.out.println(name+",你好");
}
}
4.前端引入dwr
<!-- dwr的類型轉換器,他可以將js轉換為java,然后再轉換為js -->
<script type='text/javascript' src='dwr/engine.js'></script>
<!-- 所調用的后台的方法,src格式為:'dwr/interface/后台類名.js'-->
<script type='text/javascript' src='dwr/interface/ReportRemote.js'></script>
5.前台的調用
ReportRemote.getDeviceFlowArea(參數,返回結果執行的方法體);
ReportRemote.getDeviceFlowArea(value,function(data){}