Spring MVC Exception Handling and Global Exception Example

Spring provides a robust exception handling framework. We can handle exceptions using @ResponseStatus, @ExceptionHandler, HandlerExceptionResolver and @ControllerAdvice. @ResponseStatus defines a status code for the given exception and can be used at custom exception class level and method level with @ExceptionHandler in controller. In controller class, we can define handler method using @ExceptionHandler and this will be controller specific. To handle exception globally, spring provides @ControllerAdvice that will be available for every controller. Define a class for global exception and annotate it with @ControllerAdvice and we need to define methods using @ExceptionHandler annotation within the class. Spring provides one more approach to handle exception. Use HandlerExceptionResolver in spring XML or java configuration where we can define mappings of exception type and view name. Find the complete example with description.

Spring MVC @ControllerAdvice Annotation Example

@ControllerAdvice annotation is a specialization of @Component. The classes annotated with @ControllerAdvice are auto detected by classpath scanning. The use of @ControllerAdvice is advising all or selected controllers for @ExceptionHandler, @InitBinder and @ModelAttribute. What we have to do is create a class annotated with @ControllerAdvice and create required method which will be annotated with @ExceptionHandler for global exception handling, @InitBinder for global init binding and @ModelAttribute for global model attributes addition. Whenever a request comes to controller and its method with @RequestMapping and if there is no locally defined @ExceptionHandler, @InitBinder and @ModelAttribute, the globally defined class annotated with @ControllerAdvice is served. 

Spring中集成RMI的方法

Spring提供了非常方便的RMI(远程方法调用)调用方式,本文介绍下如何在Spring中集成RMI。

使用Gulp作为Web开发服务器

构建工具Gulp.js最近正在变得越来越流行。我们可以用它做很多事,比如合并Javascript文件或者压缩图片。本文将向你介绍如何使用Gulp.js来作为本地Web服务器,而且是内置livereload支持的哦。

前端构建工具Gulp.js简单实例

在软件开发中使用自动化构建工具的好处是显而易见的。通过工具自动化运行大量单调乏味、重复性的任务,比如图像压缩、文件合并、代码压缩、单元测试等等,可以为开发者节约大量的时间,使我们能够专注于真正重要的、有意义的工作,比如设计业务逻辑,编写代码等等。

SpringMVC之Controller常用注解功能全解析

一、简介

在SpringMVC中,控制器Controller负责处理由DispatcherServlet分发的请求,它把用户请求的数据经过业务处理层处理之后封装成一个Model ,然后再把该Model返回给对应的View进行展示。在SpringMVC 中提供了一个非常简便的定义Controller 的方法,你无需继承特定的类或实现特定的接口,只需使用@Controller标记一个类是Controller,然后使用@RequestMapping和@RequestParam等一些注解用以定义URL 请求和Controller方法之间的映射,这样的Controller就能被外界访问到。此外Controller不会直接依赖于HttpServletRequest和HttpServletResponse等HttpServlet 对象,它们可以通过Controller的方法参数灵活的获取到。

JavaScript去掉字符串两端空格方法

JavaScript并没有直接提供去除字符串两端空格(包括换行符等)的方法,以下脚本借鉴于ExtJS的Ext.String.trim方法:

ASCII编码表

ASCII编码表:

java enum枚举类型用法小结

本文总结了一些有关java中enum枚举类型的用法,下面是一个示例:

VirtualBox虚拟机四种网络连接方式小结

VirtualBox中有四种网络连接方式,分别是:网络地址转换模式(NAT)、桥接模式 (Bridged Adapter)、内部网络模式(Internal)、主机模式(Host-only Adapter)。