0

I am building a spring boot application, while running project from eclipse, it works perfectly and page loads properly. 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.

application.properties

spring.mvc.view.prefix: /WEB-INF/jsp/
spring.mvc.view.suffix: .jsp

Folder Structure

enter image description here

Error enter image description here

pom.xml

This project pom xml with all the depedencies

<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

@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);
}
}
Jay Vignesh
  • 374
  • 3
  • 11

3 Answers3

0

Ensure that you have jasper and jstl in the list of dependencies:

<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

Bharat Satija
  • 362
  • 1
  • 13
0

Try this:

Move your .jsp file under this directory: src/main/resources/META-INF/resources/WEB-INF/jsp/

And your application.properties should be like this: [No change from yours]

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

  <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

Refer this for more detail

Akhil Menon
  • 306
  • 1
  • 9
  • I have did it, it works perfectly inside targer foler, when i move the jar file to my desktop and execute it, again the white label error occuring – Jay Vignesh Jul 20 '18 at 05:08
0

OK. First thing first

Placing dynamic contents like JSP pages into static locations does not work.

In a war project, JSP pages are served from src/main/webapp/WEB-INF/.

In a Jar project, JSP pages cannot simply be served from webapp location or from src/main/resources/.

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/

and property file

spring.mvc.view.prefix= /WEB-INF/views/
spring.mvc.view.suffix= .jsp

Refer this for more detail

MyTwoCents
  • 7,284
  • 3
  • 24
  • 52