【问题标题】:404 not found for Spring MVC on tomcat在 tomcat 上找不到 Spring MVC 的 404
【发布时间】:2017-07-10 16:35:06
【问题描述】:

我是 Spring MVC 的新手。我正在尝试使用 Spring MVC 和 Tomcat 作为服务器创建一个简单的 Web 应用程序。但是当我部署我的项目时,我得到错误 404 not found。 请你帮助我好吗? 这是我的 web.xml

  <web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         version="2.5">
    <servlet>
        <servlet-name>spring-web</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>spring-web</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>WEB-INF/spring-web-servlet.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener> 
    <welcome-file-list>
        <welcome-file>/main/index</welcome-file>
    </welcome-file-list>
  </web-app>

这是我的控制器:

@Controller
@RequestMapping(value = "/main")
  public class RootController {
    @RequestMapping(value = "/index")
    public String printHello(){
        return "index";
      }
    }

这是我的 spring-web-servlet.xml

<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"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-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/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-3.2.xsd">
    <context:component-scan base-package="com.controllers" />
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/views/jsp/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>
    <mvc:annotation-driven />
</beans>

【问题讨论】:

  • 服务器启动后可以访问tomcat服务器页面吗?通常http://localhost:8080/ 如果可以去管理器应用程序并看到您的应用程序在那里列出。
  • @Rajith Pemabandu,不,我在那里看不到我的应用程序,我在localhost:8080 上什么也得不到
  • 如果您只想让 Spring MVC Web 应用程序快速启动并运行,您是否考虑过 Spring Boot?您可以从 start.spring.io 快速设置项目。如果不能,请发布 tomcat 日志。
  • 你也可以发布你的jsp文件吗?顺便说一句,您不需要在 Welcome-file-list 中列出 /main/index

标签: java spring spring-mvc tomcat


【解决方案1】:

默认情况下,服务器按以下顺序查找欢迎文件:

<welcome-file-list> in web.xml
index.html
index.htm
index.jsp

如果没有找到这些文件,服务器会呈现 404 错误。

【讨论】:

    【解决方案2】:

    问题出在我的项目结构中。我错误地创建了它,因此我的项目的整个结构的正确性和其中文件的可见性存在问题。创建新项目对我有帮助。

    【讨论】:

      猜你喜欢
      • 2018-07-06
      • 2017-05-19
      • 2015-01-19
      • 2020-06-11
      • 2015-05-06
      • 2021-05-17
      • 2018-11-03
      • 2011-06-14
      • 2016-10-20
      相关资源
      最近更新 更多