首页 > 解决方案 > Spring MVC webapp未在浏览器上加载

问题描述

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

家庭控制器.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";
}
}

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

标签: javaspringspring-mvctomcat

解决方案


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

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

在哪里:

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

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

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

但是还有另一个问题:您的 servlet 映射是一个精确的映射,与/projectName/spring-mvc-demo. 您应该将其替换为前缀映射(有关 servlet 映射的概述,请参阅此问题):

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

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

备注:在某些情况下,Spring 不使用 after 部分<servlet-path>进行路由,例如,当您使用精确映射或扩展映射时,会使用整个路径 after <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


推荐阅读