Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring MVC - Mapping Controller to URLs using Annotations

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)

like image 857
rhinds Avatar asked Aug 09 '11 17:08

rhinds


1 Answers

set

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

for urls without html and use

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

for resources like .css, .jpg etc.

like image 84
Gengzu Avatar answered Sep 30 '22 14:09

Gengzu