当前位置:首页 > 开发 > 编程语言 > Spring > 正文

[简单]Spring Mvc返回html页面404错误解决记录

发表于: 2014-05-06   作者:53873039oycg   来源:转载   浏览:
摘要:        以前使用Spring Mvc时候都是返回jsp页面或者ftl页面,昨天想返回html页面,spring-mvc.xml配置如下:   <bean id="viewResolver" class="org.springframework.web.servlet.view.

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

 

<bean id="viewResolver"
		class="org.springframework.web.servlet.view.InternalResourceViewResolver"
		p:prefix="/WEB-INF/html/" p:suffix=".html" />

    Controller方法如下:

  

 @RequestMapping(value = "/add", method = RequestMethod.GET)
	public String toAddTest() {
		return "addTest";
	}

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

 

[06/05/14 10:44:35:035 GMT+08:00] DEBUG support.DefaultListableBeanFactory: Returning cached instance of singleton bean 'sqlSessionFactory'
[06/05/14 10:44:35:035 GMT+08:00] DEBUG servlet.DispatcherServlet: Successfully completed request
[06/05/14 10:44:38:038 GMT+08:00] DEBUG servlet.DispatcherServlet: DispatcherServlet with name 'spring' processing GET request for [/MyTest/test/add]
[06/05/14 10:44:38:038 GMT+08:00] DEBUG annotation.RequestMappingHandlerMapping: Looking up handler method for path /test/add
[06/05/14 10:44:38:038 GMT+08:00] DEBUG annotation.RequestMappingHandlerMapping: Returning handler method [public java.lang.String com.report.controller.testController.toaddTest()]
[06/05/14 10:44:38:038 GMT+08:00] DEBUG support.DefaultListableBeanFactory: Returning cached instance of singleton bean 'testController'
[06/05/14 10:44:38:038 GMT+08:00] DEBUG servlet.DispatcherServlet: Last-Modified value for [/MyTest/test/add] is: -1
[06/05/14 10:44:38:038 GMT+08:00] DEBUG servlet.DispatcherServlet: Rendering view [org.springframework.web.servlet.view.JstlView: name 'addTest'; URL [/WEB-INF/html/addTest.html]] in DispatcherServlet with name 'spring'
[06/05/14 10:44:38:038 GMT+08:00] DEBUG view.JstlView: Forwarding to resource [/WEB-INF/html/addTest.html] in InternalResourceView 'addTest'
[06/05/14 10:44:38:038 GMT+08:00] DEBUG servlet.DispatcherServlet: DispatcherServlet with name 'spring' processing GET request for [/MyTest/WEB-INF/html/addTest.html]
[06/05/14 10:44:38:038 GMT+08:00] DEBUG annotation.RequestMappingHandlerMapping: Looking up handler method for path /WEB-INF/html/addTest.html
[06/05/14 10:44:38:038 GMT+08:00] DEBUG annotation.RequestMappingHandlerMapping: Did not find handler method for [/WEB-INF/html/addTest.html]
[06/05/14 10:44:38:038 GMT+08:00]  WARN servlet.PageNotFound: No mapping found for HTTP request with URI [/MyTest/WEB-INF/html/addTest.html] in DispatcherServlet with name 'spring'
[06/05/14 10:44:38:038 GMT+08:00] DEBUG servlet.DispatcherServlet: Successfully completed request
[06/05/14 10:44:38:038 GMT+08:00] DEBUG servlet.DispatcherServlet: Successfully completed request

 

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

 

   参考了http://stackoverflow.com/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

    解决方法
  http://stackoverflow.com/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了

 

   全文完。

[简单]Spring Mvc返回html页面404错误解决记录

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
第一步创建ErrorPageController 第二步添加Oops页面 @{ ViewBag.Title = "Oops"; Layout = "~/Areas
不知道大家有没有注意到StakeOverflow的404错误页面?其显示了下面的这个图片: 这个是一个很有意思
不知道大家有没有注意到StakeOverflow的404错误页面?其显示了下面的这个图片: 这个是一个很有意思
不知道大家有没有注意到StakeOverflow的404错误页面?其显示了下面的这个图片: 这个是一个很有意思
不知道大家有没有注意到StakeOverflow的404错误页面?其显示了下面的这个图片: 这个是一个很有意思
不知道大家有没有注意到StakeOverflow的404错误页面?其显示了下面的这个图片: 这个是一个很有意思
这几天又被搞去做Web,好几年没搞Web,忽然搞,真心蛋疼。 Web的内容还是比较多的,一上来将所有的
Spring MVC中404 找不到页面错误可以直接使用web.xml中配置: 在<web-app/>节点内加入: <
1.有些提供web服务的网站,在用户访问一个不存在的网站文件时,会提示404错误,如下所示: 现在要求
转载:http://www.apk-doc.net/spring-mvc/spring-mvc-json.html 做app应用开发时,手机客户端适合
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号