【问题标题】:make .html default view spring mvc使.html默认视图spring mvc
【发布时间】:2012-11-29 00:23:20
【问题描述】:

有一个示例应用程序并创建了一个

view/HelloWorld.html

页面。从我的控制器,我返回以下内容

public String home(Locale locale, Model model) {
    return "HelloWorld";
}

在调试模式下,我收到此警告/错误:

WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/HelloWorld/WEB-INF/views/HelloWorld.html] in DispatcherServlet with name 'appServlet'

我的 src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml 的内容

<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />

<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />

<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <beans:property name="prefix" value="/WEB-INF/views/" />
    <beans:property name="suffix" value=".html" />
</beans:bean>

如果我将 .html 重命名为 .jsp 并将上面的更改为 .jsp 则一切正常。

【问题讨论】:

    标签: spring-mvc


    【解决方案1】:

    servlet 容器为此请求所经历的流程如下:

    1. 首先 DispatcherServlet 由 Servlet 容器调用。
    2. DispatcherServlet 找到映射到 Controller 的 home 方法的映射,并且 home 方法返回视图名称“HelloWorld”
    3. 现在 DispatcherServlet 使用 View Resolver(您的 InternalResourceViewResolver)来查找要渲染模型的视图,因为名称是“HelloWorld”,所以它映射到 /WEB-INF/view/HelloWorld.html 视图。
    4. 现在基本上是拨打RequestDispatcher.forward("/WEB-INF/views/HelloWorld.html",....
    5. 此时的 Servlet 容器试图找到可以处理 /WEB-INF/views/HellowWorld.html uri 的 servlet - 如果它是 .jsp,则注册了一个 JSPServlet,它可以处理呈现 jsp,但是对于 *.html,那里没有注册 servlet,所以调用以 "default servlet" 结束,它注册了一个 servlet 映射 /,这可能是您的 DispatcherServlet。
    6. 现在 Dispatcher servlet 找不到控制器来处理对 /WEB-INF/views/HelloWorld.html 的请求,因此找不到您看到的消息

    如果你想让这种扩展由 servlet 容器处理,比如 tomcat,你可以注册 *.html 扩展来由 JSPServlet 处理,然后它应该可以干净地工作。或者返回forward:/resources/HelloWorld.html,它将被视为相对于您的resources 文件夹的静态文件。

    【讨论】:

    【解决方案2】:

    html 和 jsp 有很大的不同。 Java 服务器页面被编译成 Java “servlet”。它可以调用bean和企业bean,例如Java Beans组件和Enterprise Java Beans组件,在服务器上执行处理。因此,拥有这样的 JSP 技术可能是基于 Web 的应用程序的高级架构中的关键组件。

    【讨论】:

      猜你喜欢
      • 2011-01-31
      • 2011-01-01
      • 2016-02-04
      • 2011-06-29
      • 2017-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-14
      相关资源
      最近更新 更多