java - 设置 Spring Boot Servlet 初始化程序时遇到问题

标签 java spring spring-mvc spring-boot web

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

最终目标是我能够访问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 =
                    new DispatcherServlet(context);

            // register and configure the dispatcher servlet
            ServletRegistration.Dynamic registration =
                    servletContext.addServlet(DISPATCHER_SERVLET_NAME, dispatcherServlet);

            registration.setLoadOnStartup(1);
            registration.addMapping("/");
        } }

WebConfig.java

package com.project.LNUProject.config;

import com.project.LNUProject.utils.ViewNames;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.UrlBasedViewResolver;

@EnableWebMvc
@Configuration
@ComponentScan
@Slf4j
public class WebConfig implements WebMvcConfigurer {

    // == constants ==
    public static final String RESOLVER_PREFIX = "/WEB-INF/view/";
    public static final String RESOLVER_SUFFIX =".jsp";

    // == bean methods
    @Bean
    public ViewResolver viewResolver() {
        UrlBasedViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setPrefix(RESOLVER_PREFIX);
        viewResolver.setSuffix(RESOLVER_SUFFIX);
        return viewResolver;
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        log.info("registry add properly");
        registry.addViewController("/").setViewName(ViewNames.HOME);
    }
}

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.project</groupId>
    <artifactId>LNU-Project</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <modules>
        <module>Database</module>
        <module>WEB</module>
    </modules>
    <packaging>pom</packaging>

    <name>LNU-Project</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            **<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.2.0</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.cargo</groupId>
                <artifactId>cargo-maven2-plugin</artifactId>
                <version>1.6.7</version>
                <configuration>
                    <container>
                        <containerId>tomcat9x</containerId>
                        <type>embedded</type>
                    </container>
                </configuration>
            </plugin>
        </plugins>**

    </build>


</project>

最佳答案

请按照以下步骤在外部 tomcat 上部署 Spring Boot 应用程序:

  1. 需要打包 WAR 应用程序而不是 JAR。为此,我们使用以下内容更改 pom.xml:

    <packaging>war</packaging>
    
  2. 添加 Tomcat 依赖:

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-tomcat</artifactId>
      <scope>provided</scope>
    </dependency>
    
  3. 最后,我们通过实现SpringBootServletInitializer接口(interface)来初始化Tomcat所需的Servlet上下文:

     @SpringBootApplication
     public class Application extends SpringBootServletInitializer {
    
       @Override
       protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
          return application.sources(Application.class);
         }
    
       public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class, args);
       }
    
    }
    

关于java - 设置 Spring Boot Servlet 初始化程序时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52918751/

相关文章:

java - 如何保持插入顺序

ajax - 从Spring Controller 抛出自定义异常,并在ajax-post错误函数中接收它

java - 如何避免 Spring JPA 上的时区调整问题?

java - Spring - 如何从 spring Controller 中排除特定 URL?

jquery - Spring Mvc Jquery

java - 如何模糊图像

java - .clone() 还是 Arrays.copyOf()?

java - C3P0:池已满

java - 非 PK 列的 Hibernate ManyToOne JOIN 抛出 "Bad value for type int"

java - Spring MVC 与 OSGi - 如何注册新 Controller - 第二部分?