We have a Spring MVC app with controllers:
@Controller("/app")
@Controller("/app/page1")
@Controller("/app/page2")
And we have AppInitializer:
public class AppInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
WebApplicationContext context = getContext();
DispatcherServlet servlet = new DispatcherServlet(context);
ServletRegistration.Dynamic dispatcher = servletContext.addServlet("DispatcherServlet1", servlet);
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/app/*");
}
private AnnotationConfigWebApplicationContext getContext() {
... prepare app context
}
}
When running this app, we are able to access
But
http://localhost:8080/app/page1 results in error
WARNING: No mapping found for HTTP request with URI [/app/page1] in DispatcherServlet with name 'DispatcherServlet1'
and
http://localhost:8080/app/page2 results in error
WARNING: No mapping found for HTTP request with URI [/app/page2] in DispatcherServlet with name 'DispatcherServlet1'
We can fix this by adding strict mapping to DispatcherServlet like this
DispatcherServlet servlet = new DispatcherServlet(context);
ServletRegistration.Dynamic dispatcher = servletContext.addServlet("DispatcherServlet1", servlet);
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/app", "/app/page1", "/app/page2");
and all pages work fine:
http://localhost:8080/app/page1
http://localhost:8080/app/page2
But the problem is that we need to add mapping to DispatcherServlet every time we add new page. For example to add page 3 we would need to create Controller
@Controller("/app/page3")
and
add dispatcher servlet mapping dispatcher.addMapping("/app", "/app/page1", "/app/page2", **"/app/page3"**);
We would really like to avoid this last step.
dispatcher.addMapping("/app/*");
would be ideal, but as I mentioned above it is not working.
Any idea what we missing?