找不到带有URI的HTTP请求的映射? [英] No mapping found for HTTP request with URI?
问题描述
我有以下代码:
控制器:
Hi I have the following code: Controller:
@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
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>
运行此代码时,收到以下警告
警告:找不到映射在DispatcherServlet中使用名称为 spring的URI [/SpringDemo/Hello.html]进行HTTP请求。我在做什么错?
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?
推荐答案
您的 web.xml
说了所有网址格式为 *。html
的请求将转发到Spring。您的 @RequestMapping
仅对 / hello
进行过滤,但是到达Spring的请求URL为 / hello .html
。您缺少的是 .html
。您的 @RequestMapping
应该为 /hello.html
。
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
.
在您的请求通过控制器后,您将转发到名为 hello
的视图,并在 spring-servlet.xml $ c中进行配置$ c>将此解析为
WEB-INF / jsp
中的 hello.jsp
,因此请确保您也有
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.
祝您编程愉快!
这篇关于找不到带有URI的HTTP请求的映射?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!