更新时间:2022-04-02 07:49:02
配置了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
.