以前使用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
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>
<servlet-mapping>
<servlet-name>jsp</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
再次访问就OK了。
全文完。
相关推荐
《精通Spring MVC4》这本书是Java开发者们的重要参考资料,它深入浅出地讲解了Spring MVC这一强大框架的各个方面。Spring MVC是Spring框架的一个模块,专为构建Web应用程序而设计,以其灵活性、高效性和可扩展性受到...
6. **定义视图解析器**:在Spring配置文件中,通常会配置一个视图解析器,如`InternalResourceViewResolver`,来将逻辑视图名转换为实际的HTML页面。 7. **创建视图**:在`src/main/webapp/WEB-INF/views`目录下...
这个“spring3.0Mvc简单示例”旨在帮助开发者快速理解并入门Spring MVC的基本概念和操作流程。让我们深入探讨一下Spring MVC的核心特性以及如何在项目中设置和使用它。 首先,Spring MVC是Spring框架的一部分,它...
在本项目中,我们探讨的是一个基于Spring MVC框架构建的JavaWeb应用。Spring MVC是Spring框架的一个模块,专为创建Web应用程序提供模型-视图-控制器(MVC)架构支持。这个项目涉及到的功能包括分页显示、单个或批量...
在Spring MVC框架中,网页间的跳转是通过控制器(Controller)处理请求并返回视图(View)来实现的。在给定的场景中,我们有一个简单的应用,用户在`index.jsp`页面上点击一个链接,目标是跳转到`success.jsp`页面。...
Spring MVC提供了ModelAndView或ResponseBody等机制来处理返回的视图,可以是HTML页面,也可以是JSON或XML等数据格式。Spring Security可以用来处理认证和授权,保护应用程序的安全。 总的来说,Spring MVC、Spring...
Spring MVC 可以与多种模板引擎(如FreeMarker、Thymeleaf、Velocity等)配合,生成动态HTML页面。 10. **单元测试**: 利用Spring Test和MockMVC,可以方便地对Spring MVC应用进行单元测试和集成测试。 通过这...
Spring MVC 是 Spring 框架的一个模块,专为构建基于模型-视图-控制器(Model-View-Controller)架构的Web应用程序。这个框架提供了一种高效、灵活且强大的方式来组织和构建Web应用。在“spring mvc的例子”中,我们...
Spring MVC 提供了统一的异常处理机制,可以通过 @ExceptionHandler 注解或配置全局异常处理器来优雅地处理错误。 11. **视图技术**: Spring MVC 支持多种视图技术,如 JSP、Thymeleaf、Freemarker 等,可以根据...
Spring MVC是Spring框架的一个核心模块,专为构建Web应用程序而设计。它提供了模型-视图-控制器(MVC)架构模式的实现,帮助开发者将业务逻辑、数据展示和用户交互分离,提高代码的可维护性和可测试性。4.2.4....
在Spring MVC中,可以通过简单的配置返回一个静态的HTML或JSP页面。这通常用于项目的初始页面或者一些不需要动态数据填充的页面。 **示例代码**: ```java @Controller public class StaticPageController { @...
Spring MVC 是一个基于Java的轻量级Web应用框架,它为构建模型-视图-控制器(MVC)架构的应用程序提供了强大的支持。该框架的主要目标是简化开发过程,提高代码的可测试性和可维护性。在给定的压缩包文件中,包含的...
Spring MVC 是一个强大的Java Web应用程序框架,用于构建高效、可维护的Web应用。它通过将Model(模型)、View(视图)和Controller(控制器)分离,实现了MVC设计模式,提高了代码的组织性和可测试性。在本课中,...
- Spring支持多种模板引擎,如FreeMarker、Thymeleaf,用于生成动态HTML页面。 9. **RESTful风格** - Spring MVC支持构建RESTful API,通过@RequestMapping和HTTP方法来设计资源操作。 10. **Spring Boot集成** ...
**Spring MVC + Ajax 用户登录增删改查功能详解** 在Web开发中,Spring MVC和Ajax是两种非常重要的技术。Spring MVC作为Spring框架的一部分,提供了一种模型-视图-控制器的架构模式,使得开发者能够更好地组织和...
在本项目中,我们利用了Java生态中的四个关键组件——Spring、Spring MVC、Hibernate和Material前端框架,构建了一个简易的BBS论坛系统。这个系统旨在提供一个互动平台,让用户能够提问、讨论技术问题,促进技术交流...
Spring MVC 是一款基于Java的轻量级Web应用框架,它为构建RESTful应用程序提供了强大的支持。作为初学者,理解Spring MVC的基本概念和工作原理是非常重要的。以下是对Spring MVC的详细介绍,以及如何使用它来快速...
本文将详细解析基于"springboot+spring mvc+mybatis+jquery+Mysql"的超市订单系统毕设项目源码中的关键知识点。该系统已经经过多次测试,确保无误,适用于学习与实践。 1. **Spring Boot** Spring Boot是Spring...
在本压缩包"Spring_MVC.rar_T7Y_springmvc"中,包含了初学者学习Spring MVC所需的基础配置和简单的log4j日志测试代码。 首先,我们来详细了解一下Spring MVC的核心概念和组成部分: 1. **DispatcherServlet**:...