12 Replies Latest reply on Jan 28, 2019 10:42 PM by jewel14878210.eed02

    spring mvc dispatcher servlet mapping

    tramwai

      Hello i have a trouble with spring mvc in jboss 7.1.1.Final. HomeController returns simple hello world jsp page.

       

       

      public class WebConfig implements WebApplicationInitializer {

                    @Override

                    public void onStartup(ServletContext servletContext) throws ServletException {

                        AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();

                        rootContext.register(ApplicationContext.class);

                        ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", new DispatcherServlet(rootContext));

                        dispatcher.setLoadOnStartup(1);

                        dispatcher.addMapping("/");

                        servletContext.addListener(new ContextLoaderListener(rootContext));

                    }

       

      Controller:

       

      @Controller

      public class HomeController {

          protected static final String INDEX_VIEW = "index";

          @RequestMapping(value = "/", method = RequestMethod.GET)

          public String showPage() {

              return INDEX_VIEW;

          }

      }

       

       

      jboss-web.xml:

       

      <jboss-web>

          <context-root>/</context-root>

      </jboss-web>

       

       

      accessing localhost:8080 results in 404. I've set jboss logging to debug, and i don't see "dispatcher proccessing get request" kinda thing. Does this mean servlet is not mapped? Everything works fine on tomcat 7 and jetty 8.

        • 1. Re: spring mvc dispatcher servlet mapping
          nickarls

          And you have disabled the standard / context app and your app deployes fine?

          • 2. Re: spring mvc dispatcher servlet mapping
            tramwai

            you mean this

            <virtual-server name="default-host" enable-welcome-root="false">

            ?

            Seams fine, i don't see any exceptions

            • 3. Re: spring mvc dispatcher servlet mapping
              nickarls

              And you see WebApplicationInitializer being called?

               

              (just guessing here, I'm not really a Spring-user)...

              • 4. Re: spring mvc dispatcher servlet mapping
                tramwai

                when deploying i see :

                (MSC service thread 1-1) Spring WebApplicationInitializers detected on classpath: [me.myself.resttest.config.WebConfig@195d568]

                 

                but i dont see calls to dispatcher servlet when accessing my app through browser

                • 5. Re: spring mvc dispatcher servlet mapping
                  tramwai

                  Does jboss 7 support web.xml-less web-applications? Because i tried web.xml equal to my WebConfig and everything worked. Isnt this standart servlet 3.0 stuff?

                  I also downloaded nightly build and i'm having same problem there. Help me out please.

                  • 6. Re: spring mvc dispatcher servlet mapping
                    nickarls

                    Yes, I think it should work since Servlet 3.0 made web.xml optional

                    • 7. Re: spring mvc dispatcher servlet mapping
                      tramwai

                      Nicklas, is there any public source code that doesn't use web.xml (utilizes spring mvc preferably) and works on jboss 7?

                      • 8. Re: spring mvc dispatcher servlet mapping
                        nickarls

                        not sure about the spring-thing but if you make a foo.war with a META-INF/classes and a

                         

                        @WebServlet(urlPatterns="/test")

                        public class Test extends HttpServlet

                        {

                          @Override

                           protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException

                           {

                              resp.getWriter().println("1-2-3 testing");

                           }

                        }

                         

                        you should be able to access it

                        • 9. Re: spring mvc dispatcher servlet mapping
                          xdury

                          I've got the same problem, I'm using Spring's AbstractAnnotationConfigDispatcherServletInitializer which should take care of registering the DispatcherServlet dynamically (not in web.xml).

                           

                          I can see that DispatcherServlet is registered but anything that should be rendered via this servlet results in 404. Only static resources are available.

                           

                          16:21:53,634 INFO  [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/project]] (ServerService Thread Pool -- 68) Spring WebApplicationInitializers detected on classpath: [my.company.project.MyInitializer@61bc4984]

                          16:21:53,783 INFO  [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/project]] (ServerService Thread Pool -- 68) Initializing Spring FrameworkServlet 'dispatcher'

                          ... spring blah blah blah ...

                          16:21:54,896 INFO  [org.springframework.web.servlet.DispatcherServlet] (ServerService Thread Pool -- 68) FrameworkServlet 'dispatcher': initialization completed in 1111 ms

                          16:21:55,059 INFO  [org.jboss.as.server] (DeploymentScanner-threads - 1) JBAS018565: Replaced deployment "project.war" with deployment "project.war"

                           

                          This is really strange because the same application runs fine in Tomcat 7 and Jetty 8,9...

                           

                          Xavier

                          • 10. Re: spring mvc dispatcher servlet mapping
                            ctomc

                            what is spring version?

                            • 11. Re: spring mvc dispatcher servlet mapping
                              vidmantas.banaitis

                              Hi, all.

                               

                              Had the same problem minutes ago. What resolved was changing dispathcer mapping from "/" to "/*"

                               

                              public class WebConfig implements WebApplicationInitializer {

                                            @Override

                                            public void onStartup(ServletContext servletContext) throws ServletException {

                                                AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();

                                                rootContext.register(ApplicationContext.class);

                                                ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", new DispatcherServlet(rootContext));

                                                dispatcher.setLoadOnStartup(1);

                                                dispatcher.addMapping("/*");

                                                servletContext.addListener(new ContextLoaderListener(rootContext));

                                            }

                                  }

                              }

                               

                               

                              Hope this helps;

                               

                              Vidmantas

                              1 of 1 people found this helpful
                              • 12. Re: spring mvc dispatcher servlet mapping
                                jewel14878210.eed02

                                The problem puzzled me for few days and this solution totally resolve it. Thank you!