spring mvc的错误控制机制
来源:互联网 发布:html怎么写php代码 编辑:程序博客网 时间:2024/06/05 16:29
先在spring-servlet.xml里面增加了一个exceptionResolver,配置和jspresolver在前缀后缀上有些
关系,所以一起贴出来:
<bean id="jspViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass">
<value>org.springframework.web.servlet.view.JstlView</value>
</property>
<property name="prefix" value="/template/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<bean id="exceptionResolver"
class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="defaultErrorView">
<value>error/defaultError</value>
</property>
<property name="exceptionMappings">
<props>
<prop
key="com.sillycat.plugin.commons.exceptions.ManagerException">
error/managerError
</prop>
<prop
key="com.sillycat.plugin.commons.exceptions.DaoException">
error/daoError
</prop>
</props>
</property>
</bean>
这样处理就是当遇到ManagerException时会自动转向error/managerError页面
当遇到DaoException时会自动转向 error/daoError页面
页面如下,templete/jsp/error/managerError.jsp:
<%@ page contentType="text/html;charset=GBK" language="java"
pageEncoding="GBK"%>
<%@ include file="/template/jsp/include/taglibs.jsp"%>
<%@ page import="java.util.Enumeration,java.util.Iterator"%>
<script>
function showErr(){
var isHidde = document.all.isHidde.value;
//alert(isHidde);
if( isHidde == "true" ){
document.all.errdiv.style.display='block';
document.all.isHidde.value= 'false';
document.all.showbtn.value="隐藏错误信息";
}else{
document.all.errdiv.style.display='none';
document.all.isHidde.value= 'true';
document.all.showbtn.value="显示错误信息";
}
}
</script>
<html>
<head>
<title>this is failure</title>
</head>
<body onload="showErr()">
出错啦。。。。。。。。。。。。。。。
<br />
<c:set value="${exception}" var="ee" />
<jsp:useBean id="ee" type="java.lang.Exception" />
<%=ee.getMessage()%><br />
<input type="hidden" id="isHidde" value="false" />
<input type="button" id="showbtn" onclick="showErr();" />
<br>
<table id="errdiv" align="center" bgcolor="black">
<tr>
<td>
<font color="green"> <%
ee.printStackTrace(new java.io.PrintWriter(out));
%> </font>
</td>
</tr>
</table>
</body>
</html>
DaoException修改为集成自runtimeException,DaoException.java:
package com.sillycat.plugin.commons.exceptions;
public class DaoException extends RuntimeException {
private static final long serialVersionUID = 1L;
public DaoException() {
super();
}
public DaoException(String msg) {
super(msg);
}
public DaoException(Throwable cause) {
super(cause);
}
public DaoException(String msg, Throwable cause) {
super(msg, cause);
}
}
ManagerException.java如下:
package com.sillycat.plugin.commons.exceptions;
public class ManagerException extends RuntimeException {
private static final long serialVersionUID = 1L;
// error code
public String errorCode;
public ManagerException() {
super();
}
public ManagerException(String message) {
super(message);
}
public ManagerException(String message, Throwable cause) {
super(message, cause);
}
public ManagerException(Throwable cause) {
super(cause);
}
public ManagerException(String errorCode, String message) {
super(message);
this.errorCode = errorCode;
}
public ManagerException(String errorCode, String message, Throwable cause) {
super(message, cause);
this.errorCode = errorCode;
}
public String getErrorCode() {
return errorCode;
}
public void setErrorCode(String errorCode) {
this.errorCode = errorCode;
}
}
这样配置和设计后,以后Dao层这样书写,UserDaoIbatisImpl.java:
package com.sillycat.core.dao.ibatis;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.sillycat.core.dao.UserDao;
import com.sillycat.core.model.User;
import com.sillycat.plugin.commons.base.BaseSqlMapClientDaoSupport;
import com.sillycat.plugin.commons.exceptions.DaoException;
public class UserDaoIbatisImpl extends BaseSqlMapClientDaoSupport implements
UserDao {
private static final String LOAD_ALL_USERS = "getUser";
public List getAll() {
List list = null;
try {
list = getSqlMapClientTemplate().queryForList(LOAD_ALL_USERS, null);
} catch (Exception e) {
throw new DaoException(e);
}
return list;
}
public User get(Integer id) {
return null;
}
public void save(User o) {
}
public void delete(Integer id) {
}
}
在manager层这样调用,不抓dao层的错误
public List getAllUser() {
if(true){
throw new ManagerException("0","test");
}
List list = userIbatisDao.getAll();
return list;
}
当DaoException抛出时会走daoError.jsp页面。
如果Controller层没有抓ManagerException.jsp,页面也会统一走向managerError.jsp
如果这样书写
protected void onList(HttpServletRequest request,
HttpServletResponse response, ModelAndView mav) throws Exception {
List list = null;
try {
list = userManager.getAllUser();
} catch (ManagerException e) {
System.out.println("aaaaaaaaaaaaaaaaaaaaaaa");
}
mav.addObject("items", list);
}
就不会走managerError.jsp了。需要自己控制提示信息和走向
- spring mvc的错误控制机制
- spring MVC错误异常机制回收
- Spring MVC的工作机制
- SPRING MVC的实现机制
- SPRING MVC的实现机制
- spring MVC框架的工作机制
- Spring MVC的参数绑定机制
- Spring--MVC--如何书写MVC的控制层Controller
- Spring MVC 机制
- Spring mvc视图机制
- Spring mvc http 406错误的解决之道
- 整合 Spring mvc mybatis 的错误
- 初学Spring MVC所遇到的错误
- Spring MVC使用ajax的400错误
- Spring MVC中日期转换的错误
- Spring学习笔记之Spring MVC的工作机制
- Spring学习笔记(十) --- 浅析Spring MVC的工作机制
- Spring mvc 400错误
- 2010-4-7
- MFC Grid control 2.26在VC6.0中的使用
- jqury 使iframe自适应高度(歉容IE6+、fireFox、chrome)
- 数据挖掘实例
- Linux下编译搜索头文件以及库文件的路径
- spring mvc的错误控制机制
- 关于c语言
- Library学习
- 在linux环境下启动oracle服务以及监听
- NSIS附加数据库,分离数据库脚本代码
- 面试问题
- 做外汇首选MF, MT4平台, 最低2个点差, 最小做0.01手...
- 英语中常见的123个中国习语
- java 心跳检测