dwr 写的小程序,配置

第一、在web.xml里面有如下配置:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">

<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>

</web-app>

第二,在web.xml同目录下添加一个dwr.xml文件:javascript 唯一,作为jsp页面的引用

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN" "http://www.getahead.ltd.uk/dwr/dwr20.dtd">

<dwr>
<allow>
<create creator="new" javascript="Hello" >
<param name="class" value="com.hoo.entity.Message"/>
</create>
</allow>
</dwr>

第三,写一个java类

package com.hoo.entity;

public class Message {

public String hello(String name) {
return "这是" + name + "的第一个dwr";
}

}

第四,写jsp页面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>Chat</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<script type="text/javascript" src="${pageContext.request.contextPath}/dwr/engine.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/dwr/util.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/dwr/interface/Hello.js"></script>
<%--<script type="text/javascript" src="chat.js"> </script>
--%><script type="text/javascript">
function hello() {
var user = document.getElementById("user").value;
//var user = $('user').value;
Hello.hello(user, callback);
}

function callback(msg) {
DWRUtil.setValue(
'result', msg);
}
</script>

</head>

<body>
<input id="user" type="text" />
<input type='button' value='你好' onclick='hello();' />
<jsp:useBean id="abc" class="com.hoo.entity.Message"></jsp:useBean>
<jsp:getProperty property="str" name="abc"/>
<div id="result"></div>
</body>
</html>

要记得在项目中添加的包有:commons-logging.jar  dwr.jar   log4j.jar



原文地址:https://www.cnblogs.com/kunpengit/p/2387519.html