|
This doesn't seem to work like I expect it to. Here's what I'd expect: 1. Add @Controller scanning to *-servlet.xml: <context:component-scan base-package="org.appfuse.web.controller"/> 2. Create a class with @Controller annotation: package org.appfuse.web.controller; import org.springframework.stereotype.Controller; @Controller @RequestMapping(method = RequestMethod.GET) 3. This Controller is available at /home.html. The only way I can get #3 to work is if I add @RequestMapping("/home") to the class. I thought this wouldn't be required anymore? I tried adding the following to my servlet.xml, but it didn't help either: <bean id="urlMapping" class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/> Thanks, Matt Any response to Matt's comments? I upgraded from 2.5.2 and had to revert back because it didn't work as expected and like Matt I thought the ControllerClassNameHandlerMapping would determine the request mapping path. In Matt's example the index method on HomeController would be mapped to the URL /home/index by the 2.5.3 convention, which I imagine isn't what he wants. Just to provide more usage examples of this feature, here is an example in the Spring MVC + Web Flow reference application "booking-mvc". You can see the code on-line here: https://springframework.svn.sourceforge.net/svnroot/springframework/spring-webflow/trunk/spring-webflow-samples/booking-mvc Specifically see: The URls mapped by convention are then: /hotels/index But no, /hotels doesn't mapped to to the index method by default... not in 2.5.3 anyway. Is this the convention you want? Perhaps we could employ a convention to map the root controller URL e.g. /home to a "home" method on the HomeController... Thanks Keith - your webmvc-config.xml seems to contain the magic sauce that makes this work as expected. Adding the following allows /home/index.html to work: <!-- Enables convention-based request URL mapping to @Controllers e.g. /home/* maps to HomeController --> Additionally adding the URL mapping bean below allows /home.html to work: <!-- Maps all other request URLs to views --> Is it possible to make these the defaults or would that mess up too much backwards compatibility? The next thing I'm going to ask for is to eliminate @Controller and detect class names that end with Controller. Thanks, Matt BTW, is there anything being done to allow extensionless URLs? I'd love to have /home/index or home instead of /spring/home or /home.html. I realize I can use UrlRewriteFilter, but it seems like I'd have to add entries for each Controller's URL to my urlrewrite.xml. For what it's worth, I finally just got back to this issue and solved it by extending ControllerClassNameHandlerMapping with my own isMultiActionControllerType() method. Mine has an extra check to see if the controller has the @Controller annotation but does NOT have any @RequestMapping annotations. This is how must of the controllers in our application are setup (@Controller + ControllerClassNameHandlerMapping without @RequestMapping) and it seems to work OK. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
As of Spring 2.5.3, ControllerClassNameHandlerMapping detects @Controller beans by default as well, treating them analogous to MultiActionControllers. Thanks for the suggestion!
This will be available in the next 2.5.3 snapshot (http://static.springframework.org/downloads/nightly/snapshot-download.php?project=SPR
). Feel free to give it a try...
Juergen