Home > database >  How to use JSP in Spring Boot
How to use JSP in Spring Boot

Time:11-16

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.

CodePudding user response:

try in your .properties to change it into

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

if it still dont work i would like to see the error in console or the stack trace.

CodePudding user response:

Gotcha at "JSP Limitations";

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 with java -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:

  • to pom.xml:
    <project>
       ...
       <packaging>war</packaging>
       ...
    
  • to build.gradle:
    plugins {
       ...
       id 'war'
       ...
    }
    
  • or at https://start.spring.io/#!packaging=war (which also sets 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.

  • Related