Spring MVC - Mapping Controller to URLs using Annotations

22,257

Solution 1

set

<url-pattern>/</url-pattern> 

for urls without html and use

<mvc:resources mapping="/resources/**" location="/resources/" />

for resources like .css, .jpg etc.

Solution 2

You do not need the /** in your controller mapping.

Mapping to @RequestMapping(value = "/question") will get you to this controller.

/list will be appended to /question.

When you add the /** like you have it you are telling it to look for a base path of question followed by anything and then add /list to the end.

Hope that helps.

Share:
22,257
rhinds
Author by

rhinds

Co-founder of NerdAbility.com - an online resume building site for tech professionals linking up StackOVerflow, GitHub, GoogleCode, BitBucket, GeekList, LinkedIn, Blogs, apps, and more! You can also check my blog here I'm also a food and cooking nerd, so you can check my food science site here

Updated on December 06, 2020

Comments

  • rhinds
    rhinds over 3 years

    I am having ongoing problems configuring my Spring Controller to map to specific URLs, and I have it down to a fairly simple scenario, that I think should be working:

    I am configuring my Controller class using annotations, and it looks as follows:

    @Controller
    @RequestMapping(value = "/question/**")
    public class QuestionController 
    {
    
        /**
         * Method to build users questions list
         * 
         * @param request
         * @param response
         * @return
         * @throws Exception
         */
        @RequestMapping("/list")
        public ModelAndView list(HttpServletRequest request, HttpServletResponse response) throws Exception{
            //Display Questions List
        }
    }
    

    There is no further configuration of the Controller, I simply have the <mvc:annotation-driven/> configuration and the <context:component-scan>.. configuration in my webmvc config, so the controller is automatically detected.

    Now, when I navigate to /question/list the applications fails to find the resource and I get a ResourceNotFound error. However, If I navigate to /question/question/list then the app loads the page I would expect correctly.

    Any ideas why this is directing to the method using /question/question/list?


    Following this I tried adding the configuration to my webmvc config to force all RequestMappings to use the parameters alwaysUseFullPath=true, I did this as follows:

    <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" p:order="3">
            <property name="alwaysUseFullPath" value="true" />
        </bean>
    

    This time, when I navigate to /question/list it still fails to load the correct page, but the logs show that Spring is at least identifying the correct Controller, but just failing to find the method (before it did not even find the Controller based on the URL):

    2011-08-09 18:02:27,885 [http-8080-3] DEBUG org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping - Matching patterns for request [/question/list] are [/question/**/list/, /question/**/list, /question/**/, /question/**]
    2011-08-09 18:02:27,886 [http-8080-3] DEBUG org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping - Mapping [/question/list] to handler 'com.tmm.enterprise.microblog.controller.QuestionController@143c423'
    2011-08-09 18:02:27,886 [http-8080-3] DEBUG org.springframework.web.servlet.DispatcherServlet - Last-Modified value for [/microblog/question/list] is: -1
    2011-08-09 18:02:27,886 [http-8080-3] DEBUG org.springframework.web.servlet.DispatcherServlet - DispatcherServlet with name 'microblog' processing GET request for [/microblog/question/list]
    2011-08-09 18:02:27,886 [http-8080-3] DEBUG org.springframework.web.servlet.handler.SimpleMappingExceptionResolver - Resolving exception from handler [com.tmm.enterprise.microblog.controller.QuestionController@143c423]: org.springframework.web.servlet.mvc.multiaction.NoSuchRequestHandlingMethodException: No matching handler method found for servlet request: path '/list', method 'GET', parameters map[[empty]]
    2011-08-09 18:02:27,886 [http-8080-3] DEBUG org.springframework.web.servlet.handler.SimpleMappingExceptionResolver - Resolving to view 'resourceNotFound' for exception of type [org.springframework.web.servlet.mvc.multiaction.NoSuchRequestHandlingMethodException], based on exception mapping [.NoSuchRequestHandlingMethodException]
    2011-08-09 18:02:27,887 [http-8080-3] DEBUG org.springframework.web.servlet.handler.SimpleMappingExceptionResolver - Exposing Exception as model attribute 'exception'
    

    It seems to me as though it is a relatively simple thing I am trying to achieve in wiring a Controller to a URL using the annotations, but it is not working correctly - has any one come across this or see any glaring errors on my part?


    UPDATE

    I have made some progress in my investigations.

    In my web.xml I define the servlet mapping as follows:

    <servlet-mapping>
            <servlet-name>microblog</servlet-name>
            <url-pattern>/question/*</url-pattern>
        </servlet-mapping>
    

    If I remove this servlet mapping, (I still have a servlet-mapping that maps all .html to the same servlet) and change the URL i am using to /question/list.html then it works (also I change the method level mapping of the @RequestMapping annotation on my list() method in the question controller to /list.html).

    In summary:

    1. I have servlet mapping /question to the web context

    2. I have another mapping of /question to the QuestionController

    3. I have a method level mapping for my list method of /list

    Now I don't want my URLs to end to .html for these cases - does anyone know how I can get around this problem? It seems as though maybe the servlet-mapping strips out the matched /question string from the URL (hence /question/list not working, but /question/question/list working)

    • Gengzu
      Gengzu over 12 years
      set <url-pattern>/</url-pattern> for urls without html
    • rhinds
      rhinds over 12 years
      If i use that then it re-directs all my image, css resources etc to the controller so fails to find them (all my images are in /images/.. etc) - so when i load the page there are no images or css styling. Any ideas how I can map just the URLs to the controller without needing to duplicate the path in the URL?
    • Gengzu
      Gengzu over 12 years
      Move your files to the folder above, not in WEB-INF folder.
    • rhinds
      rhinds over 12 years
      apologies, i was incorrect - my images, css etc are already at the directory level above WEB-INF. Any ideas why when I add <url-pattern>/</url-pattern> and navigate to the page no styling/images appear?
    • Gengzu
      Gengzu over 12 years
      oh, use <mvc:resources mapping="/resources/**" location="/resources/" />
    • rhinds
      rhinds over 12 years
      perfect, those things did the job. If you put it in an answer i will accept it
  • rhinds
    rhinds over 12 years
    Thanks, I was already doing this, but in desperation I tried adding the "/**" in an attempt to mirror the way it would have been configured in the old XML config. In both cases and i still get the same results either way.
  • rhinds
    rhinds over 12 years
    I have also updated the original question with some futher updates regarding some discoveries I have made to the web.xml servlet-mapping