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?
The Javadocs for DispatcherServlet specify how the ‘viewNameTranslator’ is determined:
So if you want slightly different “view name translator” behavior, you’ll have to supply your own instance. You can probably easily just subclass
DefaultRequestToViewNameTranslatorto 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
DefaultRequestToViewNameTranslatorclass was in the framework or that a “viewNameTranslator” is something that DispatherServlet will use!)