Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

URL Mapping issue - Spring web MVC

Tags:

spring

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!

like image 949
magiclko Avatar asked Jul 27 '12 17:07

magiclko


3 Answers

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.

like image 53
JB Nizet Avatar answered Nov 11 '22 05:11

JB Nizet


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

like image 33
Victor Bashurov Avatar answered Nov 11 '22 06:11

Victor Bashurov


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>
like image 42
redochka Avatar answered Nov 11 '22 05:11

redochka