引入DWR框架的步骤
1.添加 dwr.jar 包 在 http://getahead.ltd.uk/dwr/ 下载 dwr.jar,放到WEB-INF/lib下
2.编写 dwr.xml,修改 web.xml
dwr.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC
"-//GetAhead Limited//DTD Direct Web Remoting 1.0//EN"
"http://www.getahead.ltd.uk/dwr/dwr10.dtd">
<dwr>
<allow>
<create creator="new" javascript="HelloJS1">
<param name="class" value="wust.Hello"></param>
</create>
</allow>
</dwr>
web.xml 中添加
<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>
3.编写 jsp 页面, js文件,java 源文件。
test.jsp
<%@ page language="java" pageEncoding="utf-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=BIG5">
<title>第一個DWR程式</title>
<script type='text/javascript' src='dwr/interface/HelloJS1.js'></script>
<script type='text/javascript' src='dwr/engine.js'></script>
<script type='text/javascript' src='dwr/util.js'></script>
<script type='text/javascript' src='HelloJS.js'></script>
</head>
<body>
<input id="user" type="text" />
<input type='button' value='哈囉' onclick='hello123();' />
<div id="result"></div>
</body>
</html>
HelloJS.js
function hello123() {
var user = $("user").value;
HelloJS1.hello456(user, callback);
}
function callback(msg) {
DWRUtil.setValue("result", msg);
}
Hello.java
package wust;
public class Hello {
public String hello456(String name) {
return "哈囉!" + name + "!您的第一個DWR!";
}
}
test.jsp->hello123()->HelloJS.js->HelloJS1->dwr.xml->wust.Hello->HelloJS.js->test.jsp