Explore Spring 4 Features - Part 3
In this Post I will Capture the following features:
1) Controller Advice with Custom Annotation
2) Capturing Attributes in ModelAndView with the help of ModelMap while forwarding request from One Controller
to another.(Not a Spring 4 feature)
By Using @ControllerAdvice annotation we can apply an Advice across all the controllers in an application.
Here our objective is to apply an advice not to all the controllers in an Application Context, but to them annotated
with a custom annotation @NoControllerAdviseAnnotated in this case.
The advice that would be applied out here is @ExceptionHandler(Exception.class) for Exception class.
Our Code for ControllerAdvice is as follows:
and it would apply the advice to the controller classes annotated with @NoControllerAdviseAnnotated
so for the Controller Advice to work the only thing we need to do is:
into play and it catches the Exception and executes the handleException() of CustomControllerAdvice class.
In some scenarios, some business Logic mandates us to dispatch from one controller to another, in such scenarios we
use either forward: or redirect: tools.
In such cases often there lies the need to carry some model
objects (containing data) from one controller to another for processing purposes.
When doing redirect RedirectAttributes is there to carry all the data, but during forwarding the request,
we usually put them in the request scope and do the processing, problem happens if the target controller method
has some @ModelAttribute annotated parameter and data needs to be mapped to that parameter from the forwarding
controller to the forwarded controller.
In such cases if the Controller method to which the control is being forwarded to has the signature:
The forwarding Controller and the Controller being forwarded to should have the same HTTP METHOD
In my mext post I will share my experience with one of the interesting feature of Spring 4 i.e. Spring WEBSOCKETS
1) Controller Advice with Custom Annotation
2) Capturing Attributes in ModelAndView with the help of ModelMap while forwarding request from One Controller
to another.(Not a Spring 4 feature)
Part1:
By Using @ControllerAdvice annotation we can apply an Advice across all the controllers in an application.
Here our objective is to apply an advice not to all the controllers in an Application Context, but to them annotated
with a custom annotation @NoControllerAdviseAnnotated in this case.
The advice that would be applied out here is @ExceptionHandler(Exception.class) for Exception class.
Our Code for ControllerAdvice is as follows:
@ControllerAdvice(annotations=NoControllerAdviseAnnotated.class) public class CustomControllerAdvice { @ExceptionHandler(Exception.class) public ModelAndView handleException(Exception exception,WebRequest request) { ModelAndView mv = new ModelAndView(); mv.addObject("msg", exception.getMessage()); mv.setViewName("ExceptionLogger"); return mv; } }The code for Custom Annotation is:
@Target(value=ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) public @interface NoControllerAdviseAnnotated { }During application startup the @ControllerAdvice would be automatically registered in the Application Context
and it would apply the advice to the controller classes annotated with @NoControllerAdviseAnnotated
so for the Controller Advice to work the only thing we need to do is:
@NoControllerAdviseAnnotated @Controller public class BaseControllerNew { ......So whenever any Exceptions is being thrown from the above mentioned controller class the Controller Advice comes
into play and it catches the Exception and executes the handleException() of CustomControllerAdvice class.
Part2:
In some scenarios, some business Logic mandates us to dispatch from one controller to another, in such scenarios we
use either forward: or redirect: tools.
In such cases often there lies the need to carry some model
objects (containing data) from one controller to another for processing purposes.
When doing redirect RedirectAttributes is there to carry all the data, but during forwarding the request,
we usually put them in the request scope and do the processing, problem happens if the target controller method
has some @ModelAttribute annotated parameter and data needs to be mapped to that parameter from the forwarding
controller to the forwarded controller.
In such cases if the Controller method to which the control is being forwarded to has the signature:
@RequestMapping(value="/testFwdModAttr" , method=RequestMethod.GET) public ModelAndView test(@ModelAttribute(value="add")Address address)throws ExceptionThen the forwarding controller should have:
ModelAndView mv = new ModelAndView(); ModelMap mp = new ModelMap(); Address address = new Address(); address.setCity("KOLKATA"); address.setHouseNo(22); address.setStreetName("HARIPADA DUTTA LANE"); mp.addAttribute("add", address); mv.addAllObjects(mp); mv.setViewName("forward:/config/testFwdModAttr"); return mv;One very important thing that I need to mention out here is:
The forwarding Controller and the Controller being forwarded to should have the same HTTP METHOD
In my mext post I will share my experience with one of the interesting feature of Spring 4 i.e. Spring WEBSOCKETS
Comments
Post a Comment