设置 Spring Boot Servelet Initializer 时遇到问题

我正在尝试使用 Spring Boot 设置一个简单的 Web CRUD 应用程序。我知道我应该是 spring boot servelet 初始值设定项,但是在日志中,我怀疑它没有正常运行,因为它没有记录我写的内容。


最终目标是让我能够访问http://localhost:8080/LNU-Project/和 home.jsp 显示。


这是 github 上的链接。https://github.com/rjpruitt16/LNU-Project/tree/master/src/main


WebAppInitializer.java


package com.project.LNUProject;


    import com.project.LNUProject.config.WebConfig; import lombok.extern.slf4j.Slf4j; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; import org.springframework.web.servlet.DispatcherServlet;


    import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.ServletRegistration;


    @Slf4j 

    @SpringBootApplication 

    public class WebAppInitializer extends SpringBootServletInitializer {

        private static final String DISPATCHER_SERVLET_NAME = "dispatcher";


        public static void main(String[] args) {

            SpringApplication.run(WebAppInitializer.class, args);

        }


        @Override

        protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {

            return application.sources(WebAppInitializer.class);

        }


        @Override

        public void onStartup(ServletContext servletContext) throws ServletException {

            log.info("onStartUp");


            // create the spring application context

            AnnotationConfigWebApplicationContext context =

                    new AnnotationConfigWebApplicationContext();

            context.register(WebConfig.class);


            // create the dispatcher servlet

            DispatcherServlet dispatcherServlet =


        } }


慕哥6287543
浏览 168回答 2
2回答

慕神8447489

请按照以下步骤在外部 tomcat 上部署 Spring Boot 应用程序:需要打包一个 WAR 应用程序而不是一个 JAR。为此,我们将 pom.xml 更改为以下内容:<packaging>war</packaging>添加Tomcat依赖:<dependency>&nbsp; <groupId>org.springframework.boot</groupId>&nbsp; <artifactId>spring-boot-starter-tomcat</artifactId>&nbsp; <scope>provided</scope></dependency>最后,我们通过实现SpringBootServletInitializer接口来初始化Tomcat所需的Servlet上下文:&nbsp;@SpringBootApplication&nbsp;public class Application extends SpringBootServletInitializer {&nbsp; &nbsp;@Override&nbsp; &nbsp;protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {&nbsp; &nbsp; &nbsp; return application.sources(Application.class);&nbsp; &nbsp; &nbsp;}&nbsp; &nbsp;public static void main(String[] args) throws Exception {&nbsp; &nbsp; SpringApplication.run(Application.class, args);&nbsp; &nbsp;}}

阿波罗的战车

https://docs.spring.io/spring-boot/docs/current/reference/html/howto-traditional-deployment.html该文档明确指出您必须在 pom.xml中将包装标签从pom更改为war<?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"&nbsp; &nbsp; xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">&nbsp; &nbsp; <modelVersion>4.0.0</modelVersion>&nbsp; &nbsp; <groupId>com.project</groupId>&nbsp; &nbsp; <artifactId>LNU-Project</artifactId>&nbsp; &nbsp; <version>0.0.1-SNAPSHOT</version>&nbsp; &nbsp; <modules>&nbsp; &nbsp; &nbsp; &nbsp; <module>Database</module>&nbsp; &nbsp; &nbsp; &nbsp; <module>WEB</module>&nbsp; &nbsp; </modules>&nbsp; &nbsp; **<packaging>war</packaging>**&nbsp; &nbsp; <name>LNU-Project</name>&nbsp; &nbsp; <description>Demo project for Spring Boot</description>&nbsp; &nbsp; <parent>&nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.springframework.boot</groupId>&nbsp; &nbsp; &nbsp; &nbsp; <artifactId>spring-boot-starter-parent</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; <version>2.0.6.RELEASE</version>&nbsp; &nbsp; &nbsp; &nbsp; <relativePath/> <!-- lookup parent from repository -->&nbsp; &nbsp; </parent>&nbsp; &nbsp; <properties>&nbsp; &nbsp; &nbsp; &nbsp; <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>&nbsp; &nbsp; &nbsp; &nbsp; <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>&nbsp; &nbsp; &nbsp; &nbsp; <java.version>1.8</java.version>&nbsp; &nbsp; </properties>&nbsp; &nbsp; <dependencies>&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.springframework.boot</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>spring-boot-starter-jdbc</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.springframework.boot</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>spring-boot-starter-web</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>com.h2database</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>h2</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <scope>runtime</scope>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.projectlombok</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>lombok</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <optional>true</optional>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.springframework.boot</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>spring-boot-starter-test</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <scope>test</scope>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; </dependencies>&nbsp; &nbsp; <build>&nbsp; &nbsp; &nbsp; &nbsp; <plugins>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <plugin>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.springframework.boot</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>spring-boot-maven-plugin</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </plugin>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <plugin>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.apache.maven.plugins</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>maven-war-plugin</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <version>3.2.0</version>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <configuration>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <failOnMissingWebXml>false</failOnMissingWebXml>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </configuration>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </plugin>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <plugin>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.codehaus.cargo</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>cargo-maven2-plugin</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <version>1.6.7</version>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <configuration>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <container>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <containerId>tomcat9x</containerId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <type>embedded</type>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </container>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </configuration>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </plugin>&nbsp; &nbsp; &nbsp; &nbsp; </plugins>&nbsp; &nbsp; </build></project>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java