【问题标题】:HTTP Status 404 occur when load index or first page加载索引或首页时出现 HTTP 状态 404
【发布时间】:2015-09-17 09:40:25
【问题描述】:

是 Spring MVC 的新手。我正在开发我的第一个项目。如果我运行我的项目得到一个错误 HTTP 状态 404。没有部署错误

我的 web.xml 如下所示

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
         xmlns="http://java.sun.com/xml/ns/j2ee" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

   <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <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>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>

</web-app>

和我的调度器 servlet

<?xml version='1.0' encoding='UTF-8' ?>
<!-- was: <?xml version="1.0" encoding="UTF-8"?> -->
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       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-3.2.xsd
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd ">

    <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
    <mvc:annotation-driven />
    <mvc:view-controller path="/" view-name="view/index" />
    <context:component-scan base-package="com.innovaturelabs.multicast.controller" use-default-filters="false">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
    </context:component-scan>
    <bean id="viewResolver"
         class="org.springframework.web.servlet.view.InternalResourceViewResolver"
         p:prefix="/WEB-INF/"
         p:suffix=".html">

    </bean>
</beans>

应用 servlet 是

<?xml version='1.0' encoding='UTF-8' ?>
<!-- was: <?xml version="1.0" encoding="UTF-8"?> -->
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-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/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">

    <bean id="propertyConfigurer"
          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
          p:location="/WEB-INF/Properties/Database.properties" />

    <util:properties id="hibernateProperties" location="/WEB-INF/Properties/Hibernate.properties" />

    <bean id="dataSource"
          class="org.apache.tomcat.jdbc.pool.DataSource"
          p:driverClassName="${database.driver}"
          p:url="${database.url}"
          p:username="${database.user}"
          p:password="${database.password}"
          p:testWhileIdle="true"
          p:validationQuery="SELECT 1" />


    <bean id="UserDao" class="com.innovaturelabs.multicast.dao.UserDao" />
    <bean id="sessionFactory"
      class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"
      p:dataSource-ref="dataSource"
      p:packagesToScan="com.innovaturelabs.spring.web.entity"
      p:hibernateProperties-ref="hibernateProperties" />

</beans>

【问题讨论】:

  • 404 是客户端错误 - “我找不到该页面”您在访问的 url 上运行什么上下文?访问日志说什么?
  • 将html文件放在WEB-INF/view/index.html中。 tomcat 日志不产生任何错误信息
  • 你的 JAR 文件的名称是什么,你能发布你的 Maven POM 吗?
  • 您使用什么 url 来尝试访问它? localhost:8080??

标签: java spring spring-mvc servlets


【解决方案1】:

就您在“/”处映射调度程序 servlet 而言,所有请求都通过它获取。

<context:component-scan base-package="com.innovaturelabs.multicast.controller" use-default-filters="false">
    <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>

你的控制器是用这个包写的吗?

类似:

package com.innovaturelabs.multicast.controller;

@Controller
public class  Controler() {
  @RequestMapping("/")
  public String index() {
    return "index";
  }
}

另一件事是你应该使用带有InternalResourceViewResolver而不是html的jsp文件。

   <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"
         p:prefix="/WEB-INF/"
         p:suffix=".jsp">
    </bean>

Html 文件应该在静态文件夹中,并通过 mvc:resource 标签映射

<mvc:resources mapping="/resources/**"
               location="/resources/html/"

我也是春天的新手。希望我能帮上忙。

【讨论】:

  • 你的回答不充分
猜你喜欢
  • 2020-05-21
  • 2015-08-04
  • 1970-01-01
  • 1970-01-01
  • 2012-02-23
  • 2017-02-24
  • 2018-03-05
  • 1970-01-01
  • 2021-01-21
相关资源
最近更新 更多