4

I like to keep all my mapping in the same place, so I use XML config:

<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">

    <property name="mappings">
        <value>
            /video/**=videoControllerr
            /blog/**=blogController
        </value>
    </property>
    <property name="alwaysUseFullPath">
        <value>true</value>
    </property>
</bean>

If I create a second request mapping with the same name in a different controller,

@Controller
public class BlogController {
    @RequestMapping(value = "/info", method = RequestMethod.GET)
    public String info(@RequestParam("t") String type) {
        // Stuff
    }
}

@Controller
public class VideoController {
    @RequestMapping(value = "/info", method = RequestMethod.GET)
    public String info() {
        // Stuff
    }
}

I get an exception:

Caused by: java.lang.IllegalStateException: Cannot map handler 'videoController' to URL path [/info]: There is already handler of type [class com.cyc.cycbiz.controller.BlogController] mapped.

Is there a way to use the same request mappings in different controllers?

I want to have 2 urls as:

/video/info.html

/blog/info.html

Using Spring MVC 3.1.1

EDIT: I' not the only one: https://spring.io/blog/2008/03/24/using-a-hybrid-annotations-xml-approach-for-request-mapping-in-spring-mvc

The rest of the app works perfectly.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
laffuste
  • 16,287
  • 8
  • 84
  • 91
  • I don't think so, because both these request mappings are processed by two different mapping handlers/adapters. Any way can you tell me the rational behind your decision to keep the `SimpleUrlHandlerMapping`? – Arun P Johny Jun 29 '12 at 03:36
  • Just a matter of personal taste. I like to have it centralized rather than defined in each controller. I think it offers more flexibility too. – laffuste Jun 29 '12 at 07:45

2 Answers2

5

Just put a requestmapping at the level of the Controller also:

@Controller
@RequestMapping("/video")
public class VideoController {
    @RequestMapping(value = "/info", method = RequestMethod.GET)
    public String info() {
        // Stuff
    }
}

@Controller
@RequestMapping("/blog")
public class BlogController {
    @RequestMapping(value = "/info", method = RequestMethod.GET)
    public String info(@RequestParam("t") String type) {
        // Stuff
    }
}
Biju Kunjummen
  • 49,138
  • 14
  • 112
  • 125
2

You can use parameter per method mapping. See my question and answer:

Community
  • 1
  • 1
gavenkoa
  • 45,285
  • 19
  • 251
  • 303