gpt4 book ai didi

java - 打包到 Apache Tomcat 的 WAR 时配置 Web 资源的位置

转载 作者:行者123 更新时间:2023-11-28 22:23:54 25 4
gpt4 key购买 nike

我试图在不使用 Spring Boot 中的嵌入式服务器的情况下将我的 Web 应用程序部署到 Apache Tomcat。我已经按照指南中的所有步骤成功部署了应用程序(我的意思是它在 Tomcat 管理器中找到),但是当我转到 webapp url 时,我得到了 404。

问题是我的 HTML 文件位于 webapps/mywebapp/WEB-INF/classes。当我将 HTML 文件移动到简单的 webapps/mywebapp 时,页面成功加载,但没有我的 javascript 文件或图像文件等资源。使用 spring boot 嵌入式服务器运行应用程序时,webapp 工作正常。这是我的项目结构:

enter image description here

我的 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>io.johnnathan.springboot</groupId>
<artifactId>product-store</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>

<name>product-store</name>
<description>Product storefront using Spring Boot</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.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-thymeleaf</artifactId>

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

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

</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>

</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>4.1.3</version>

</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.3.1</version>

</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.12</version><!--$NO-MVN-MAN-VER$-->

</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.0.8.RELEASE</version><!--$NO-MVN-MAN-VER$-->

</dependency>
</dependencies>

<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>


</project>

通过 Apache Tomcat 部署时解压的 WAR 文件的文件结构(包含在产品商店 (mywebapp) 文件夹中:

+---META-INF
| | MANIFEST.MF
| | war-tracker
| |
| \---maven
| \---io.johnnathan.springboot
| \---product-store
| pom.properties
| pom.xml
|
+---org
| \---springframework
| \---boot
| \---loader
| | ExecutableArchiveLauncher.class
| | JarLauncher.class
| | LaunchedURLClassLoader$UseFastConnectionExceptionsEnumeration.class
| | LaunchedURLClassLoader.class
| | Launcher.class
| | MainMethodRunner.class
| | PropertiesLauncher$1.class
| | PropertiesLauncher$ArchiveEntryFilter.class
| | PropertiesLauncher$PrefixMatchingArchiveFilter.class
| | PropertiesLauncher.class
| | WarLauncher.class
| |
| +---archive
| | Archive$Entry.class
| | Archive$EntryFilter.class
| | Archive.class
| | ExplodedArchive$1.class
| | ExplodedArchive$FileEntry.class
| | ExplodedArchive$FileEntryIterator$EntryComparator.class
| | ExplodedArchive$FileEntryIterator.class
| | ExplodedArchive.class
| | JarFileArchive$EntryIterator.class
| | JarFileArchive$JarFileEntry.class
| | JarFileArchive.class
| |
| +---data
| | RandomAccessData.class
| | RandomAccessDataFile$1.class
| | RandomAccessDataFile$DataInputStream.class
| | RandomAccessDataFile$FileAccess.class
| | RandomAccessDataFile.class
| |
| +---jar
| | AsciiBytes.class
| | Bytes.class
| | CentralDirectoryEndRecord.class
| | CentralDirectoryFileHeader.class
| | CentralDirectoryParser.class
| | CentralDirectoryVisitor.class
| | FileHeader.class
| | Handler.class
| | JarEntry.class
| | JarEntryFilter.class
| | JarFile$1.class
| | JarFile$2.class
| | JarFile$JarFileType.class
| | JarFile.class
| | JarFileEntries$1.class
| | JarFileEntries$EntryIterator.class
| | JarFileEntries.class
| | JarURLConnection$1.class
| | JarURLConnection$JarEntryName.class
| | JarURLConnection.class
| | StringSequence.class
| | ZipInflaterInputStream.class
| |
| \---util
| SystemPropertyUtils.class
|
\---WEB-INF
+---classes
| | application.properties
| |
| +---io
| | \---johnnathan
| | \---springboot
| | | ProductStoreApp.class
| | |
| | +---index
| | | IndexController.class
| | |
| | \---product
| | Product.class
| | ProductController.class
| | ProductDAO$1.class
| | ProductDAO$ProductRowMapper.class
| | ProductDAO.class
| | ProductService.class
| |
| +---static
| | +---css
| | | bootstrap.min.css
| | | custom.css
| | |
| | +---images
| | | | lights.jpg
| | | | mountain.jpg
| | | |
| | | \---svg
| | | house-with-wifi-svgrepo-com.svg
| | |
| | \---js
| | addProduct.js
| | bootstrap.min.js
| | custom.js
| | jquery-3.3.1.js
| |
| \---templates
| addProduct.html
| index.html
| productPage.html
| products.html
|
+---lib
| accessors-smart-1.2.jar
| android-json-0.0.20131108.vaadin1.jar
| asm-5.0.4.jar
| assertj-core-3.9.1.jar
| attoparser-2.0.4.RELEASE.jar
| bootstrap-4.1.3.jar
| byte-buddy-1.7.11.jar
| byte-buddy-agent-1.7.11.jar
| hamcrest-core-1.3.jar
| hamcrest-library-1.3.jar
| HikariCP-2.7.9.jar
| javax.annotation-api-1.3.2.jar
| jquery-3.3.1.jar
| json-path-2.4.0.jar
| json-smart-2.3.jar
| jsonassert-1.5.0.jar
| jul-to-slf4j-1.7.25.jar
| junit-4.12.jar
| log4j-api-2.10.0.jar
| log4j-to-slf4j-2.10.0.jar
| logback-classic-1.2.3.jar
| logback-core-1.2.3.jar
| mockito-core-2.15.0.jar
| mysql-connector-java-8.0.12.jar
| objenesis-2.6.jar
| popper.js-1.14.3.jar
| protobuf-java-2.6.0.jar
| slf4j-api-1.7.25.jar
| snakeyaml-1.19.jar
| spring-aop-5.0.8.RELEASE.jar
| spring-beans-5.0.8.RELEASE.jar
| spring-boot-2.0.4.RELEASE.jar
| spring-boot-autoconfigure-2.0.4.RELEASE.jar
| spring-boot-starter-2.0.4.RELEASE.jar
| spring-boot-starter-jdbc-2.0.4.RELEASE.jar
| spring-boot-starter-logging-2.0.4.RELEASE.jar
| spring-boot-starter-test-2.0.4.RELEASE.jar
| spring-boot-starter-thymeleaf-2.0.4.RELEASE.jar
| spring-boot-test-2.0.4.RELEASE.jar
| spring-boot-test-autoconfigure-2.0.4.RELEASE.jar
| spring-context-5.0.8.RELEASE.jar
| spring-core-5.0.8.RELEASE.jar
| spring-expression-5.0.8.RELEASE.jar
| spring-jcl-5.0.8.RELEASE.jar
| spring-jdbc-5.0.8.RELEASE.jar
| spring-test-5.0.8.RELEASE.jar
| spring-tx-5.0.8.RELEASE.jar
| thymeleaf-3.0.9.RELEASE.jar
| thymeleaf-extras-java8time-3.0.1.RELEASE.jar
| thymeleaf-spring5-3.0.9.RELEASE.jar
| unbescape-1.1.5.RELEASE.jar
| xmlunit-core-2.5.1.jar
|
\---lib-provided
classmate-1.3.4.jar
hibernate-validator-6.0.11.Final.jar
jackson-annotations-2.9.0.jar
jackson-core-2.9.6.jar
jackson-databind-2.9.6.jar
jackson-datatype-jdk8-2.9.6.jar
jackson-datatype-jsr310-2.9.6.jar
jackson-module-parameter-names-2.9.6.jar
javafx.base-11.0.0-SNAPSHOT-linux.jar
javafx.base-11.0.0-SNAPSHOT-mac.jar
javafx.base-11.0.0-SNAPSHOT-win.jar
javafx.base-11.0.0-SNAPSHOT.jar
jboss-logging-3.3.2.Final.jar
spring-boot-starter-json-2.0.4.RELEASE.jar
spring-boot-starter-tomcat-2.0.4.RELEASE.jar
spring-boot-starter-web-2.0.4.RELEASE.jar
spring-web-5.0.8.RELEASE.jar
spring-webmvc-5.0.8.RELEASE.jar
tomcat-embed-core-8.5.32.jar
tomcat-embed-el-8.5.32.jar
tomcat-embed-websocket-8.5.32.jar
validation-api-2.0.1.Final.jar

我是否必须将所有网络资源移动到不同的文件结构?如果是这样,部署为 WAR 时正确的文件结构是什么?

最佳答案

问题出在我的 pom.xml 上。

依赖:

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

自带嵌入式服务器,所以我把scope设置为“provided”。但是,此依赖项中还需要其他一些东西才能正确部署为 WAR。所以我将此依赖项更改为:

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

关于java - 打包到 Apache Tomcat 的 WAR 时配置 Web 资源的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58056289/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com