'I'm trying to add a jsp page in my Spring Boot Aplication. My problem is that every time I try to go to that page I have this:

Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback.

Tue Apr 26 16:10:15 IRDT 2022 There was an unexpected error (type=Not Found, status=404). JSP file [/WEB-INF/jsp/home.jsp] not found

I have added the prefix and sufix into my application.properties:

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

This is my controller class:

@Controller
@RequestMapping("/")
public class HomeController {
@GetMapping("/")
public String index(){
    return "home";
}
}

and i have 1 warning in controller class :

Cannot resolve MVC view 'home'

my project folders picture

My Application class:

@SpringBootApplication
public class OnlineShopApplication {
public static void main(String[] args) {
SpringApplication.run(OnlineShopApplication.class, args);
}}

And the home.jsp:

<h1> hello world from jsp</h1>

pom.xml:

   <?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.5.13</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>largesize.shop.app</groupId>
<artifactId>OnlineShop</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>OnlineShop</name>
<description>Online shopping web application</description>
<properties>
    <java.version>11</java.version>
</properties>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </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>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
        <scope>provided</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>
</project>


Solution 1:[1]

When I opened the project, I opened the folder that contained the project, which was one roll higher than the main project folder.

Solution 2:[2]

Seems you are missing the template named home. You need to create this template and store it under \src\main\resources\templates The template is a html file to which you are returning your response from spring to. It should be something like this:

project-directory
->src
    ->main
        ->java->com->...etc
        ->resources
            ->static
            ->templates
                home.html
                other_file.html
            application.properties
      
    ->test

Also stop using specific versions. Use this:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

Also rollback to TomCat 9 as 10 is not supported.

<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
    <version>9.0.19</version>
</dependency>

You can get these dependencies from The MVN Repository

Solution 3:[3]

Change the following:

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

to:

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ali Bazargani
Solution 2
Solution 3 Omar Hussien