Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/371.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在Spring中尝试加载默认页面时出现404错误_Java_Spring_Spring Mvc_Default - Fatal编程技术网

Java 在Spring中尝试加载默认页面时出现404错误

Java 在Spring中尝试加载默认页面时出现404错误,java,spring,spring-mvc,default,Java,Spring,Spring Mvc,Default,我正在尝试将应用程序的默认页面设置为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"

我正在尝试将应用程序的默认页面设置为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-INF/views/
.html
我的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>

NewAppPoc
index.html
调度员
org.springframework.web.servlet.DispatcherServlet
1.
调度员
/
上下文配置位置
/WEB-INF/dispatcher-servlet.xml
org.springframework.web.context.ContextLoaderListener

我正在尝试访问,因此出现404错误。我尝试了很多次搜索,但都无法显示我的index.html。我确信我做错了什么,但找不到什么。任何帮助都将不胜感激。

您遇到了这个问题,因为没有能够处理
/WEB-INF/views/index.html
请求的映射

最快的解决方案是将.html文件转换为.jsp文件,并将InternalResourceViewResolver的后缀也从.html转换为.jsp。如果您需要保留html文件,您应该使用配置中的标记将它们配置为静态资源。差不多

<mvc:resources mapping="/index.html" location="/WEB-INF/views/" /> 

更长的解释

动态资源由映射到URL的servlet处理。现在,初始请求会发生什么

  • 您的初始请求将由DispatcherServlet处理,因为它映射为
    /
  • handler方法返回视图名称索引
  • 视图名称与前缀和sufix连接,成为
    /WEB-INF/views/index.html
    ,然后转发给其他servlet处理
  • 这将再次被DispatcherServlet捕获
  • 将找不到映射的处理程序方法
    /WEB-INF/views/index.html
    ,此时它将失败
  • 如果将视图后缀设置为
    .jsp
    ,则结果会有所不同。
    Servlet容器有一个Servlet,该Servlet映射到defualt注册的
    *.jsp
    。例如,在tomcat的web.xml中,您将看到一个org.apache.jasper.servlet.JspServlet映射到
    *.jsp
    *.jspx
    ,因此在这种情况下,将呈现一个视图

    所有请求都映射到dispatcher servlet,这就是为什么它尝试使用视图解析器解析index.jsp的视图。理想情况下,您应该将静态资源放在不同的文件夹中,如下所示

    <mvc:resources mapping="/resources/**" location="/resources/static/" />
    
    
    

    或者为返回“index”的主路径“/”添加一个控制器,然后它将由视图解析程序正确解析。

    因此,我应该从dispatcher servlet中删除并添加一个映射“/”的控制器,这样做行吗?不太行,通过视图控制器或通过控制器映射归结为同一件事。若要使其正常工作,请将index.html更改为index.jsp,同时更改后缀。或者向您的配置中添加一个元素,该元素会请求静态资源,因此ommiting mapping问题实际上现在当我访问index.html时,它会显示页面,但不会加载css和js文件。这些也是静态资源,因此如何在我的html中映射它们?是的,您也应该为此配置静态映射,检查答案中的链接。根据您的文件夹结构,您应该列出
    等。通常,人们会将所有静态资源移动到webapp根目录中单个文件夹下,例如资源,在映射
    的基础上,选择对您感觉更好的方法,只是我们使用了/index.html并映射了资源是否存在,以便在默认情况下将其加载到“/”上。因此,我应该从dispatcher servlet中删除并添加一个映射“/”的控制器,该控制器会起作用吗?这也应该起作用,抱歉,我没有第一次看到它。尝试从web.xml中删除欢迎文件