Springmvc异常处理器及拦截器实现代码

网友投稿 257 2022-11-18


Springmvc异常处理器及拦截器实现代码

一、异常处理器

1、实现HandlerExceptionResolver接口

package com.wuxi.exceptions;

import org.springframework.web.servlet.HandlerExceptionResolver;

import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

public class RequestExceptionResolver implements HandlerExceptionResolver {

@Override

public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {

ModelAndView mv = new ModelAndView();

mv.addObject("errorMsg", e.getMessage());//错误信息

mv.setViewName("error");//请求转发的页面

return mv;

}

}

2、springmvc的xml配置文件

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xmlns:mvc="http://springframework.org/schema/mvc"

xsi:schemaLocation="http://springframework.org/schema/beans

https://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/context

https://springframework.org/schema/context/spring-context.xsd

http://springframework.org/schema/mvc

https://springframework.org/schema/mvc/spring-mvc.xsd">

</bean>

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xmlns:mvc="http://springframework.org/schema/mvc"

xsi:schemaLocation="http://springframework.org/schema/beans

https://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/context

https://springframework.org/schema/context/spring-context.xsd

http://springframework.org/schema/mvc

https://springframework.org/schema/mvc/spring-mvc.xsd">

</bean>


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Spring如何基于xml实现声明式事务控制
下一篇:Java fastdfs客户端实现上传下载文件
相关文章

 发表评论

暂时没有评论,来抢沙发吧~