Spring Mvc返回html页面404错误解决记录--转载
2015-05-13 14:37
190 查看
原文地址:http://53873039oycg.iteye.com/blog/2061992
以前使用Spring Mvc时候都是返回jsp页面或者ftl页面,昨天想返回html页面,spring-mvc.xml配置如下:
Xml代码
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/html/" p:suffix=".html" />
Controller方法如下:
Java代码
@RequestMapping(value = "/add", method = RequestMethod.GET)
public String toAddTest() {
return "addTest";
}
在tomcat下测试,页面一直是404,log日志如下:
Java代码
[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时候都是返回jsp页面或者ftl页面,昨天想返回html页面,spring-mvc.xml配置如下:
Xml代码
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/html/" p:suffix=".html" />
Controller方法如下:
Java代码
@RequestMapping(value = "/add", method = RequestMethod.GET)
public String toAddTest() {
return "addTest";
}
在tomcat下测试,页面一直是404,log日志如下:
Java代码
[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了。
全文完。
相关文章推荐
- iOS模态跳转的页面WebView中加载html调用本机相册会dismiss返回上一个页面解决办法
- Nginx自定义404错误页面,返回200状态码,非404状态码解决!
- spring mvc 返回HTML页面的一系列问题
- Spring MVC前台使用html页面作为视图,配置静态资源后Controller控制器不起作用的解决办法
- 解决点击浏览器返回按钮回到上一页面,页面变为初始界面的问题(java+html)
- spring mvc 访问url以.html为后缀时候,返回不了前端html页面
- jsp页面包含含有中文的html页面出现乱码的解决方法(转载)
- Spring MVC前台使用html页面作为视图,配置静态资源后Controller控制器不起作用的解决办法
- spring mvc 中通过responsebody返回json格式到jsp页面乱码解决方法
- ios 解决第一个页面是通过loadData或者是loadHtml时返回控制的问题
- NGINX反向代理对HTML页面的POST请求返回405状态码解决方法
- 记录一个Spring MVC Controller层,跳转到前端页面以及返回参数的包装类
- 如何解决ACCESS中SELECT TOP语句竟然返回多条记录的问题?
- 微信公众号开发 用vue做前端页面 解决IOS返回白屏问题
- 解决HTML加载时,外部js文件引用较多,影响页面打开速度问题
- 解决vue单页使用keep-alive页面返回不刷新的问题
- Python中Django的MTV开发模式(服务器返回html页面功能实现)
- struts2返回到JSP页面的html字符串如何在JSP页面上解析成html标签
- springboot + freemarker 返回html页面基本操作(初学者)
- js 页面history.back()返回上一页,ios 不重新加载ready的解决办法