原文地址:http://53873039oycg.iteye.com/blog/2061992

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

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

Controller方法如下:

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

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

  1. [06/05/14 10:44:35:035 GMT+08:00] DEBUG support.DefaultListableBeanFactory: Returning cached instance of singleton bean 'sqlSessionFactory'
  2. [06/05/14 10:44:35:035 GMT+08:00] DEBUG servlet.DispatcherServlet: Successfully completed request
  3. [06/05/14 10:44:38:038 GMT+08:00] DEBUG servlet.DispatcherServlet: DispatcherServlet with name 'spring' processing GET request for [/MyTest/test/add]
  4. [06/05/14 10:44:38:038 GMT+08:00] DEBUG annotation.RequestMappingHandlerMapping: Looking up handler method for path /test/add
  5. [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()]
  6. [06/05/14 10:44:38:038 GMT+08:00] DEBUG support.DefaultListableBeanFactory: Returning cached instance of singleton bean 'testController'
  7. [06/05/14 10:44:38:038 GMT+08:00] DEBUG servlet.DispatcherServlet: Last-Modified value for [/MyTest/test/add] is: -1
  8. [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'
  9. [06/05/14 10:44:38:038 GMT+08:00] DEBUG view.JstlView: Forwarding to resource [/WEB-INF/html/addTest.html] in InternalResourceView 'addTest'
  10. [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]
  11. [06/05/14 10:44:38:038 GMT+08:00] DEBUG annotation.RequestMappingHandlerMapping: Looking up handler method for path /WEB-INF/html/addTest.html
  12. [06/05/14 10:44:38:038 GMT+08:00] DEBUG annotation.RequestMappingHandlerMapping: Did not find handler method for [/WEB-INF/html/addTest.html]
  13. [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'
  14. [06/05/14 10:44:38:038 GMT+08:00] DEBUG servlet.DispatcherServlet: Successfully completed request
  15. [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错误解决记录--转载的更多相关文章

  1. spring mvc访问html页面404报错解决

    <servlet> <servlet-name>context</servlet-name> <servlet-class>org.springfram ...

  2. spring mvc MultipartFile 上传文件错误解决

    Field error in object 'xxxx' on field 'xxxx': rejected value [20129259128131.jpg]; codes [typeMismat ...

  3. Spring MVC与html页面的交互(以传递json数据为例)

    一.导入相jar包 主要包括spring相关jar包和fastjson jar包,具体步骤略. 二.配置相关文件 1.配置web.xml文件 <?xml version="1.0&qu ...

  4. 转:spring mvc model.addAttribute页面c:forEach取不到

    原文链接:http://www.cnblogs.com/beautifulFuture/p/3957426.html spring mvc model.addAttribute页面c:forEach取 ...

  5. spring mvc返回json字符串的方式

    spring mvc返回json字符串的方式 方案一:使用@ResponseBody 注解返回响应体 直接将返回值序列化json            优点:不需要自己再处理 步骤一:在spring- ...

  6. spring mvc返回json字符串数据,只需要返回一个java bean对象就行,只要这个java bean 对象实现了序列化serializeable

    1.spring mvc返回json数据,只需要返回一个java bean对象就行,只要这个java bean 对象实现了序列化serializeable 2. @RequestMapping(val ...

  7. IIS7或者IIS7.5部署MVC项目时出现404错误

    IIS7或者IIS7.5部署MVC项目时出现404错误 服务器上需要安装Windows 补丁 kb980368  下载链接:http://support.microsoft.com/kb/980368

  8. Spring MVC 返回NULL时客户端用$.getJSON的问题

    如果Spring MVC返回是NULL,那么客户端的$.getJSON就不会触发: 必须返回点什么东西: 如果返回的是一个字符串,客户端的$.getJSON也不会触发:把字符串 包装成List< ...

  9. http协议 put、delete请求asp.net mvc应用,报404错误

    http协议 put.delete请求asp.net mvc应用,报404错误 更改web.config,在<modules>节点中设置 runAllManagedModulesForAl ...

随机推荐

  1. Windows Azure 配置多个站点的虚拟网络连接

    通过上一篇"Windows Azure 虚拟网络配置(Site to Site)" 我们建立了可以进行Site to Site连接的虚拟网络,配置过后有些朋友会有疑问:如果需要连接 ...

  2. NServiceBus教程-持久化配置

    当配置在NServiceBus v5持久性,秩序是非常重要的.最后赢得持久性配置选项.我们看看一些例子. 示例1 在本例中最后一个配置选项将会覆盖前面的所有选项. v5.2 v5.0 编辑 var c ...

  3. rhel5.5 Oracle10g安装记录

    ---恢复内容开始--- Rhel5.5 Oracle10g安装成功截图如下

  4. int* V.S. int[]

    [int* V.S. int[]] 在C++中,int[]有2种形态,一种是指针形态,即使用方法和int*一样,另一种是符号形态,即只是一个编译期的符号(意味着在runtime期,所定义的int[]根 ...

  5. Wisdombud.CommonTool及其应用

    @(编程) 1. 用法 student类 using System.ComponentModel; namespace WindowsFormsApplication1 { public class ...

  6. JPA多对多@manytomany注解配置实例

    维护端注解 @ManyToMany (cascade = CascadeType.REFRESH) @JoinTable (//关联表 name = "student_teacher&quo ...

  7. E3-1230和E3-1230 V2有多神?

    最近追E3-1230,枪E3-1230的人那叫一个多啊,都被捧成神了,我也来说说对E3-1230的看法.同档次的装机方案,我更倾向i5 2320/2500K/3570K. 首 先比较两个U的规格吧.E ...

  8. hdu 4115 Eliminate the Conflict ( 2-sat )

    Eliminate the Conflict Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  9. HDU 5164Matching on Array(AC自动机)

    这是BC上的一道题,当时比赛没有做,回头看看题解,说是AC自动机,想着没有写过AC自动机,于是便试着抄抄白书的模板,硬是搞了我数个小时2000ms时限1800过了= = ! 这里就直接贴上BC的结题报 ...

  10. HDU 4596 Yet another end of the world (数学,扩展欧几里德)

    题意:给出n组x,y,z.判断是否存在一个id使得id%x1∈(y1,z1),id%x2∈(y2,z2). 析: 设 id/x1=a , id/x2=b ,则 id-a*x1=u;   (1) id- ...