DWR传递bean参数并返回Map以及异常处理SimpleCode

前端之家收集整理的这篇文章主要介绍了DWR传递bean参数并返回Map以及异常处理SimpleCode前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
公司有个老项目使用了DWR框架技术,配置文件内容很多,抽出代表性的知识点给大家参考,也给自己做个笔记。

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	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">
    <display-name>dwrtest</display-name>  

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

	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>

dwr.xml

<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN" "http://www.directwebremoting.org/dwr/dwr20.dtd">

<dwr>
	<allow>
		<create creator="new" javascript="DwrTestBean">
			<param name="class" value="testbean.DwrTestBean"></param>
			<include method="myMethod1" />
		</create>

		<convert converter="exception" match="java.lang.Exception">
			<param name="include" value="message" />
		</convert>
		<convert converter="bean" match="pojo.OrderBean"></convert>
	</allow>

	<signatures>
		<![CDATA[
		import java.util.Map;
		import pojo.OrderBean;
		
       	Map<String,Object> DwrTestBean.myMethod1(OrderBean orderBean);
       ]]>
	</signatures>
</dwr>


调用的java class method

package testbean;

import java.util.HashMap;
import java.util.Map;

import pojo.OrderBean;

public class DwrTestBean {
	public Map<String,Object> myMethod1(OrderBean orderBean) throws Exception{
		try {
			System.out.println(orderBean);//打印出来页面的传过来的信息则成功把页面的信息传到后台。
			//int i = 1 / 0;//会报异常
			return new HashMap<String,Object>() {
				/**
			 * 
			 */
				private static final long serialVersionUID = 3043066466825780101L;

				{
					put("successKey","success!!");
				}
			};
		} catch (Exception e) {
			throw new Exception("Failed!!");
		}
	}
}

参数的class

package pojo;

public class OrderBean {
	private String id;
	private String orderCd;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getOrderCd() {
		return orderCd;
	}

	public void setOrderCd(String orderCd) {
		this.orderCd = orderCd;
	}

	@Override
	public String toString() {
		return "OrderBean---------> [id=" + id + ",orderCd=" + orderCd + "]";
	}

}

前端文件index.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
<script type="text/javascript" src="dwr/engine.js"></script>
<script type="text/javascript" src="dwr/util.js"></script>
<script type="text/javascript" src="dwr/interface/DwrTestBean.js"></script>
</head>

<script type="text/javascript">
	function call() {
		var orderBean = {
			id : "1",orderCd : "20160320001W"
		};
		DwrTestBean.myMethod1(orderBean,{
			errorHandler : function(message) {
			alert(message);
			},callback : function(data) {
				for ( var p in data) {
					alert(data[p]);
					//alert(data["successKey"]);//或者直接获取某个key的value
				}
			}
		});
	}
</script>
<body>
	<input type="button" id="testBtn" onclick="call()" value="submit"></input>
</body>
</html>
写的不好,有问题或者错误的地方请多多包涵~ 原文链接:https://www.f2er.com/ajax/162359.html

猜你在找的Ajax相关文章