1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > Spring Mvc返回html页面404错误解决记录--转载

Spring Mvc返回html页面404错误解决记录--转载

时间:2020-11-03 18:02:01

相关推荐

Spring Mvc返回html页面404错误解决记录--转载

原文地址:/blog/2061992

以前使用Spring Mvc时候都是返回jsp页面或者ftl页面,昨天想返回html页面,spring-mvc.xml配置如下:

Xml代码 <beanid="viewResolver"class="org.springframework.web.servlet.view.InternalResourceViewResolver"p:prefix="/WEB-INF/html/"p:suffix=".html"/>

Controller方法如下:

Java代码 @RequestMapping(value="/add",method=RequestMethod.GET)publicStringtoAddTest(){return"addTest";}

在tomcat下测试,页面一直是404,log日志如下:

Java代码 [06/05/1410:44:35:035GMT+08:00]DEBUGsupport.DefaultListableBeanFactory:Returningcachedinstanceofsingletonbean'sqlSessionFactory'[06/05/1410:44:35:035GMT+08:00]DEBUGservlet.DispatcherServlet:Successfullycompletedrequest[06/05/1410:44:38:038GMT+08:00]DEBUGservlet.DispatcherServlet:DispatcherServletwithname'spring'processingGETrequestfor[/MyTest/test/add][06/05/1410:44:38:038GMT+08:00]DEBUGannotation.RequestMappingHandlerMapping:Lookinguphandlermethodforpath/test/add[06/05/1410:44:38:038GMT+08:00]DEBUGannotation.RequestMappingHandlerMapping:Returninghandlermethod[publicjava.lang.Stringcom.report.controller.testController.toaddTest()][06/05/1410:44:38:038GMT+08:00]DEBUGsupport.DefaultListableBeanFactory:Returningcachedinstanceofsingletonbean'testController'[06/05/1410:44:38:038GMT+08:00]DEBUGservlet.DispatcherServlet:Last-Modifiedvaluefor[/MyTest/test/add]is:-1[06/05/1410:44:38:038GMT+08:00]DEBUGservlet.DispatcherServlet:Renderingview[org.springframework.web.servlet.view.JstlView:name'addTest';URL[/WEB-INF/html/addTest.html]]inDispatcherServletwithname'spring'[06/05/1410:44:38:038GMT+08:00]DEBUGview.JstlView:Forwardingtoresource[/WEB-INF/html/addTest.html]inInternalResourceView'addTest'[06/05/1410:44:38:038GMT+08:00]DEBUGservlet.DispatcherServlet:DispatcherServletwithname'spring'processingGETrequestfor[/MyTest/WEB-INF/html/addTest.html][06/05/1410:44:38:038GMT+08:00]DEBUGannotation.RequestMappingHandlerMapping:Lookinguphandlermethodforpath/WEB-INF/html/addTest.html[06/05/1410:44:38:038GMT+08:00]DEBUGannotation.RequestMappingHandlerMapping:Didnotfindhandlermethodfor[/WEB-INF/html/addTest.html][06/05/1410:44:38:038GMT+08:00]WARNservlet.PageNotFound:NomappingfoundforHTTPrequestwithURI[/MyTest/WEB-INF/html/addTest.html]inDispatcherServletwithname'spring'[06/05/1410:44:38:038GMT+08:00]DEBUGservlet.DispatcherServlet:Successfullycompletedrequest[06/05/1410:44:38:038GMT+08:00]DEBUGservlet.DispatcherServlet:Successfullycompletedrequest

可以看出No mapping found for HTTP request with URI错误导致了404,问题原因:

参考了/questions/13616821/make-html-default-view-spring-mvc

写道 1 First the DispatcherServlet is invoked by the Servlet Container.

2 The DispatcherServlet finds a mapping which maps to the home method of your Controller and the home method returns a view name "HelloWorld"

3 Now the DispatcherServlet uses a View Resolver (your InternalResourceViewResolver) to find the View to render the model through, since the name is "HelloWorld", this maps to the /WEB-INF/view/HelloWorld.html view.

4 Now essentially a call is made to RequestDispatcher.forward("/WEB-INF/views/HelloWorld.html",....

5 The Servlet container at this point tries to find the servlet which can handle /WEB-INF/views/HellowWorld.html uri - if it had been a .jsp there is a JSPServlet registered which can handle rendering the jsp, however for *.html there is no servlet registered, so the call ends up with the "default servlet", which is registered with a servlet-mapping of / which probably your DispatcherServlet is.

6 Now the Dispatcher servlet does not find a controller to handle request for /WEB-INF/views/HelloWorld.html and hence the message that you are seeing

解决方法:

/questions/4249622/using-html-files-as-jsps

写道 Add this servletmapping for the JSP servlet(web.xml):

<servlet-mapping>

<servlet-name>jsp</servlet-name>

<url-pattern>*.html</url-pattern>

</servlet-mapping>

再次访问就OK了。

全文完。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。