簡體   English   中英

沒有視圖解析器但純HTML的Spring

[英]Spring with no view resolver but plain html

我試圖將Spring配置為僅使用我的html文件而不是jsp視圖解析器,但無法使其正常工作。 我嘗試了許多不同的配置,並且我只想每次輸入localhost:8080 /時都重定向到/WEB-INF/views/index.html。 現在,我的tomcat控制台中的內容是:

org.springframework.web.servlet.PageNotFound-在DispatcherServlet中,名稱為“ appServlet”的URI [/WEB-INF/views/index.html]的HTTP請求未找到映射。

這是我的servlet-context.xml代碼段。

<beans:bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <beans:property name="prefix" value="WEB-INF/views/" />
    <beans:property name="suffix" value="" />
</beans:bean>
<view-controller path="/" view-name="index.html"/>

有什么建議我想念什么?

編輯-web.xml:

<?xml version="1.0" encoding="UTF-8"?>

http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd“>

<!-- The definition of the Root Spring Container shared by all Servlets 
    and Filters -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- Processes application requests -->
<servlet>

    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
    <welcome-file>/WEB-INF/views/index.html</welcome-file>
</welcome-file-list> 

我知道這是一個比較老的問題,所以這是給現在來這里的人的。 只要文件路徑正確,Spring就會自動配置為獲取您的視圖,因此您不必進行任何spring webmvc configuration

我在Intellij中遇到了這個問題,只需要重新啟動應用程序,錯誤就消失了。 希望有人看到這個並且不會花很多時間在此上。

這個錯誤

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

告訴我們您實際上要轉到以下地址

localhost:8080/WEB-INF/views/index.html

這顯然沒有意義。

如果你想

每次輸入localhost:8080 /時都重定向到/WEB-INF/views/index.html

將歡迎文件添加到您的部署描述符。

<welcome-file-list>
    <welcome-file>WEB-INF/views/index.html</welcome-file>
</welcome-file-list>

嘗試在前綴前添加/。

<beans:bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <beans:property name="prefix" value="/WEB-INF/views/" />
    <beans:property name="suffix" value="" />
</beans:bean>
<view-controller path="/" view-name="index.html"/>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM