java - Spring Boot - 白标签错误
问题描述
我正在构建一个 Spring Boot 应用程序,在从 Eclipse 运行项目时,它运行良好并且页面加载正常。但是当我进行 maven 构建并生成 JAR 文件并尝试执行该文件时,JSP 页面没有加载,它显示 Whitelabel 错误。
应用程序属性
spring.mvc.view.prefix: /WEB-INF/jsp/
spring.mvc.view.suffix: .jsp
文件夹结构
pom.xml
这个项目 pom xml 具有所有依赖项
<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>testproject</groupId>
<artifactId>testproject</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>HB</name>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
<!-- <version>2.0.2.RELEASE</version> -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId>
</dependency> -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!-- <dependency> <groupId>io.jsonwebtoken</groupId> <artifactId>jjwt</artifactId>
<version>0.6.0</version> </dependency> -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>
<!-- <dependency> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version> </dependency> -->
<!-- <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope> </dependency> -->
<!-- <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId>
<version>1.2</version> </dependency> -->
<dependency>
<groupId>org.springframework.security.oauth</groupId>
<artifactId>spring-security-oauth2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-jwt</artifactId>
</dependency>
<!-- <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId>
</dependency> -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.2.11</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
<version>2.2.11</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.2.11</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>
<!-- <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId>
</dependency> -->
</dependencies>
<properties>
<!-- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> -->
<java.version>1.8</java.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<executable>true</executable>
</configuration>
</plugin>
</plugins>
</build>
</project>
Spring Boot 启动类文件
@SpringBootApplication
@ComponentScan(basePackages= {"com.testproject",
"com.testproject.controller"})
public class Application extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
解决方案
好的。第一件事
将 JSP 页面等动态内容放入静态位置是行不通的。
在一个战争项目中,JSP 页面是从 src/main/webapp/WEB-INF/ 提供的。
在 Jar 项目中,不能简单地从 webapp 位置或从 src/main/resources/ 提供 JSP 页面。
这是因为引导参考文档中所述的限制。
所以你可以在以下位置添加动态页面 src/main/resources/META-INF/resources/
和属性文件
spring.mvc.view.prefix= /WEB-INF/views/
spring.mvc.view.suffix= .jsp
请参阅此以获取更多详细信息
推荐阅读
- scala - 在 spark 1.6 中读取逗号分隔的文本文件
- wordpress - WooCommerce CSV 导出 - Excel 中的字符显示不正确
- laravel - 如何使用电子邮件和密码通过 Laravel 基本身份验证登录 Flutter 应用程序
- c# - 尝试使用 Tpm2Lib 创建主键时出现 BadAuth 响应
- android - 如何使用 DateTimeFormatter 转换为字符串
- javascript - 如何将焦点设置在 Vuejs 2 中 v-for 内呈现的 textarea 上?
- gerrit - 如何限制对自定义 gerrit 插件的访问
- apache-spark - 如何修复“BlockManagerMasterEndpoint - 没有更多副本可用于 rdd”问题?
- clips - 对一组事实 CLIPS 进行排序
- java - 为什么将信息从 Java 代码更新到 SQL 数据库时会出现错误?