16

I'm a newbie with Spring and web MVC module. Basically, i have the following :

web.xml

<servlet>
    <servlet-name>abc-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>abc-dispatcher</servlet-name>
    <url-pattern>/user/*</url-pattern>
</servlet-mapping>

abc-dispatcher-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:context="http://www.springframework.org/schema/context"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">


<context:component-scan base-package="myPkg" />


<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
    <property name="prefix" value="/WEB-INF/pages/"></property>
    <property name="suffix" value=".jsp"></property>        
</bean>

And i have a controller, related parts are :

@Controller
public class ABCController {

@RequestMapping("/user/welcome")
public String printWelcome(ModelMap model) {

    //code

}

Now whenever i try to access http://localhost:8080/myapp/user/welcome

it gives me 404.

Logs say that "mapped url '/user/welcome' onto handler 'ABCController' but it failed to map URI [/MYAPP/user/welcome] in DispatcherServlet with name 'abc-dispatcher' .

Totally confused. I have checked all the threads where we specify a mapping twice, but that's not the case here. I must be missing something!

Thanks for the help!

Don Branson
  • 13,631
  • 10
  • 59
  • 101
magiclko
  • 283
  • 2
  • 5
  • 12

3 Answers3

17

The URL should be http://localhost:8080/myapp/user/user/welcome. Indeed, unless the alwaysUseFullPath property of the handler is set to true, the servlet-mapping is prepended to the request mapping URL to form the full path.

See http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/htmlsingle/spring-framework-reference.html#mvc-handlermapping for details:

alwaysUseFullPath

If true , Spring uses the full path within the current Servlet context to find an appropriate handler. If false (the default), the path within the current Servlet mapping is used. For example, if a Servlet is mapped using /testing/* and the alwaysUseFullPath property is set to true, /testing/viewPage.html is used, whereas if the property is set to false, /viewPage.html is used.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Thanks.. that worked :) One follow-up question if that's ok.. I am including static resources like css and js in jsp's but the url gives 404. Link results in /user/welcome/resources/css/abc.css when i want the url to look like /resources/css/abc.css .. Any pointers on that? – magiclko Aug 03 '12 at 11:52
  • Ask another question, and tell what the context path is, where the resources are stored, and the code used to generate the URLs. – JB Nizet Aug 03 '12 at 13:48
5

It's' been added context:component-scan element into the sample context file snippet but there is no <annotation-driven/> element that says spring framework to look for controllers annotated with @Controller

The Hungry Dictator
  • 3,444
  • 5
  • 37
  • 53
Victor Bashurov
  • 370
  • 3
  • 12
3

For me, the problem was that I was using the deprecated DefaultAnnotationHandlerMapping, and even if setting the alwaysUseFullPath to true, it didn't take effect, however replacing DefaultAnnotationHandlerMapping in benefit of RequestMappingHandlerMapping with the alwaysUseFullPath set to true solved the problem.

<bean id="handlerMapping" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
    <property name="alwaysUseFullPath" value="true"></property>
</bean>
redochka
  • 12,345
  • 14
  • 66
  • 79