未找到带有 URI Spring MVC 的 HTTP 请求的映射 [英] No mapping found for HTTP request with URI Spring MVC
问题描述
这是我的 Web.xml
Here's my Web.xml
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/servlet-context.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
我的 servlet-context.xml
my servlet-context.xml
<context:component-scan base-package="com.springexample.controller.impl" />
<mvc:annotation-driven />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/views/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
最后是Handler类.这是在 com.springexample.controller.impl 下
And lastly the Handler class. which is under com.springexample.controller.impl
@Controller
public class IndexControllerImpl implements IndexController {
@RequestMapping("/")
public String index() {
return "index";
}
}
但是去localhost:8080/projectname/
它返回一个 404 错误.
it returns a 404 error.
Jul 27, 2013 8:18:31 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/tasklist/WEB-INF/views/index.jsp] in DispatcherServlet with name 'dispatcherServlet'
Jul 27, 2013 8:18:37 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/tasklist/index] in DispatcherServlet with name '
这是我的项目结构
推荐答案
使用 web.xml 配置他们在问题中的方式,特别是:
With the web.xml configured they way you have in the question, in particular:
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
对您的 Web 应用程序发出的所有请求都将被定向到 DispatcherServlet
.这包括像 /tasklist/
、/tasklist/some-thing.html
、/tasklist/WEB-INF/views/index.jsp
这样的请求.
ALL requests being made to your web app will be directed to the DispatcherServlet
. This includes requests like /tasklist/
, /tasklist/some-thing.html
, /tasklist/WEB-INF/views/index.jsp
.
因此,当您的控制器返回一个指向 .jsp
的视图时,而不是允许您的服务器容器为请求提供服务,DispatcherServlet
会跳入并开始寻找可以为这个请求提供服务的控制器,它没有找到任何,因此 404
.
Because of this, when your controller returns a view that points to a .jsp
, instead of allowing your server container to service the request, the DispatcherServlet
jumps in and starts looking for a controller that can service this request, it doesn't find any and hence the 404
.
最简单的解决方法是让你的servlet url映射如下:
The simplest way to solve is to have your servlet url mapping as follows:
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
注意缺少的*
.这告诉容器任何没有 path info
的请求(url 末尾没有 .xxx),应该被发送到 DispatcherServlet
.使用此配置,当接收到 xxx.jsp
请求时,不会查询 DispatcherServlet
,并且您的 servlet 容器的默认 servlet 将服务该请求并按预期呈现 jsp.
Notice the missing *
. This tells the container that any request that does not have a path info
in it (urls without a .xxx at the end), should be sent to the DispatcherServlet
. With this configuration, when a xxx.jsp
request is received, the DispatcherServlet
is not consulted, and your servlet container's default servlet will service the request and present the jsp as expected.
希望这会有所帮助,我意识到您之前的评论指出问题已解决,但解决方案不能只是将 method=RequestMethod.GET
添加到 RequestMethod
.
Hope this helps, I realize your earlier comments state that the problem has been resolved, but the solution CAN NOT be just adding method=RequestMethod.GET
to the RequestMethod
.
这篇关于未找到带有 URI Spring MVC 的 HTTP 请求的映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!