问题
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?
回答1:
Your web.xml
says all requests with url pattern *.html
are forwarded to Spring. Your @RequestMapping
only filters on /hello
, but the request url reaching Spring is /hello.html
. What you are missing is the .html
. Your @RequestMapping
should be /hello.html
.
After your request goes through your controller you are forwarding to a view named hello
, and the configuration in your spring-servlet.xml
resolves this to hello.jsp
in WEB-INF/jsp
, so make sure you have that as well.
Happy coding!
来源:https://stackoverflow.com/questions/9192061/no-mapping-found-for-http-request-with-uri