`
53873039oycg
  • 浏览: 825999 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

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

 
阅读更多

       以前使用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了

 

   全文完。

0
1
分享到:
评论

相关推荐

    springmybatis

    其实还有更简单的方法,而且是更好的方法,使用合理描述参数和SQL语句返回值的接口(比如IUserOperation.class),这样现在就可以至此那个更简单,更安全的代码,没有容易发生的字符串文字和转换的错误.下面是详细...

    Spring-Reference_zh_CN(Spring中文参考手册)

    2.5.1. Spring MVC的表单标签库 2.5.2. Spring MVC合理的默认值 2.5.3. Portlet 框架 2.6. 其他特性 2.6.1. 动态语言支持 2.6.2. JMX 2.6 .3. 任务规划 2.6.4. 对Java 5(Tiger)的支持 2.7. 移植到Spring 2.0 ...

    基于J2EE框架的个人博客系统项目毕业设计论文(源码和论文)

    由于J2EE的开源的框架中提供了MVC模式实现框架Struts、对象关系模型中的Hibernate 的框架及拥有事务管理和依赖注入的Spring。利用现存框架可以更快开发系统。所以选择Java技术作为blog 的开发工具。 为了增加系统的...

    基于SSM框架+Mysql的企业CRM客户关系管理系统项目源码+数据库+项目说明.zip

    Spring+SpringMVC+MyBatis+Thymeleaf+Vue+JS+Jquery+Axios+Json 服务器: Tomcat_9.0.60 数据库: MySQL_8.0.28 开发工具:IDEA_2021.3.3 页面:Bootstrap_3.3.0 ## 2️⃣已实现的功能 **:star: 账号登录** ...

    asp.net知识库

    如何解决ACCESS中SELECT TOP语句竟然返回多条记录的问题? Asp.net 利用OleDb的GetOLEDBSchemaTable方法得到数据库架构信息 用于 Visual Studio .Net 的 IBM DB2 开发外接程序 第2章 并发操作的一致性问题 (2) Using...

    JAVA上百实例源码以及开源项目源代码

    5个目标文件,演示Address EJB的实现 ,创建一个EJB测试客户端,得到名字上下文,查询jndi名,通过强制转型得到Home接口,getInitialContext()函数返回一个经过初始化的上下文,用client的getHome()函数调用Home接口...

    JAVA上百实例源码以及开源项目

    5个目标文件,演示Address EJB的实现,创建一个EJB测试客户端,得到名字上下文,查询jndi名,通过强制转型得到Home接口,getInitialContext()函数返回一个经过初始化的上下文,用client的getHome()函数调用Home接口...

Global site tag (gtag.js) - Google Analytics