日期:2014-05-16  浏览次数:20622 次

DWR使用JSON格式的回调

web.xml文件
<?xml version="1.0" encoding="GBK"?>

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">

	<!-- 配置DWR的核心Servlet -->
	<servlet>
		<!-- 指定DWR核心Servlet的名字 -->
		<servlet-name>dwr-invoker</servlet-name>
		<!-- 指定DWR核心Servlet的实现类 -->
		<servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
		<!--  指定DWR核心Servlet处于调试状态 -->
		<init-param>
			<param-name>debug</param-name>
			<param-value>true</param-value>
		</init-param>
	</servlet>
	<!-- 指定核心Servlet的URL映射 -->
	<servlet-mapping>
		<servlet-name>dwr-invoker</servlet-name>
		<!-- 指定核心Servlet映射的URL -->
		<url-pattern>/dwr/*</url-pattern>
	</servlet-mapping>

</web-app>
?处理类Java
package com.lbx.dwr.json.test;

public class Test1 {
	
	public String sss(String name){
		System.out.println("Test1.test()");
		return name + ",你好!";
	}
	
}
?dwr.xml文件配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 3.0//EN" "http://getahead.org/dwr/dwr30.dtd">

<dwr>
 
  <allow>

    <create creator="new" javascript="Demo">
      <param name="class" value="com.lbx.dwr.json.test.Test1"/>
    </create>
    
  </allow>
  
</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>My JSP 'index.jsp' starting page</title>
		<meta http-equiv="pragma" content="no-cache">
		<meta http-equiv="cache-control" content="no-cache">
		<meta http-equiv="expires" content="0">
		<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
		<meta http-equiv="description" content="This is my page">
		<!-- 导入DWR为Demo对象动态生成的JavaScript代码库 -->
		<script type='text/javascript' src='dwr/interface/Demo.js'></script>
		<!-- 导入DWR引擎的核心JavaScript代码库 -->
		<script type='text/javascript' src='dwr/engine.js'></script>
		<!-- DWR的工具JavaScript文件,通过这工具的工具方法可以简化DOM操作 -->
		<script type='text/javascript' src='dwr/util.js'></script>
		
		<script type="text/javascript">
			function sendMessage(){
				var username = $("name").value;
				alert(username);
				Demo.sss(username,{
					//指定回调函数
				  	callback:getMsg,
					//指定超时时长
				  	timeout:1000,
					//指定错误处理函数
				  	errorHandler:function(message) { alert("错误提示: " + message); },
					//指定
					warningHandler:function(message) { alert("Oops: " + message); },
					textHtmlHandler: function(message) { alert("Oops: " + message); },
					exceptionHandler: function(message) { alert("Oops: " + message); },
					//指定发送请求的方式
					httpMethod:'POST',
					async:true,
					//指定发送请求之前的勾子函数
					preHook:function(){alert('远程调用之前...')},
					//指定发送请求之后的勾子函数
					postHook:function(){alert('远程调用之后...')}
				});
			}
			//回调函数
			function getMsg(data)
			{
				alert("s");
				document.getElementById("show").innerHTML = data;
			}
		</script>
		
	</head>

	<body>
		<h3>
			使用JSON格式回调
		</h3>
		请输入您的名字
		<input id="name" name="name" type="text" />
		<br />
		<input type="button" value="使用JSON格式回调" onclick="sendMessage();" />
		<hr />
		下面是服务器的回应:
		<br />