Configured Spring Boot according to a guide, still it cannot find my jsp views. So after launching I get this message "This application has no explicit mapping for /error, so you are seeing this as a fallback."
Any suggestions?
pom:
<?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.7.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.lib.secondtry</groupId>
<artifactId>MyLibrary</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>MyLibrary</name>
<description>MyLibrary</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<version>9.0.44</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<version>9.0.44</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</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>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Properties:
spring.mvc.view.prefix=/webapp/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
Controller:
@Controller
public class FirstController {
@GetMapping("/")
public String sayHello(){
return "hello";
}
}
Console:
2022-11-15 23:09:26.879 INFO 11896 --- [ restartedMain] c.l.s.mylibrary.MyLibraryApplication : No active profile set, falling back to 1 default profile: "default"
2022-11-15 23:09:26.907 INFO 11896 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2022-11-15 23:09:26.908 INFO 11896 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2022-11-15 23:09:27.369 INFO 11896 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2022-11-15 23:09:27.374 INFO 11896 --- [ restartedMain] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2022-11-15 23:09:27.375 INFO 11896 --- [ restartedMain] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.68]
2022-11-15 23:09:27.495 INFO 11896 --- [ restartedMain] org.apache.jasper.servlet.TldScanner : At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
2022-11-15 23:09:27.500 INFO 11896 --- [ restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2022-11-15 23:09:27.501 INFO 11896 --- [ restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 593 ms
2022-11-15 23:09:27.663 INFO 11896 --- [ restartedMain] o.s.b.d.a.OptionalLiveReloadServer : LiveReload server is running on port 35729
2022-11-15 23:09:27.694 INFO 11896 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2022-11-15 23:09:27.700 INFO 11896 --- [ restartedMain] c.l.s.mylibrary.MyLibraryApplication : Started MyLibraryApplication in 1.008 seconds (JVM running for 1.342)
2022-11-15 23:09:43.705 INFO 11896 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2022-11-15 23:09:43.705 INFO 11896 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2022-11-15 23:09:43.706 INFO 11896 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 1 ms
In main I created webapp/WEB-INF/jsp/ and it didn't help.
When running a Spring Boot application that uses an embedded servlet container (and is packaged as an executable archive), there are some limitations in the JSP support.
With Jetty and Tomcat, it should work if you use
war
packaging. An executable war will work when launched withjava -jar
, and will also be deployable to any standard container. JSPs are not supported when using an executable jar.Undertow does not support JSPs.
Creating a custom
error.jsp
page does not override the default view for error handling. Custom error pages should be used instead.
In maven (if omit) default packaging is jar
, so please add:
pom.xml
:
<project>
...
<packaging>war</packaging>
...
build.gradle
:
plugins {
...
id 'war'
...
}
spring-boot-starter-tomcat
to prvoided
scope (i.e. excludes it from packaging/prepares for external deployment))(With all of its consequences.)
Please Also Note "Tip":
If possible, JSPs should be avoided. There are several known limitations when using them with embedded servlet containers.
I was able to get JSPs to work with an executable JAR by using https://stackoverflow.com/a/50846032/2137125, part of https://stackoverflow.com/a/68619247/2137125, and https://dzone.com/articles/spring-boot-with-jsps-in-executable-jars-1.
But as other answers in the first link say, the supported way seems to be using an executable war, i.e. you can java -jar something.war
, although I didn't try it, maybe because I'm stubborn.
Edit 2024: I've since migrated off of JSP on a JAR as we started having random pages not loading, the pages would change after each restart, or sometimes no pages would be affected.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With