web中添加ajax框架dwr

DWR(Direct Web Remoting)是一个用于改善web页面与Java类交互的远程服务器端Ajax开源框架,可以帮助开发人员开发包含AJAX技术的网站。它可以允许在浏览器里的代码使用运行在WEB服务器上的JAVA函数,就像它就在浏览器里一样。

一、web.xml配置dwr

	<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>false</param-value>
		</init-param>
		<load-on-startup>3</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>dwr-invoker</servlet-name>
		<url-pattern>/dwr/*</url-pattern>
	</servlet-mapping>	
二、dwr.xml配置信息。其中SwitchRecordRemote是spring中配置的id

<!DOCTYPE dwr PUBLIC
    "-//GetAhead Limited//DTD Direct Web Remoting 1.0//EN" 
    "http://www.getahead.ltd.uk/dwr/dwr10.dtd">
<dwr>
	<allow>
		<create creator="spring" javascript="SwitchRecordRemote" scope="application">
			<param name="beanName" value="SwitchRecordRemote"/>
			<include method="deleteRecord"/> 
		</create>
	</allow>
</dwr>
三、applicationContext-dwr.xml配置dwr用到的bean

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
	<bean id="SwitchRecordRemote" class="com.sasis.webapp.dwr.SwitchRecordRemote">
		<property name="switchRecordManager" ref="switchRecordManager" />
	</bean>	
</beans>
四、switchRecordManager.jsp调用后台dwr定义的方法

<%@ page pageEncoding="utf-8"%>
<%@ include file="/common/taglibs.jsp"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@ taglib prefix="aa" uri="http://ajaxanywhere.sourceforge.net/" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<script type='text/javascript' src='dwr/engine.js'></script>
<script type='text/javascript' src='dwr/interface/SwitchRecordRemote.js'></script>
function deleteRecord(){
  
  var elements = document.getElementsByName("selected_id");
  var selected=new Array();
  for (var i=0;i <elements.length; i++){
     var e = elements[i];
     if(e.checked){
         var bb = e.value; 
		selected.push(bb);
	 }
  }
  if(confirm("确实要删除?")){
   SwitchRecordRemote.deleteRecord(selected,deleteRecordCallBack);
  }else{
			alert("<fmt:message key="common.delete.message"/>");
		}	
  }	
  function deleteRecordCallBack(bln){
	if(bln){
	
	       ajaxAnywhere.getZonesToReload = function() {
    return "recordZone";
	}
	ajaxAnywhere.getAJAX('switchRecord.html?method=switchRecordManager&flightInfoId=<c:out value="${param.flightInfoId}"/>','recordZone')
	}
}
</script>
</html> 



你可能感兴趣的:(DWR)