尝试在 Spring 中加载默认页面时出现 404 错误
Getting 404 error while trying to load the default page in Spring
我正在尝试将我的应用程序的默认页面设置为 index.html
我的文件夹结构是
我的调度员-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 处理。现在初始请求发生了什么
- 您的初始请求将由 DispatcherServlet 处理,因为它映射到
<url-pattern>/</url-pattern>
- 处理程序方法returns视图名称索引
- 视图名称与前缀和后缀连接成为
/WEB-INF/views/index.html
,然后转发给其他 servlet 处理
- 这将再次被 DispatcherServlet
捕获
- 找不到映射
/WEB-INF/views/index.html
的处理程序方法,此时它失败了。
如果视图后缀设置为 .jsp
,结果会有所不同。
Servlet containters 有一个 servlet,映射到默认注册的 *.jsp
。例如 tomcat 的 web.xml 你会看到 org.apache.jasper.servlet.JspServlet 映射到 *.jsp
或 *.jspx
,因此在这种情况下将呈现一个视图
您的所有请求都映射到调度程序 servlet,这就是它尝试使用视图解析器为 index.jsp 解析视图的原因。理想情况下,您应该将静态资源放在不同的文件夹中,如下所示。
<mvc:resources mapping="/resources/**" location="/resources/static/" />
或者为 returns 'index' 的主路径“/”添加一个控制器,然后它将被视图解析器正确解析。
我正在尝试将我的应用程序的默认页面设置为 index.html
我的文件夹结构是
我的调度员-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 处理。现在初始请求发生了什么
- 您的初始请求将由 DispatcherServlet 处理,因为它映射到
<url-pattern>/</url-pattern>
- 处理程序方法returns视图名称索引
- 视图名称与前缀和后缀连接成为
/WEB-INF/views/index.html
,然后转发给其他 servlet 处理 - 这将再次被 DispatcherServlet 捕获
- 找不到映射
/WEB-INF/views/index.html
的处理程序方法,此时它失败了。
如果视图后缀设置为 .jsp
,结果会有所不同。
Servlet containters 有一个 servlet,映射到默认注册的 *.jsp
。例如 tomcat 的 web.xml 你会看到 org.apache.jasper.servlet.JspServlet 映射到 *.jsp
或 *.jspx
,因此在这种情况下将呈现一个视图
您的所有请求都映射到调度程序 servlet,这就是它尝试使用视图解析器为 index.jsp 解析视图的原因。理想情况下,您应该将静态资源放在不同的文件夹中,如下所示。
<mvc:resources mapping="/resources/**" location="/resources/static/" />
或者为 returns 'index' 的主路径“/”添加一个控制器,然后它将被视图解析器正确解析。