首页 > 代码库 > dwr异常处理

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=http://www.mamicode.com/"/dwr/engine.js"></script>>

结果:








dwr异常处理