无法将 Spring mvc 部署到 intellij 中的 tomcat

我正在尝试在 IntelliJ 中将没有 spring boot 的简单 spring MVC 项目部署到 tomcat,但我无法让它工作,服务器总是返回 404。我尝试了很多教程,但它仍然不起作用。


我也尝试过 AbstractAnnotationConfigDispatcherServletInitializer 类,但它也不起作用。似乎onStartup从未调用过该方法。


有人可以指出我错在哪里并让它发挥作用吗?


这是我的 pom.xml 文件


<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0"

         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>


    <groupId>com.learning</groupId>

    <artifactId>spring-web-no-boot</artifactId>

    <version>1.0-SNAPSHOT</version>


    <properties>

        <org.springframework.version>5.1.5.RELEASE</org.springframework.version>

    </properties>


    <dependencies>

        <!-- core spring dependency -->

        <dependency>

            <groupId>org.springframework</groupId>

            <artifactId>spring-context</artifactId>

            <version>${org.springframework.version}</version>

            <!--<scope>runtime</scope>-->

        </dependency>

        <!-- spring web -->

        <dependency>

            <groupId>org.springframework</groupId>

            <artifactId>spring-web</artifactId>

            <version>${org.springframework.version}</version>

        </dependency>

        <dependency>

            <groupId>org.springframework</groupId>

            <artifactId>spring-webmvc</artifactId>

            <version>${org.springframework.version}</version>

        </dependency>


        <!-- provide servlet, web server -->

        <dependency>

            <groupId>org.apache.tomcat.embed</groupId>

            <artifactId>tomcat-embed-core</artifactId>

            <version>9.0.16</version>

        </dependency>


        <!-- json jackson -->

        <dependency>

            <groupId>com.fasterxml.jackson.core</groupId>

            <artifactId>jackson-databind</artifactId>

            <version>2.9.8</version>

        </dependency>

杨__羊羊
浏览 91回答 2
2回答

ABOUTYOU

首先,您应该添加<packaging>war</packaging>到您的pom.xml中,但这不会导致错误。pom.xml<groupId>com.learning</groupId><artifactId>spring-web-no-boot</artifactId><version>1.0-SNAPSHOT</version><packaging>war</packaging>您正在使用两种不同的上下文。如果您仅使用根上下文,它应该可以工作。尝试以下 Application.java:public class Application implements WebApplicationInitializer {&nbsp; &nbsp; public void onStartup(ServletContext container) throws ServletException {&nbsp; &nbsp; &nbsp; &nbsp; AnnotationConfigWebApplicationContext context =&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new AnnotationConfigWebApplicationContext();&nbsp; &nbsp; &nbsp; &nbsp; context.setConfigLocation("configuration");&nbsp; &nbsp; &nbsp; &nbsp; container.addListener(new ContextLoaderListener(context));&nbsp; &nbsp; &nbsp; &nbsp; ServletRegistration.Dynamic dispatcher =&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; container.addServlet("dispatcher", new DispatcherServlet(context));&nbsp; &nbsp; &nbsp; &nbsp; dispatcher.setLoadOnStartup(1);&nbsp; &nbsp; &nbsp; &nbsp; dispatcher.addMapping("/");&nbsp; &nbsp; }}

潇湘沐

我认为下面的行可能是问题@ComponentScan(basePackages&nbsp;=&nbsp;"controller")你能试着把它改成@ComponentScan(basePackages&nbsp;=&nbsp;{"controller"})
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java