Hi I have the following code:
Controller:
@Controller
public class HelloWorldController {
@RequestMapping("/hello")
public ModelAndView HelloWorld() {
String message = "My First SpringMVC Program ";
return new ModelAndView("hello","message",message);
}
web.xml
<servlet>
<!-- load on startup is used to determine the order of initializing the servlet when the application
server starts up. The lower the number, earlier it starts -->
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
spring-servlet.xml
<context:component-scan
base-package="org.example.controller"/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView">
</property>
<property name="prefix" value="/WEB-INF/jsp/">
</property>
<property name="suffix" value=".jsp"></property>
When I run this code, I get the following warning
“WARNING: No mapping found for HTTP request with URI [/SpringDemo/Hello.html] in DispatcherServlet with name ‘spring'”. What wrong am I doing?
Your
web.xmlsays all requests with url pattern*.htmlare forwarded to Spring. Your@RequestMappingonly filters on/hello, but the request url reaching Spring is/hello.html. What you are missing is the.html. Your@RequestMappingshould be/hello.html.After your request goes through your controller you are forwarding to a view named
hello, and the configuration in yourspring-servlet.xmlresolves this tohello.jspinWEB-INF/jsp, so make sure you have that as well.Happy coding!