Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Needing help to externalize url mappings for @RequestMapping

  1. #1
    Join Date
    Jun 2010
    Posts
    16

    Default Needing help to externalize url mappings for @RequestMapping

    I'm creating an email form that will be used in multiple applications and multiple places in the same application on different url patterns. Ideally I would like to create it as an @MVC controller that is autowired and automatically starts itself up via a component scan when the servlet container starts.

    Here is my problem. I need the @RequestMapping(value={"/url1","url2","..."}) to be configurable outside the java in a xml file (sort of the way you use to be able to do using the SimpleFormController and SimpleUrlHanderMapping). I need to be able to inject my url mappings so that each application can have a completely separate set of url mappings. Can someone tell me if this is possible?

    I could use SimpleFormController to accomplish this (minus the component scan) but I really wanted to use @MVC because I was told that the old controller hierarchy is or will soon be deprecated.

  2. #2
    Join Date
    Mar 2010
    Location
    Boston, MA
    Posts
    316

    Default

    AFAIK ..i dont think its possible out of the box. Maybe your email functionality should be a service and not a controller? This way you can let consumers define the controllers.

  3. #3
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    Actually it is available..

    Simple mark your method with @RequestMapping and use a SimpleUrlHandlerMapping to explicitly configure the urls which are handled by this controller. That should basically do it.
    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

  4. #4
    Join Date
    Jun 2010
    Posts
    16

    Default

    You're right, I could and probably should create a service that handles all the email functionality but I was also trying to create a component to duplicate current functionality we have with a struts email form.

    Am I correct that the old controllers have been deprecated? I'm trying to figure out what my options are...I obviously don't want to code something that won't work in the next version of spring.

  5. #5
    Join Date
    Mar 2010
    Location
    Boston, MA
    Posts
    316

    Default

    Quote Originally Posted by Marten Deinum View Post
    Actually it is available..

    Simple mark your method with @RequestMapping and use a SimpleUrlHandlerMapping to explicitly configure the urls which are handled by this controller. That should basically do it.
    I thought the OP wanted to pull URLs from a properties file and use it in his annotated controllers something like

    Code:
    @RequestMapping(pullvaluefrompropertyfile)
    @Controller
    Maybe i am mistaken?

  6. #6
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    He wants to externalize configuration, which you can do by explicitly configure the urls and create a single @RequestMapping method. If he wants to stay with properties files and @RequestMapping that isn't going to work...
    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

  7. #7
    Join Date
    Jun 2010
    Posts
    16

    Default

    Quote Originally Posted by Marten Deinum View Post
    Actually it is available..

    Simple mark your method with @RequestMapping and use a SimpleUrlHandlerMapping to explicitly configure the urls which are handled by this controller. That should basically do it.
    So can my controller only have two methods - one marked as @RequestMapping(method=RequestMethod.GET) and one marked @RequestMapping(method=RequestMethod.POST)? Otherwise how does it know which url calls with method in your controller?

  8. #8
    Join Date
    Jun 2010
    Posts
    16

    Default

    Quote Originally Posted by Marten Deinum View Post
    He wants to externalize configuration, which you can do by explicitly configure the urls and create a single @RequestMapping method. If he wants to stay with properties files and @RequestMapping that isn't going to work...
    I don't need it in a properties file...the SimpleUrlHandlerMapping works just fine. I believe I have it working...I'm just not sure why it works

  9. #9
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    So can my controller only have two methods - one marked as @RequestMapping(method=RequestMethod.GET) and one marked @RequestMapping(method=RequestMethod.POST)? Otherwise how does it know which url calls with method in your controller?
    If there is only one with only @RequestMapping then that method will always be called, if you want to make it a bit more flexible you could add one for a GET and a POST.

    I don't need it in a properties file...the SimpleUrlHandlerMapping works just fine. I believe I have it working...I'm just not sure why it works
    What is it you doubt? It is simply a Handler and you can use any means of HandlerMapping to map a URL to a Handler. Execution is done by a HandlerAdapter which knows how to handle a Controller (SimpleControllerHandlerAdapter) or a @Controller (DefaultAnnotationHandlerAdapter). So the mapping and execution of a handler are 2 different things and shouldn't be confused.
    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

  10. #10
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    So can my controller only have two methods - one marked as @RequestMapping(method=RequestMethod.GET) and one marked @RequestMapping(method=RequestMethod.POST)? Otherwise how does it know which url calls with method in your controller?
    If there is only one with only @RequestMapping then that method will always be called, if you want to make it a bit more flexible you could add one for a GET and a POST.

    I don't need it in a properties file...the SimpleUrlHandlerMapping works just fine. I believe I have it working...I'm just not sure why it works
    What is it you doubt? It is simply a Handler and you can use any means of HandlerMapping to map a URL to a Handler. Execution is done by a HandlerAdapter which knows how to handle a Controller (SimpleControllerHandlerAdapter) or a @Controller (DefaultAnnotationHandlerAdapter). So the mapping and execution of a handler are 2 different things and shouldn't be confused.
    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

Posting Permissions

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