版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/QuietHRH/article/details/82667487
异常处理
web层定义处理请求的方法
web层定义处理异常的方法
错误处理页面
// 测试异常处理
@RequestMapping("testException")
public ModelAndView testException(@RequestParam("num") int num) {
ModelAndView mv = new ModelAndView("hello");
mv.addObject("msg", "result:" + (10/num));
return mv;
}
@ExceptionHandler({Exception.class})
public ModelAndView handlerException(Exception ex) {
System.out.println("出现异常了: " + ex);
ModelAndView mv = new ModelAndView("error");
mv.addObject("exMessage", ex);
return mv;
}
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>您的操作出现错误原因如下:</h1>
<span style="color:red;font-size:25px;">${exMessage}</span>
</body>
</html>
非注解版
自定义异常类
异常处理器实现
错误页面
异常处理器配置
package cn.hrh.exception;
/**
* 自定义异常类
*/
public class CustomException extends Exception{
public CustomException(String exMessage) {
super();
this.exMessage = exMessage;
}
private String exMessage;
public String getExMessage() {
return exMessage;
}
public void setExMessage(String exMessage) {
this.exMessage = exMessage;
}
}
package cn.hrh.exception;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.io.StringWriter;
public class CustomHandlerExceptionResolver implements HandlerExceptionResolver {
@Override
public ModelAndView resolveException(HttpServletRequest request,
HttpServletResponse response,
Object handler,
Exception ex) {
// 1 发短信,发邮件通知主要负责人
// 2 判断是自定义异常吗?
String exMessage = "";
if(ex instanceof CustomException) {
// 2.1 如果是自定义异常, 获取异常信息
CustomException customException = (CustomException) ex;
exMessage = ((CustomException) ex).getExMessage();
}else {
// 2.2 其他异常, 获取异常信息
StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stringWriter);
ex.printStackTrace(printWriter);
CustomException customException = new CustomException(stringWriter.toString());
exMessage = customException.getExMessage();
}
// 3 给浏览器返回响应信息
ModelAndView mv = new ModelAndView("error");
mv.addObject("exMessage", exMessage);
return mv;
}
}
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>您的操作出现错误原因如下:</h1>
<span style="color:red;font-size:25px;">${exMessage}</span>
</body>
</html>
<bean class="cn.itcast.exception.CustomHandlerExceptionResolver"/>
分页插件
添加依赖
在mybatis-config.xml中配置分页插件
修改业务类
<!-- 分页助手 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>3.7.5</version>
</dependency>
<dependency>
<groupId>com.github.jsqlparser</groupId>
<artifactId>jsqlparser</artifactId>
<version>0.9.1</version>
</dependency>
<plugins>
<!-- 配置分页助手的插件 -->
<plugin interceptor="com.github.pagehelper.PageHelper">
<!-- 指定数据库方言 -->
<property name="dialect" value="mysql"/>
<!-- 设置为true时,查询结果中会查询出总条数信息 -->
<property name="rowBoundsWithCount" value="true"/>
</plugin>
</plugins>
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
// 查询分页显示的数据
public Pagebean queryPagebean(int pageNum, int numPerPage) {
// 使用分页助手
PageHelper.startPage(pageNum, numPerPage);
// 调用mapper查询需要的数据
List<User> userList = userMapper.queryAll(); // 分页显示的数据
// 需要查询总记录数和分页显示数据
PageInfo<User> pageInfo = new PageInfo<User>(userList);
// 1 查询总记录数
long total = pageInfo.getTotal();
// 2 将总记录数和分页显示数据封装到pagebean中返回
Pagebean pagebean = new Pagebean(total, userList);
return pagebean;
}
}