[英]Getting 404 error while trying to load the default page in Spring
我正在嘗試將我的應用程序的默認頁面設置為index.html
我的文件夾結構是
我的dispatcher-Servlet.xml是
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<mvc:view-controller path="/" view-name="index"/>
<!-- <context:component-scan base-package="com.programcreek.helloworld.controller" /> -->
<mvc:annotation-driven />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/views/</value>
</property>
<property name="suffix">
<value>.html</value>
</property>
</bean>
</beans>
我的web.xml是
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>NewAppPoc</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<!-- <welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file> -->
</welcome-file-list>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml
</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
</web-app>
我正在嘗試訪問http:// localhost:7001 / NewAppPoc /,因此出現404錯誤。 我嘗試了很多搜索,但無法顯示我的index.html。我確定我做錯了什么,但是找不到。 任何幫助將不勝感激。
您遇到了這個問題,因為沒有可以處理/WEB-INF/views/index.html
請求的映射。
最快的解決方案是將.html轉換為.jsp文件,並將InternalResourceViewResolver的后綴也從.html轉換為.jsp 。 如果需要保留html文件,則應在配置中使用mvc:resources標記將它們配置為靜態資源。 就像是
<mvc:resources mapping="/index.html" location="/WEB-INF/views/" />
更長的解釋
動態資源由映射到URL的servlet處理。 現在,初始請求會發生什么
<url-pattern>/</url-pattern>
映射的 /WEB-INF/views/index.html
,然后轉發給其他servlet處理 /WEB-INF/views/index.html
映射的處理程序方法,此時該方法失敗。 如果將視圖后綴設置為.jsp
會有所不同。 Servlet容器包含一個servlet,該servlet映射到defualt注冊的*.jsp
。 例如tomcat的web.xml,您將看到org.apache.jasper.servlet.JspServlet映射到*.jsp
或*.jspx
,因此在這種情況下將呈現視圖
您所有的請求都映射到調度程序servlet,這就是為什么它嘗試使用視圖解析器解析index.jsp視圖的原因。 理想情況下,您應將靜態資源放在另一個文件夾中,如下所示。
<mvc:resources mapping="/resources/**" location="/resources/static/" />
或為返回“索引”的主路徑“ /”添加一個控制器,然后它將由視圖解析器正確解析。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.