dwr异常处理

配置异常转换器:

dwr.xml:

<!-- 异常转换 -->
	<convert match="java.lang.Exception" converter="exception"/>
  	<convert match="java.lang.StackTraceElement" converter="bean"/>


UserService:

package com.demo.service;

import java.util.ArrayList;
import java.util.List;

import com.demo.model.User;

public class UserService {
	public static final String SUCCESS="success";
	public String add(User user){
		System.out.println(user);
		return SUCCESS;
	}
	public String deleted(int userId){
		throw new RuntimeException("删除出错了...");
	}
	public String update(User user){
		System.out.println("update:"+user);
		return SUCCESS;
	}
	public List<User> list(){
		List<User> users=new ArrayList<User>();
		users.add(new User(1,"aa"));
		users.add(new User(2,"bb"));
		users.add(new User(3,"cc"));
		return users;
	}
}


delete.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>user delete</title>
<script type="text/javascript" src="<%=request.getContextPath()%>/dwr/engine.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/dwr/util.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/dwr/interface/UserService.js"></script>
<script type="text/javascript">
	UserService.deleted(11,{
		//回调函数  
		callback:callBack,
		  //异常处理
		  errorHandler:function(msg) {
			  alert(msg);
		  }
	});
	function callBack(result){
		alert(result);
	}
</script>
</head>
<body>

</body>
</html>


结果:








posted @ 2017-06-05 14:13  yangykaifa  阅读(269)  评论(0编辑  收藏  举报