【问题标题】:.html file in <welcome-file> throws 404 error while a .jsp file loads<welcome-file> 中的 .html 文件在加载 .jsp 文件时引发 404 错误
【发布时间】:2015-06-09 07:44:21
【问题描述】:

我的 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_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>Go Scrapper</display-name>

  <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>

  <welcome-file-list>
    <welcome-file>new.jsp</welcome-file>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>

</web-app>

我只在运行程序时使用以下任何一种,当我使用 new.jsp 时,我得到一个页面,但是当我使用 index.html 时,我得到一个错误 404,两者都在同一个目录中,(即)在 WebContent/ 下。

我都输入了这里以供您理解

我的 dispatcher-servlet.xml

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

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

    <context:component-scan base-package="com.go.controller" />
    <mvc:annotation-driven />

    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/" />
        <property name="suffix" value=".html" />
    </bean>

    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="org.postgresql.Driver" />
        <property name="url" value="jdbc:postgresql://localhost:5432/GoAnalyserDB" />
        <property name="username" value="postgres" />
        <property name="password" value="toor" />
    </bean>

</beans>

大家好,当欢迎文件是 jsp 时,它会加载页面,如果我将其更改为 html,则会出现 404 错误。我已经检查过其他代码。一切似乎都是正确的。我不能完全理解我在这里做错了什么。

【问题讨论】:

  • 我认为您的jsp 文件名是index.jsp,您的jsp 文件在哪里?您将wellcome文件设置为new.jsp请先检查文件名。
  • 问题是当我将欢迎文件设置为 new.jsp 时,会加载欢迎页面,但是当我将欢迎文件设置为 index .html .... 时,它会显示 404。两个文件存在于 WebContent/
  • 我知道,但你的jsp 文件名是什么?
  • jsp文件名为new.jsp,html文件名为index.html
  • 您的 jsp 文件在“webapp”文件夹中?

标签: java spring jsp spring-mvc http-status-code-404


【解决方案1】:
    i have tried following way and it's work.

    Spring config:

    <resources mapping="/static/**" location="/WEB-INF/static/" />

<beans:bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
     <beans:property name="prefix" value="" />
<beans:property name="suffix" value=".html" />
    </beans:bean>



    Controller method:

    @RequestMapping(value = "/", method = RequestMethod.GET)
      public String homePage(ModelMap model, HttpServletRequest servletRequest, HttpServletResponse response) {
        String returnText = "static/html/index";
        return returnText;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-15
    • 2014-10-20
    • 2022-11-14
    • 2015-05-17
    • 1970-01-01
    • 2021-12-31
    • 2015-12-24
    • 2023-02-02
    相关资源
    最近更新 更多