1.导入dws.jar
2.web.xml
<servlet> <servlet-name>dwr</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</servlet-name> <url-pattern>/dwr/*</url-pattern> </servlet-mapping>
3.编写/WEB-INF/dwr.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN" "http://getahead.org/dwr/dwr20.dtd"> <dwr> <allow> <create creator="new" javascript="AjaxTest"> <param name="class" value="com.southking.ajax.AjaxTest"/> </create> </allow> </dwr>
4.编写类
package com.southking.ajax;
public class AjaxTest {
public String opretion(String data){
return data+" Ajax!";
}
}
5.编写页面
<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<script type="text/javascript" src="/dwr/interface/AjaxTest.js"></script>
<script type='text/javascript' src='/Struts2Demo/dwr/engine.js'></script>
<script type="text/javascript">
function myFunction(str)
{
AjaxTest.opretion(str,callBackFunction);
}
function callBackFunction(data)
{
alert(data);
}
</script>
</head>
<body οnlοad=myFunction('我是')>
试试
</body>
</html>