views:

234

answers:

1

I'm trying out the whole "convention over configuration" thing with Spring MVC. Spring has all sorts of tools to help with this, and I'm trying some of them out. However, I ran into a problem with our team's configuration not quite matching what Spring wants.

The problem is that we take URLs like "http://ourSite/SomePage.do", put them through "SomePageController", and render them with "somePage.jsp". Note that SomePage.do begins with a capital letter, and somePage.jsp does not. It appears that Spring's DefaultRequestToViewNameTranslator is keeping the capital letter around, and the ViewResolvers can't figure out how to make that first character lowercase.

Is there a way to configure either the NameTranslator or a ViewResolver to make that first letter lowercase?

+1  A: 

The Javadocs for DispatcherServlet specify how the 'viewNameTranslator' is determined:

If a View or view name is not supplied by the user, then the configured RequestToViewNameTranslator will translate the current request into a view name. The corresponding bean name is "viewNameTranslator"; the default is DefaultRequestToViewNameTranslator.

So if you want slightly different "view name translator" behavior, you'll have to supply your own instance. You can probably easily just subclass DefaultRequestToViewNameTranslator to add logic about the case of the filename.

But to be honest, I'd really suggest that your controllers returned named views and not have to rely on the viewname being the same as the URI. This lets you use the same view for multiple URLs/controllers, and allows you to truly abstract away the concept of the "view" from the URL.

(To be totally honest, I've been using Spring MVC for about 5 months on a fairly large app and didn't even know that this DefaultRequestToViewNameTranslator class was in the framework or that a "viewNameTranslator" is something that DispatherServlet will use!)

matt b