Results 1 to 9 of 9

Thread: Spring MVC URI template not working correctly

  1. #1
    Join Date
    Nov 2010
    Posts
    9

    Default Spring MVC URI template not working correctly

    In Spring MVC 3.0.5 on Tomcat 6, I can go to http://localhost:8080/myapp/browse/books and the method is resolved correctly.

    Code:
    @RequestMapping(value="/browse/{categoryName}");
    However this doesn't work:

    Code:
    @RequestMapping(value="/browse/category/{categoryName}");
    Browsing to
    Code:
    http://localhost:8080/myapp/browse/category/books
    generates a HTTP 404 from Tomcat.
    I'm sure I'm following the examples here correctly. What am I doing wrong?

    Here's the full controller method:

    Code:
    @Controller
    public class BrowseController {
        @RequestMapping(value = "/browse/category/{categoryName}", method = RequestMethod.GET)
        public String showCategory(@PathVariable("categoryName") String categoryName, Model model) {
            model.addAttribute("categoryName", categoryName);
            return "Browse";
        }
    }
    I see this message in the Tomcat output window in Netbeans 6.9:

    Code:
    Nov 14, 2010 2:02:03 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound
    WARNING: No mapping found for HTTP request with URI [/myapp/browse/category/books] in DispatcherServlet with name 'dispatcher'
    Last edited by sajee; Nov 14th, 2010 at 01:26 PM.

  2. #2
    Join Date
    Aug 2006
    Location
    Brooklyn
    Posts
    556

    Default

    The mapping looks correct to me. Enable DEBUG logging for org.springframework.web to see if you get any more information on the request. Do you have any other controllers mapped to URL patterns that may also match to the incoming URL?

  3. #3
    Join Date
    Nov 2010
    Posts
    9

    Default

    I'm new to Spring. How do I enable DEBUG?

    I only have one other controller and it's mapped to a completely different URL.

  4. #4
    Join Date
    Aug 2006
    Location
    Brooklyn
    Posts
    556

    Default

    Spring using commons logging so you just need to do what you normally do to increase logging in your app. If you use log4j as your logging framework you'd modify your log4j configuration to enable logging for the "org.springframework.web" category:

    Code:
    <logger name="org.springframework.web">
      <level value="debug" />
    </logger>

  5. #5
    Join Date
    Nov 2010
    Posts
    9

    Default

    Thank you. I'm really very new to all this but can you provide more details on setting up logging? Which file do I add this to?
    Code:
    <logger name="org.springframework.web">
      <level value="debug" />
    </logger>
    Are there any other changes I need to make?

    Once DEBUG is enabled, where do I find the log file?

    Thanks again for your help.

  6. #6
    Join Date
    Aug 2006
    Location
    Brooklyn
    Posts
    556

    Default

    You might want to check what you're using for logging by looking at the list of application dependencies. Assuming that's log4j, I suggest looking up the documentation for that logging framework.

  7. #7
    Join Date
    Nov 2010
    Posts
    9

    Default

    I figured out logging. Yay.

    So now when I make a request such as http://localhost:8080/myapp/browse/category/model

    I see in the log:
    Nov 15, 2010 9:34:28 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound
    WARNING: No mapping found for HTTP request with URI [/myapp/browse/category/model] in DispatcherServlet with name 'dispatcher'

    In case it matters, here's how my dispatcher is defined in web.xml:
    Code:
    <servlet>
            <servlet-name>dispatcher</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <load-on-startup>2</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>dispatcher</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
    Is there anything wrong with any of this? Any clues as to to why it's not working?

  8. #8
    Join Date
    Nov 2010
    Posts
    9

    Default

    Also, I see the following in the Tomcat6 log:

    Code:
    INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@b29562: defining beans [browseController,homeController,showController,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping#0,viewResolver]; parent: org.springframework.beans.factory.support.DefaultListableBeanFactory@22a6bb
    Nov 15, 2010 9:44:26 PM org.springframework.web.servlet.handler.AbstractUrlHandlerMapping registerHandler
    INFO: Mapped URL path [/browse] onto handler 'browseController'
    Nov 15, 2010 9:44:26 PM org.springframework.web.servlet.handler.AbstractUrlHandlerMapping registerHandler
    INFO: Mapped URL path [/browse/*] onto handler 'browseController'
    Nov 15, 2010 9:44:26 PM org.springframework.web.servlet.handler.AbstractUrlHandlerMapping registerHandler
    INFO: Mapped URL path [/home] onto handler 'homeController'
    Nov 15, 2010 9:44:26 PM org.springframework.web.servlet.handler.AbstractUrlHandlerMapping registerHandler
    INFO: Mapped URL path [/home/*] onto handler 'homeController'
    Nov 15, 2010 9:44:26 PM org.springframework.web.servlet.handler.AbstractUrlHandlerMapping registerHandler
    INFO: Mapped URL path [/show] onto handler 'showController'
    Nov 15, 2010 9:44:26 PM org.springframework.web.servlet.handler.AbstractUrlHandlerMapping registerHandler
    INFO: Mapped URL path [/show/*] onto handler 'showController'

  9. #9
    Join Date
    Nov 2010
    Posts
    9

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •