Spring MVC webapp 未在浏览器上加载

Spring MVC webapp not loading on browser

我是 spring mvc 和 tomcat 的新手。 我开发了一个演示 spring mvc 项目,并试图通过 eclipse 将它部署在 tomcat 9 上。 服务器成功启动,但是当我尝试从浏览器访问 url 时,我得到 404 并在屏幕上显示以下错误消息。 :

消息请求的资源[/spring-mvc-demo/]不可用

说明源服务器未找到目标资源的当前表示或不愿透露该表示存在。

以下是我的代码详情:

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">

<display-name>spring-mvc-demo</display-name>

<absolute-ordering />

<!-- Spring MVC Configs -->

<!-- Step 1: Configure Spring MVC Dispatcher Servlet -->
<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring-mvc-demo-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<!-- Step 2: Set up URL mapping for Spring MVC Dispatcher Servlet -->
<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/spring-mvc-demo</url-pattern>
</servlet-mapping>

</web-app>

spring-mvc-demo-servlet.xml

<?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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc.xsd">

<!-- Step 3: Add support for component scanning -->
<context:component-scan base-package="main.webapp" />

<!-- Step 4: Add support for conversion, formatting and validation support -->
<mvc:annotation-driven/>

<!-- Step 5: Define Spring MVC view resolver -->
<bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/view/" />
    <property name="suffix" value=".jsp" />
</bean>

</beans>

HomeController.java

package main.webapp.springdemo.controller;

import javax.annotation.PostConstruct;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
 public class HomeController {

@PostConstruct
public void init() {
    System.out.println("HomeController bean getting intiated");
}

@RequestMapping("/")
public String getMainMenu() {
    return "main-menu";
}
}

我在 tomcat 9 上通过 eclipse 以及手动尝试了 运行 这个应用程序,但在这两种情况下我都遇到了同样的错误。

假设应用程序部署没有错误(检查您的日志),您用来访问它的 URI 路径几乎肯定是错误的。在 servlet 环境中,URI 路径分解为:

<context-path><servlet-path><path-info>

其中:

  • <context-path> 是您应用程序的前缀。在 Eclipse 服务器配置页面中,这被称为 "path" 并且默认为 /<project-name>,
  • <servlet-path> 是通过 web.xml 部署描述符中的 <servlet-mapping> 元素配置的,
  • <path-info>是Spring通常用来执行其内部路由的部分(除非alwaysUseFullPath设置在HandlerMapping上)。

因此(理论上)您应该尝试访问:

http://localhost:8080/projectName/spring-mvc-demo/

然而还有另一个问题:您的 servlet 映射是 精确 映射,不匹配 /projectName/spring-mvc-demo 之外的任何其他内容。您应该将其替换为 prefix 映射(有关 servlet 映射的概述,请参阅 this question):

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/spring-mvc-demo/*</url-pattern>
</servlet-mapping>

如果您想缩短 URL,DispatcherServlet 通常映射为默认 servlet /。请注意不要使用包罗万象的前缀映射/*,这将覆盖JSP servlet 的映射。

备注: 有些情况下Spring不使用<servlet-path>之后的部分作为路由,例如当您使用精确映射或扩展映射时,将使用 <context-path> 之后的整个路径。

因此,如果您使用:

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/spring-mvc-demo</url-pattern>
</servlet-mapping>

你应该指定:

@RequestMapping("/spring-mvc-demo")
public String getMainMenu() {
   ...
}

并使用 URL http://localhost:8080/appName/spring-mvc-demo.