0

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

http://localhost:8080/app

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

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?

yardie
  • 1,583
  • 1
  • 14
  • 29
user606621
  • 493
  • 1
  • 5
  • 16

1 Answers1

0

You can do the mapping like this:

@Controller
@RequestMapping("/app/products")
public class ProductController implements Serializable {...}
Paulo Galdo Sandoval
  • 2,173
  • 6
  • 27
  • 44
  • How is it different from `@Controller("/app/products")` `public class ProductController implements Serializable {...}` – user606621 Jan 05 '16 at 15:26
  • @user606621 `@Controller("/app/products")` means you're creating a bean as controller with name "/app/products" – Wins Jan 05 '16 at 16:16