[英]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.