当前位置: 首页 > 知识库问答 >
问题:

设置spring boot Servelet初始值设定项时遇到问题

祁彬
2023-03-14

我正在尝试使用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 =
                    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>

共有2个答案

越景天
2023-03-14

https://docs.spring.io/spring-boot/docs/current/reference/html/howto-tradition-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"
    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>war</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>
燕正卿
2023-03-14

请按照以下步骤在外部Tomcat上部署spring boot应用程序:

>

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

    <packaging>war</packaging>
    

    添加Tomcat依赖项:

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-tomcat</artifactId>
      <scope>provided</scope>
    </dependency>
    

    最后,我们通过实现SpringBootServletInitializer接口初始化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。下面的代码是关于学习静态初始值设定项块的。例外情况是thown和Capture,但程序仍在运行,我不确定原因。 输入:-1,2 预期输出:java.lang.例外:宽度和高度必须为正 实际输出:宽度和高度必须为正-2

    • 我对Swift类有一个问题。我有UITableViewController类和UITableViewCell类的swift文件。我的问题是UITableViewCell类和网点。这个类有一个错误Class“HomeCell”没有初始化程序,我不明白这个问题。 感谢您的回复。

    • 当安装完成并首次启动 Navicat Monitor 时,浏览器会弹出并打开你的 Navicat Monitor 的网址“http://<your_ip_address>:<port_number>”。你需要在欢迎页面完成 Navicat Monitor 的基本配置。 【注意】<your_host_address> 是安装了 Navicat Monitor 的系统的主机名,以及 <port_num

    • 我已经成功地设置了Elasticsearch、Kibana等,当我运行:'sudo systemctl status Elasticsearch'时,它都运行得很好。 但是,当我执行“sudo systemctl status logstash”时,这是输出: 它无法启动logstash,我在网上读了很多文章,说这可能与路径或配置有关,但我没有找到一个正确的工作解决方案。 我已经下载了JDK,并遵

    • 我开始使用钩子,我遇到了一个问题。 我有一个状态: 最初设置为,它在初始渲染时正确渲染。 但问题是,我希望它的值是我使用hook在初始渲染中获取的数组的长度 但是我不知道阵列的长度,直到组件安装完毕。 所以,在这个函数中,我试图访问priceToFilter,但它返回,因为这是它的初始值。 但是,如果您在中看到,我将通过函数将设置为另一个值。但是,当发生在初始化挂载之后时,它不会获得新值,而是保持

    • 可让PS Vita系统软件的设定回复初始设定,使主画面及开始画面皆回到至购买时的状态。 轻触[格式化]>[初始化设定]。请遵循画面指示正确操作。 重要 初始化设定后无法复原,请注意。 初始化中请勿关闭PS Vita的电源。初始化若遭到中断,可能会导致故障。 即使进行此操作,也无法让系统软件回到先前版本。