I am using Spring 3 and trying to configure a simple web application using annotations to determine controller mappings. This seems to be incredibly complicated without overloading all URLs with * .form or * .do
Since part of the site must be password protected, these URLs are all protected. There is <security-constraint> in web.xml protecting everything under this root. I want to map all Spring controllers to / secure / app /.
Sample URLs: / Provisions / Application / LandingPage
/ Secure / application / edit / client / {id}
each of which I would process using the appropriate jsp / xml / whatever.
So in web.xml I have this:
<servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/secure/app/*</url-pattern> </servlet-mapping>
And in despatcher-servlet.xml I have this:
<context:component-scan base-package="controller" />
In the Controller package, I have a controller class:
package controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; @Controller @RequestMapping("/secure/app/main") public class HomePageController { public HomePageController() { } @RequestMapping(method = RequestMethod.GET) public ModelAndView getPage(HttpServletRequest request) { ModelAndView mav = new ModelAndView(); mav.setViewName("main"); return mav; } }
In the / WEB-INF / jsp section, I have "main.jsp" and a suitable resolving tool to point this out. I had something working when matching a despatcher using * .form, but can't get anything using the code above.
When you start Spring, it displays everything correctly:
13:22:36,762 INFO main annotation.DefaultAnnotationHandlerMapping:399 - Mapped URL path [/secure/app/main] onto handler [ controller.HomePageController@2a8ab08f ]
I also noticed this line, which looked suspicious:
13:25:49,578 DEBUG main servlet.DispatcherServlet:443 - No HandlerMappings found in servlet 'dispatcher': using default
And at runtime, any attempt to view / secure / app / main simply returns a 404 error in Tomcat with this log output:
13:25:53,382 DEBUG http-8080-1 servlet.DispatcherServlet:842 - DispatcherServlet with name 'dispatcher' determining Last-Modified value for [/secure/app/main] 13:25:53,383 DEBUG http-8080-1 servlet.DispatcherServlet:850 - No handler found in getLastModified 13:25:53,390 DEBUG http-8080-1 servlet.DispatcherServlet:690 - DispatcherServlet with name 'dispatcher' processing GET request for [/secure/app/main] 13:25:53,393 WARN http-8080-1 servlet.PageNotFound:962 - No mapping found for HTTP request with URI [/secure/app/main] in DispatcherServlet with name 'dispatcher' 13:25:53,393 DEBUG http-8080-1 servlet.DispatcherServlet:677 - Successfully completed request
So ... Spring displays the URL and then forgets about this mapping a second later? What's happening?
Thanks.