当前位置: 首页 > news >正文

做网站 贴吧网站如何做收款二维码

做网站 贴吧,网站如何做收款二维码,网站做跳转链接的好处,设计素材网站排行SpringMVC之异常处理 异常分为编译时异常和运行时异常,编译时异常我们trycatch捕获,捕获后自行处理,而运行时异常是不可预期的,就需要规范编码来避免,在SpringMVC中,不管是编译异常还是运行时异常&#xff…

SpringMVC之异常处理

异常分为编译时异常和运行时异常,编译时异常我们trycatch捕获,捕获后自行处理,而运行时异常是不可预期的,就需要规范编码来避免,在SpringMVC中,不管是编译异常还是运行时异常,都可以最终由SpringMVC提供的异常处理器进行统一管理,这样就可以避免随时随地捕获异常的繁琐性。

 

三种处理方式

1.简单异常处理器:使用Spring MVC内置的异常处理器处理:SimpleMappingExceptionResolver

@Component
public class MysimpleMappingExceton  {@Beanpublic SimpleMappingExceptionResolver simpleMappingExceptionResolver(){SimpleMappingExceptionResolver simpleMappingExceptionResolver = new SimpleMappingExceptionResolver();//默认错误simpleMappingExceptionResolver.setDefaultErrorView("default.html");Properties properties = new Properties();properties.setProperty("java.lang.ArithmeticExceotion","erro1.html");properties.setProperty("java.io.FileNotFoundException","erro2.html");simpleMappingExceptionResolver.setExceptionMappings(properties);return simpleMappingExceptionResolver;}
}

2.自定义异常处理器:实现HandlerExceptionResolver接口,自定义异常进行处理

@Component
public class MyHandlerExceptionResolver implements HandlerExceptionResolver {@Overridepublic ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {ModelAndView modelAndView = new ModelAndView();modelAndView.setViewName("/default.html");return modelAndView;}
}

3.使用@ControllerAdvice@ExceptionHandler实现全局异常

@ControllerAdvice
public class GloExceotion {@ExceptionHandler(RuntimeException.class)@ResponseBodypublic Result runtimeException(){Result result = new Result(200,"错误",new Object());return result;}@ExceptionHandler(FileNotFoundException.class)public ModelAndView fileNotException(){ModelAndView modelAndView = new ModelAndView();modelAndView.setViewName("/erro2.html");return modelAndView;}@ExceptionHandler(Exception.class)public ModelAndView Exception(){ModelAndView modelAndView = new ModelAndView();modelAndView.setViewName("/default.html");return modelAndView;}
}

例子

@RestController
public class ExceptionController {@RequestMapping("/e1")public String e1 (){int a=10/0;return "ruuning exception";}@RequestMapping("/e2")public String e2() throws FileNotFoundException {FileInputStream fileInputStream = new FileInputStream("file:/barch:/");return "ruuning exception";}@RequestMapping("/e3")public String e3()  {int [] array ={1,2};System.out.println(array[5]);return "ruuning exception";}
}

http://www.yayakq.cn/news/546139/

相关文章:

  • 网站海报是怎么做的1688创业商机网
  • 做网站后台主要负责什么大型网站开发基本流程
  • 网站建设手机官网成都企业建设网站电话
  • 汕头网站建设推荐html插件代码大全
  • 房屋中介网站建设方案基于h5的移动网站开发
  • 白银市住房和城乡建设局网站织梦网站导入链接怎么做
  • 网站建设模板免费下载成功案例 品牌网站
  • 网站外链推广工具重庆景点攻略
  • 四川建设岗培注册中心官网网络营销seo是什么
  • 南通模板自助建站手机兼职的正规平台有哪些
  • 红古微信网站建设盱眙网站建设
  • 网站推广的工具网站建设服务亿企网络
  • 网站后台功能模块手机怎么看网页源代码
  • 家里的电脑怎样做网站赚钱wordpress输出文章id
  • 本地的营销网站建设哪里可以做网站的
  • 永州网站建设如何百度推广费用多少
  • 如何做地方网站app安装下载官网
  • 北京市违法建设投诉网站行业网站网址
  • 盐城市建设银行网站中国最好的域名注册网站
  • 单纯做网站的公司竞价被恶意点击怎么办
  • 诸城做网站的网络营销工具的分类
  • 徐州开发的网站青岛做网站建设价格
  • wordpress网站主修改网站链接如何做二维码
  • 中国建设银行北京分行官网站什么网站能代做预算
  • 企业网站设计html深圳建设网站的公司哪家好
  • 重要的网站建设wordpress文章排行榜
  • 建设投资公司网站网站域名空间费用
  • 免费建立个人app网站集团网站 wordpress
  • 校园网站建设的意义电子商务网站预算模板
  • 住房和城乡建设局部网站怎么才能提高网站点击量 免费