Results 1 to 3 of 3

Thread: Execute a method before any Handler in Controller

  1. #1
    Join Date
    Apr 2009
    Location
    Italy
    Posts
    37

    Default Execute a method before any Handler in Controller

    Hello everyone,

    I'm looking for a way to have a method executed before any Handler in a given Controller. Something like

    Code:
    @Controller
    @RequestMapping(value = "/{x}")
    class MyController {
    
      @PreDispatch // this is a totally invented annotation just to give you an idea
      private void aMethod(@PathVariable String x) {
        ...
      }
    
      @RequestMapping(value = "/handler")
      public String handler(@PathVariable String x, Model model) {
        ...
      }
    
    }
    So that when I go to /{x}/handler, for any {x}, I have my "PreDispatch" method invoked, do some processing on x, and than have the regular handler invoked.

    Right now, I have all my handlers that execute the "preDispatch" method as the first line of code, and I would like to have this behaviour "automatized".

    If there's no way to have this behaviour via SpringMVC probably I have to use AOP.

    Thank you very much

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    That is basically why there are HandlerInterceptors.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3

    Default

    Thanks Martin. I've been looking for the answer to a similar question and I think you've answered it for me. Cheers!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •