我使用
Spring Boot
开发了一个全栈 thymeleaf
应用程序,它在本地运行良好,没有任何问题。
现在我尝试将其部署到
AWS cloud
,但它给了我错误[内部服务器错误]
我检查了关系数据库并尝试从应用程序连接,它工作正常。
它似乎存在的唯一问题是 创建 jar 文件时无法解析模板并且没有渲染任何模板。
当我启动应用程序时,它会将 HttpRequest 发送到“/”,它会像这样渲染主页模板,但失败了:
// displays home page
@GetMapping("/")
public String homePage(Model model) {
model.addAttribute("title", "Home Page - Information Keeper");
return "home"; // return home.html page from templates folder
}
我在资源/模板/主页下有所有模板[等等]
我的
properties file
是
server.port = 5000
spring.thymeleaf.enabled=true
spring.thymeleaf.cache=false
spring.thymeleaf.prefix=classpath:/templates/
spring.datasource.url=jdbc:postgresql://RDS_Endpoint:5432/myinfokeeper
spring.datasource.username=username
spring.datasource.password=password
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQL81Dialect
spring.jpa.show-sql = true
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto = update
我的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 https://maven.apache.org/xsd/maven-
4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>info.keeper</groupId>
<artifactId>info_keeper</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Info_Keeper</name>
<description>Information Keeper using Spring boot, JPA and Thymeleaf with Spring Security.</description>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</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-data-jpa</artifactId>
</dependency>
<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>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<!-- use postgres sql -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</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>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.1.5.Final</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
<version>2.5.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${project.parent.version}</version>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
<configuration>
<useSystemClassLoader>false</useSystemClassLoader>
<goal>repackage</goal>
</configuration>
</plugin>
</plugins>
<finalName>InfoKeeperWebApp</finalName>
</build>
我到底应该做什么?为什么它无法使用 jar 解析这些模板,但在 localhost 上却可以完美运行?
我在 stackoverflow 上查看并尝试了许多解决方案,其中许多解决方案似乎在 localhost 上有问题,但我的在 localhost 上运行良好,但 不在 jar 上,导致 AWS beanstalk 出现错误。 我已经被困了好几天了:)
如有任何帮助,我们将不胜感激。
我的 home.html 看起来像这样:
<!doctype html>
<html lang="en" xmlns:th="http://www.thymeleaf.org"
th:replace="base::layout(~{::div})">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>My Information Keeper || Home Page</title>
</head>
<body>
<div class="home d-flex justify-content-center align-items-center">
<section>
<h3>Keep Your Information Secure on Web</h3>
<h4>Recall Anywhere Anytime</h4>
<a class="btn btn-warning btn-lg text-white" href="/register">Get Started</a>
</section>
<script>
let items = document.getElementsByClassName("item");
<!-- alert(items.length); -->
<!-- remove active class first -->
for(i in items) {
<!-- alert(items[i].className)-->
if(items[i].className === 'item active') {
items[i].classList.remove('active');
}
}
const homeLink = document.getElementById("home-link");
homeLink.classList.add("active");
</script>
</div>
</body>
</html>
您可以尝试使用这些配置,并确保您的 HTML 文件包含在“templates”文件夹中,而不是问题中的“template”文件夹中
spring.thymeleaf.check-template=true
spring.thymeleaf.check-template-location=true
我在使用时遇到了同样的错误: 所以我将代码更改为: 一切都完成了