[英]Spring Boot - Whitelabel Error
I am building a spring boot application, while running project from eclipse, it works perfectly and page loads properly. 我正在构建一个Spring Boot应用程序,同时从eclipse运行项目时,它可以完美运行并且页面可以正确加载。 But When I do a maven build and generate JAR file and try to execute that, JSP pages are not loading, it shows Whitelabel error.
但是,当我执行maven构建并生成JAR文件并尝试执行该文件时,JSP页面未加载,它显示Whitelabel错误。
application.properties application.properties
spring.mvc.view.prefix: /WEB-INF/jsp/
spring.mvc.view.suffix: .jsp
Folder Structure 资料夹结构
pom.xml pom.xml
This project pom xml with all the depedencies 这个项目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 Start up class file 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);
}
}
Ensure that you have jasper and jstl in the list of dependencies: 确保依赖项列表中包含jasper和jstl:
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
Here is a working starter project - https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-boot-sample-web-jsp 这是一个正在工作的入门项目-https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-boot-sample-web-jsp
Try this: 尝试这个:
Move your .jsp file under this directory: src/main/resources/META-INF/resources/WEB-INF/jsp/ 将.jsp文件移动到以下目录中: src / main / resources / META-INF / resources / WEB-INF / jsp /
And your application.properties should be like this: [No change from yours] 您的application.properties应该是这样的:[您的不变]
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
Do a clean package before you run 运行前请做一个干净的包装
mvn clean package
java -jar path-to-jar
Also remove or comment scope from from tomcat-embed-jasper dependency 还要从tomcat-embed-jasper依赖项中删除或注释范围
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<!--<scope>provided</scope>-->
</dependency>
Note: This won't work from Spring Boot 2.0.1 and above because of a hard coded file pattern in Tomcat was removed 注意:由于删除了Tomcat中的硬编码文件模式,因此在Spring Boot 2.0.1及更高版本中将无法使用
OK. 好。 First thing first
首先第一件事
Placing dynamic contents like JSP pages into static locations does not work. 将动态内容(如JSP页面)放置到静态位置不起作用。
In a war project, JSP pages are served from src/main/webapp/WEB-INF/. 在战争项目中,JSP页面从src / main / webapp / WEB-INF /提供。
In a Jar project, JSP pages cannot simply be served from webapp location or from src/main/resources/. 在Jar项目中,不能简单地从webapp位置或src / main / resources /提供JSP页面。
That's because of the limitation stated in boot ref docs. 这是由于引导参考文档中所述的限制。
So you can add dynamic pages at following location src/main/resources/META-INF/resources/ 因此,您可以在以下位置src / main / resources / META-INF / resources /添加动态页面
and property file 和属性文件
spring.mvc.view.prefix= /WEB-INF/views/
spring.mvc.view.suffix= .jsp
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.