I am learning Spring MVC and I am blocked since a few hours in this problem, that should have an obvious resolution:
I am defining in web.xml the DispatcherServlet springSoccer and configuring it in springSoccer-servlet.xml under WEB-INF directory.
In springSoccer-servlet.xml I am configuring the ViewResolver setting up the component scan pointing to the package of my controller.
I am deploying in Tomcat 8.0 and when I point my browser to http://localhost:8080/SoccerSpringMaven3/springSoccer/users/ I am getting the error:
Nov 19, 2015 9:07:50 PM org.springframework.web.servlet.PageNotFound noHandlerFound
WARNING: No mapping found for HTTP request with URI [/SoccerSpringMaven3/springSoccer/users] in DispatcherServlet with name 'springSoccer'
It's just a problem of not finding my RequestMapping. Find below the configuration:
web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<!-- Source project: sip05, branch: 01 (Maven Project) -->
<servlet>
<servlet-name>springSoccer</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springSoccer</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
springSoccer-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<context:component-scan base-package="com.spring.soccer.controller" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/views/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<mvc:resources mapping="/resources/**" location="/resources/" />
<mvc:annotation-driven />
</beans>
SoccerController.xml
package com.spring.soccer.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class SoccerController {
@RequestMapping("/users")
public String printSoccerHome(ModelMap model) {
return "HelloSoccer";
}
}
Any pointers?. Thanks a lot.
The <servlet-name>
only defines which DispatcherServlet
handles specific requests. It doesn't work like @RequestMapping
annotation. You only configured Spring so that all requests are handled by springSoccer
DispatcherServlet
.
Read Spring MVC documentation- 21.2 The DispatcherServlet for more info.
In order for this request to work:
http://localhost:8080/SoccerSpringMaven3/springSoccer/users/
you have to either annotate your controller with @RequestMapping("/springSoccer")
or annotate your method with @RequestMapping("/springSoccer/users")