<welcome-file>中的.html文件在.jsp文件加载</welcome-file>时抛出404错误

时间:2015-04-04 10:34:26

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

我的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,两者都在同一目录中,( ie)在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错误。我已经检查了其他代码。一切似乎都是正确的。我不能完全理解我在这里做错了。

1 个答案:

答案 0 :(得分:0)

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