sens2010 / run-jetty-run

Automatically exported from code.google.com/p/run-jetty-run
0 stars 0 forks source link

Running Spring MVC app with WebApplicationInitializer instead of web.xml does not work #184

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What is the expected output? What do you see instead?

When using XML-based configuration (web.xml), controller methods work.

When using Java-based configuration (WebApplicationInitializer) for dynamic web 
app, controller methods are not executed.

Controller method breakpoints are not reached. Controller method mapped to root 
(http://localhost:8080/) returns directory listing (Directory: / ... WEB-INF/, 
resources/). Other controller methods (e.g., http://localhost:8080/users) 
return 404 Not Found.

I have tried both XmlWebApplicationContext (using original XML Spring MVC 
configuration) AnnotationConfigWebApplicationContext (Java-based Spring MVC 
configuration).

Request mappings show up in log correctly.

What steps will reproduce the problem?

1. Use WebApplicationInitializer instead of web.xml
2. Run/Debug using Run-Jetty-Run
3. Request URI in browser (http://localhost:8080/)
4. Breakpoint in controller not reached
5. Browser displays directory listing
6. Request URI in browser (http://localhost:8080/users)
7. Breakpoint in controller not reached
8. Browser displays 404 Not Found

What Eclipse version are you using ?

Kepler R2

What Run-Jetty-Run version are you using?

1.3.3

What OS are you using ? 32bit or 64 bit?

Max OS X 64 bit

Please provide any additional information below.

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app
  version="3.0"
  xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  <!-- Define spring container -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:/spring/root-context.xml</param-value>
  </context-param>

  <!-- Create spring container -->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <!-- Define servlet -->
  <servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:/spring/appServlet/servlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <!-- Map context root -->
  <servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

CustomWebApplicationInitializer (using XmlWebApplicationContext):

public class CustomWebApplicationInitializer implements 
WebApplicationInitializer {
    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        org.apache.log4j.BasicConfigurator.configure();

        XmlWebApplicationContext rootContext = new XmlWebApplicationContext();
        rootContext.setConfigLocation("classpath:/spring/root-context.xml");
        servletContext.addListener(new ContextLoaderListener(rootContext));

        ServletRegistration.Dynamic appServlet = servletContext.addServlet("appServlet", DispatcherServlet.class);
        appServlet.setInitParameter("contextConfigLocation", "classpath:/spring/appServlet/servlet-context.xml");
        appServlet.addMapping("/");
        appServlet.setLoadOnStartup(1);
    }
}

CustomWebApplicationInitializer (using AnnotationConfigWebApplicationContext):

public class CustomWebApplicationInitializer implements 
WebApplicationInitializer {
    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        org.apache.log4j.BasicConfigurator.configure();

        AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
        rootContext.register(RootContextConfig.class);
        servletContext.addListener(new ContextLoaderListener(rootContext));

        AnnotationConfigWebApplicationContext appServletContext = new AnnotationConfigWebApplicationContext();
        appServletContext.register(AppServletContextConfig.class);
        ServletRegistration.Dynamic appServlet = servletContext.addServlet("appServlet", new DispatcherServlet(appServletContext));
        appServlet.addMapping("/");
        appServlet.setLoadOnStartup(1);
    }
}

Controllers:

@Controller
public class PagesController {
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public @ResponseBody String home() {
        return "PagesController/home";
    }
}

@Controller
public class UsersController {
    @RequestMapping(value = "/users", method = RequestMethod.GET)
    public @ResponseBody String index() {
        return "UsersController/index";
    }
}

Log excerpt showing request mappings:

566 [main] INFO 
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMappi
ng  - Mapped 
"{[/],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" 
onto public java.lang.String apps.app_0.PagesController.home()
565 [main] INFO 
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMappi
ng  - Mapped 
"{[/users],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}
" onto public java.lang.String apps.app_0.UsersController.index()

Original issue reported on code.google.com by Alexande...@gmail.com on 12 May 2014 at 4:50