I\'m trying to add a jsp page in my Spring Boot service. My problem is that every time I try to go to that page I have this:
Whitelabel Error Page
To have this in pom.xml
<!-- JSP -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<!-- jstl for jsp -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
It may be not enough.
You must not miss this.
<packaging>war</packaging>
Otherwise when you build the package, you will get as a jar file and that does not have JSP nor the embedded tomcat.
See runable example and its explanation here https://www.surasint.com/spring-boot-jsp/
If you are using IDEA development tools, then you can try specifying
Configurations -> Configuration -> environment -> Working directory
The value in $MODULE_DIR$
In addition to the answers above the application needs to be deployed as war instead jar
<groupId>com.igt</groupId>
<artifactId>customer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
to run
java -jar customer-0.0.1-SNAPSHOT.war
Also If you intend to start your application as a war or as an executable application, you need to share the customizations of the builder in a method that is both available to the SpringBootServletInitializer callback and the main method, something like
package com.igt.customer;
import java.util.Arrays;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class CustomerApplication extends org.springframework.boot.web.support.SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(CustomerApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(CustomerApplication.class, args);
}
@Bean
public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
return args -> {
System.out.println("Let's inspect the beans provided by Spring Boot:");
String[] beanNames = ctx.getBeanDefinitionNames();
Arrays.sort(beanNames);
for (String beanName : beanNames) {
System.out.println(beanName);
}
};
}
}
Please see
this
Spring MVC offers no default (fall-back) error page out-of-the-box. The most common way to set a default error page has always been the SimpleMappingExceptionResolver
(since Spring V1 in fact). However Spring Boot also provides for a fallback error-handling page.
At start-up, Spring Boot tries to find a mapping for /error
. By convention, a URL ending in /error
maps to a logical view of the same name: error
. Generally this view maps in turn to the error.html
Thymeleaf template. (If using JSP, it would map to error.jsp
according to the setup of your
InternalResourceViewResolver).
Spring Boot will automatically use and configure Thymeleaf as the view rendering engine, as long as it's on the classpath.
Thymeleaf with Maven:
Make sure you have Maven 3 installed with the following command: mvn --version. Navigate to the directory you want to create your project in and execute Maven archtetype:
mvn archetype:generate -DarchetypeArtifactId=maven-archetype-quickstart -DgroupId=pl.codeleak.demos.sbt -DartifactId=spring-boot-thymeleaf -interactiveMode=false
The above command will create a new directory spring-boot-thymeleaf. Now you can import it to your IDE. The next step is to configure the application. Open pom.xml and add a parent project:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.1.8.RELEASE</version>
</parent>
Values from the parent project will be the default for this project if they are left unspecified. The next step is to add web dependencies. In order to do so, I firstly removed all previous dependencies (junit 3.8.1 actually) and added the below dependencies:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
Now, wait a second until Maven downloads the dependencies and run mvn dependency:tree to see what dependencies are included. The next thing is a packaging configuration. Let's add Spring Boot Maven Plugin:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
Thymeleaf with Gradle:
To put Thymeleaf on the classpath use
compile("org.springframework.boot:spring-boot-starter-thymeleaf")
in the gradle build file (using the relevant maven dependency is straightforward).
In your case in order to display the index.jsp
view (in accordance to the controller you are using), you need to place it under src/main/resources/templates/
.
If no mapping from /error to a View can be found, Spring Boot defines its own fall-back error page - the so-called Whitelabel Error Page
(a minimal page with just the HTTP status information and any error details, such as the message from an uncaught exception).
This is working solution for me about White label error page : Cannot find view page(jsp)
At POM.xml, Make sure packaging is "war" and add tomcat/jasper dependencies
<packaging>war</packaging>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
Add prefix/suffix at application.properties
spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp
** If you use Intellij, you must set Web Resource directories. At Project Structure (ctrl+alt+shift+ S) > Facets > Select Web(your application) > Add(+) Web Resource Directories ( mine is ......\src\main\webapp)
** If you have multiple modules(At intellij), Run> Edit configuration> Select springboot your application > Configuration tab> Working directory as $MODULE_WORKING_DIR$
My issue was that I was using @RestController instead of @Controller as the annotation in my controller class. Hope this can help someone out.