找不到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错误.
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屋!