Skip to content

Commit a66771d

Browse files
committed
springmvc的异常处理
1 parent 0a53a48 commit a66771d

File tree

7 files changed

+109
-5
lines changed

7 files changed

+109
-5
lines changed

WebRoot/WEB-INF/jsp/custom_error.jsp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<%@ page language="java" contentType="text/html; charset=UTF-8"
2+
pageEncoding="UTF-8"%>
3+
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
4+
<html>
5+
<head>
6+
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
7+
<title>错误页面</title>
8+
</head>
9+
<body>
10+
${ex}
11+
</body>
12+
</html>

WebRoot/WEB-INF/jsp/error.jsp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<%@ page language="java" contentType="text/html; charset=UTF-8"
2+
pageEncoding="UTF-8"%>
3+
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
4+
<html>
5+
<head>
6+
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
7+
<title>错误页面</title>
8+
</head>
9+
<body>
10+
出错啦~~
11+
${message }
12+
</body>
13+
</html>

config/spring/springmvc.xml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,21 @@
110110
<!-- 对资源文件内容缓存时间,单位秒 -->
111111
<property name="cacheSeconds" value="120"/>
112112
</bean>
113+
114+
<!-- springmvc提供的简单异常处理器 -->
115+
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
116+
<property name="defaultErrorView" value="/WEB-INF/jsp/error.jsp"/>
117+
<property name="exceptionAttribute" value="ex"/>
118+
<property name="exceptionMappings">
119+
<props>
120+
<prop key="ssm.exception.CustomException">/WEB-INF/jsp/custom_error.jsp</prop>
121+
</props>
122+
</property>
123+
</bean>
124+
125+
<!-- 自定义的全局异常处理器
126+
只要实现HandlerExceptionResolver接口就是全局异常处理器
127+
<bean class="ssm.exception.CustomExceptionResolver"></bean>
128+
-->
129+
113130
</beans>

src/ssm/controller/ItemsController.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import org.springframework.web.servlet.ModelAndView;
1616

1717
import ssm.controller.validation.ValidGroup1;
18+
import ssm.exception.CustomException;
1819
import ssm.po.ItemsCustom;
1920
import ssm.po.ItemsQueryVo;
2021
import ssm.service.ItemsService;
@@ -46,10 +47,12 @@ public String editItems(Model model,
4647
// 根据id查询对应的Items
4748
ItemsCustom itemsCustom = itemsService.findItemsById(items_id);
4849

50+
if(itemsCustom == null) {
51+
throw new CustomException("修改的商品信息不存在!");
52+
}
53+
4954
model.addAttribute("itemsCustom", itemsCustom);
5055

51-
// 通过形参中的model将model数据传到页面
52-
// 相当于modelAndView.addObject方法
5356
return "/WEB-INF/jsp/items/editItems.jsp";
5457
}
5558

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package ssm.exception;
2+
3+
//定义一个简单的异常类
4+
public class CustomException extends Exception {
5+
6+
//异常信息
7+
public String message;
8+
9+
public CustomException(String message) {
10+
super(message);
11+
this.message = message;
12+
}
13+
14+
public String getMessage() {
15+
return message;
16+
}
17+
18+
public void setMessage(String message) {
19+
this.message = message;
20+
}
21+
22+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package ssm.exception;
2+
3+
import javax.servlet.http.HttpServletRequest;
4+
import javax.servlet.http.HttpServletResponse;
5+
6+
import org.springframework.web.servlet.HandlerExceptionResolver;
7+
import org.springframework.web.servlet.ModelAndView;
8+
9+
public class CustomExceptionResolver implements HandlerExceptionResolver {
10+
11+
@Override
12+
public ModelAndView resolveException(HttpServletRequest request,
13+
HttpServletResponse response, Object handler, Exception ex) {
14+
15+
ex.printStackTrace();
16+
CustomException customException = null;
17+
18+
//如果抛出的是系统自定义的异常则直接转换
19+
if(ex instanceof CustomException) {
20+
customException = (CustomException) ex;
21+
} else {
22+
//如果抛出的不是系统自定义的异常则重新构造一个未知错误异常
23+
customException = new CustomException("系统未知错误");
24+
}
25+
26+
//向前台返回错误信息
27+
ModelAndView modelAndView = new ModelAndView();
28+
modelAndView.addObject("message", customException.getMessage());
29+
modelAndView.setViewName("/WEB-INF/jsp/error.jsp");
30+
31+
return modelAndView;
32+
}
33+
34+
}

src/ssm/service/impl/ItemsServiceImpl.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,12 @@ public ItemsCustom findItemsById(Integer id) throws Exception {
3636
// 这里根据实际情况,对商品信息进行业务处理
3737
// ....
3838
// 返回扩展类ItemsCustom
39-
ItemsCustom itemsCustom = new ItemsCustom();
40-
// 将items的属性拷贝到itemsCustom
41-
BeanUtils.copyProperties(items, itemsCustom);
39+
ItemsCustom itemsCustom = null;
40+
if(items != null) {
41+
itemsCustom = new ItemsCustom();
42+
// 将items的属性拷贝到itemsCustom
43+
BeanUtils.copyProperties(items, itemsCustom);
44+
}
4245

4346
return itemsCustom;
4447
}

0 commit comments

Comments
 (0)