views:

1801

answers:

2

Following the directions from Spring Source and the book Spring in Action, I am trying to set up Spring MVC in a way that minimizes xml configuration. However according to Spring Source this is how you set up the ControllerClassNameHandlerMap

<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>

<bean id="viewShoppingCart" class="x.y.z.ViewShoppingCartController">
    <!-- inject dependencies as required... -->
</bean>

Which strikes me as being completely useless, as it is actually simpler to use the handlers to just set the beans manually, as it is about the same amount of XML.

Now the book Spring in Action makes it sound like all you need is the first line from that code block to use the ControllerClassNameHandlerMapping, which would make it far more useful. However, I have not yet been able to get this to work.

Can anyone with Spring experience help me out?

+5  A: 

There are actually two different things going on here:

  1. the mapping between URLs and controllers
  2. the definition of controllers as Spring beans

For #1, if you define the ControllerClassNameHandlerMapping as you've done, that takes care of the URL-to-controller mapping. E.g., http://example.com/context/home -> HomeController

For #2, you can define the controller beans as you've done. Or you can go down the path of using the Spring 2.5-style annotations for @Controllers and auto-wiring, which eliminates the need for XML bean definitions. Or not, the choice is up to you.

What you avoid by using ControllerClassNameHandlerMapping is having to explictly map all your potential URLs to Controllers. We have used this successfully.

One other thing you might want to use is the DefaultRequestToViewNameTranslator:

<!-- Generates view names based on the request url (e.g. "/home.htm" => "home", "/user/list.htm" => "user/list", etc.) -->
<bean id="viewNameTranslator" class="org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator"/>

We also use the UrlBasedViewResolver:

<!-- Maps view names to tiles view definitions files.  E.g., "home" => "home", etc.  -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.tiles2.TilesView"/>
</bean>
Jeff Olson
Thanks for the response Jeff. What I am trying to accomplish is just #1 for now, however without the additional bean definition none of my URLs are mapping to my controllers.
James McMahon
Right, that's likely because Spring doesn't know about your controllers unless you make them Spring beans. You can do that with <bean> defs, or by using annotations.
Jeff Olson
Even with the @Controller annotation it isn't working, but I might be missing something from my configuration.
James McMahon
To use the annotations you have to use <context:annotation-config/> and <context:component-scan>...see the Spring docs for more details (caveat: I have not used these...in my projects I've declared all the beans explicitly)
Jeff Olson
A: 

I don't think using ControllerClassNameHandlerMapping is a good engineering practice, as it is really preventing you from doing refactoring work on the Controller java class.

spring user
I prefer the convention over configuration approach, it is just a matter of preference. I disagree that it prevents refactoring, you just have to keep your view and your controller names in sync, versus the configuration method, where you have to keep your view, your controller and your configuration in sync.
James McMahon