springMVC之异常处理



来自:http://blog.csdn.net/zdp072/article/details/38693027

1. 自定义一个异常类: UserException.java

  1. public class UserException extends RuntimeException {  
  2.   
  3.     private static final long serialVersionUID = 1L;  
  4.   
  5.     public UserException() {  
  6.         super();  
  7.     }  
  8.   
  9.     public UserException(String message, Throwable cause) {  
  10.         super(message, cause);  
  11.     }  
  12.   
  13.     public UserException(String message) {  
  14.         super(message);  
  15.     }  
  16.   
  17.     public UserException(Throwable cause) {  
  18.         super(cause);  
  19.     }  
  20.   
  21. }  
public class UserException extends RuntimeException {

	private static final long serialVersionUID = 1L;

	public UserException() {
		super();
	}

	public UserException(String message, Throwable cause) {
		super(message, cause);
	}

	public UserException(String message) {
		super(message);
	}

	public UserException(Throwable cause) {
		super(cause);
	}

}

2. 使用用户登录的例子: UserController.java

   假定用户名不存在或用户密码错误系统会抛出异常, 并跳到error.jsp页面

  1. @Controller  
  2. @RequestMapping("/user")  
  3. public class UserController {  
  4.     // 使用map模拟数据库   
  5.     private Map<String, User> userMap = new HashMap<String, User>();  
  6.   
  7.     public UserController() {  
  8.         userMap.put("zhangsan"new User("zhangsan""123"));  
  9.         userMap.put("lishimin"new User("lishimin""456"));  
  10.     }  
  11.       
  12.     // 用户登录之异常处理  
  13.     // 访问方法: http://localhost/springmvc_user/login.jsp  
  14.     @RequestMapping(value="/login", method=RequestMethod.POST)  
  15.     public String login(String username, String password, HttpSession session) {  
  16.         if(!userMap.containsKey(username)) {  
  17.             throw new UserException("用户名不存在");  
  18.         }  
  19.         User user = userMap.get(username);   
  20.         if(!user.getPassword().equals(password)) {  
  21.             throw new UserException("用户密码不正确");  
  22.         }  
  23.         session.setAttribute("loginUser", user);  
  24.         return "redirect:/user/users";  
  25.     }  
  26.       
  27.     /** 
  28.      * 局部异常处理(只能处理这个控制器中的异常) 
  29.      */  
  30.     @ExceptionHandler(value={UserException.class})  
  31.     public String handlerException(UserException e,HttpServletRequest request) {  
  32.         request.setAttribute("exception",e);   
  33.         return "exception/error";  
  34.     }  
  35.       
  36. }  
@Controller
@RequestMapping("/user")
public class UserController {
	// 使用map模拟数据库 
	private Map<String, User> userMap = new HashMap<String, User>();

	public UserController() {
		userMap.put("zhangsan", new User("zhangsan", "123"));
		userMap.put("lishimin", new User("lishimin", "456"));
	}
	
	// 用户登录之异常处理
	// 访问方法: http://localhost/springmvc_user/login.jsp
	@RequestMapping(value="/login", method=RequestMethod.POST)
	public String login(String username, String password, HttpSession session) {
		if(!userMap.containsKey(username)) {
			throw new UserException("用户名不存在");
		}
		User user = userMap.get(username); 
		if(!user.getPassword().equals(password)) {
			throw new UserException("用户密码不正确");
		}
		session.setAttribute("loginUser", user);
		return "redirect:/user/users";
	}
	
	/**
	 * 局部异常处理(只能处理这个控制器中的异常)
	 */
	@ExceptionHandler(value={UserException.class})
	public String handlerException(UserException e,HttpServletRequest request) {
		request.setAttribute("exception",e); 
		return "exception/error";
	}
	
}

3. 配置全局异常处理: user-servlet.xml

  1. <!-- 全局异常处理 -->  
  2. <bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">  
  3.     <property name="exceptionMappings">  
  4.         <props>  
  5.             <prop key="com.zdp.exception.UserException">exception/error</prop>  
  6.         </props>  
  7.     </property>  
  8. </bean>  
<!-- 全局异常处理 -->
<bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
	<property name="exceptionMappings">
		<props>
			<prop key="com.zdp.exception.UserException">exception/error</prop>
		</props>
	</property>
</bean>

4. 错误信息页面: error.jsp

  1. <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  3. <html>  
  4. <head>  
  5. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  6. <title>错误页面</title>  
  7. </head>  
  8. <body>  
  9. 发现错误: ${exception.message}  
  10. </body>  
  11. </html>  
<%@ 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>错误页面</title>
</head>
<body>
发现错误: ${exception.message}
</body>
</html>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值