Spring Boot JSP 404

JavaSpringJspSpring Mvc

Java Problem Overview


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 > > This application has no explicit mapping for /error, so you are seeing > this as a fallback. > > Tue Apr 21 23:16:00 EEST 2015 There was an unexpected error (type=Not > Found, status=404). No message available

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
public class MarkerController {
	@RequestMapping(value="/map")
	public String trafficSpy() {
		return "index";
	}
}

My Application class:

@SpringBootApplication
public class Application extends SpringBootServletInitializer {
	private static Logger logger = Logger.getLogger(Application.class.getName());
		
	public static void main(String[] args) {
		 	logger.info("SPRING VERSION: " + SpringVersion.getVersion());
	        SpringApplication.run(Application.class, args);
	    }
}

And the index.jsp:

<!DOCTYPE html>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html lang="en">

<body>
    <h1>Hello, World!!!</h1>


    <p>JSTL URL: ${url}</p>
</body>

</html>

And this is the src file structure:

├── src
│   ├── main
│   │   ├── java
│   │   │   └── com
│   │   │       └── example
│   │   │           └── internetprogramming
│   │   │               └── myserver
│   │   │                   └── server
│   │   │                       ├── Application.java
│   │   │                       ├── config
│   │   │                       │   └── DatabaseConfig.java
│   │   │                       ├── controller
│   │   │                       │   └── MarkerController.java
│   │   │                       ├── dao
│   │   │                       │   ├── MarkerDaoImplementation.java
│   │   │                       │   └── MarkerDaoInterface.java
│   │   │                       ├── Marker.java
│   │   │                       └── service
│   │   │                           ├── MarkerServiceImplementation.java
│   │   │                           └── MarkerServiceInterface.java
│   │   ├── resources
│   │   │   └── application.properties
│   │   └── webapp
│   │       └── WEB-INF
│   │           └── jsp
│   │               └── index.jsp

Java Solutions


Solution 1 - Java

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

Solution 2 - Java

In newer versions of Spring, following needs to be put in application.properties file:

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

Also, JSP files need to be put under src/main/resources/META-INF/resources/WEB-INF/jsp

Solution 3 - Java

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$

Solution 4 - Java

We were adding Spring Boot to our system in order to run it as executable application without standalone tomcat and also faces the 404 status during JSP initialization.
What should be done for fixing it:

a) Add dependencies to your pom file (WARNING: tomcat-embed-jasper must have compile scope no provided):

<parent>
 <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-parent</artifactId>
     <version>2.3.3.RELEASE</version>
     <relativePath/>
 </parent>
 <dependencies>
   <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-web</artifactId>
   </dependency>
   <dependency>
     <groupId>javax.servlet</groupId>
     <artifactId>jstl</artifactId>
   </dependency>
   <dependency>
     <groupId>org.apache.tomcat.embed</groupId>
     <artifactId>tomcat-embed-jasper</artifactId>
   </dependency>
 <dependencies>

b) Add spring boot maven plugin for building your application:

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

c) Check that you are using *.war file for your artifact, not jar (because of JSP support limitation):

<packaging>war</packaging>

e) Now you should be able to run your spring boot application using maven plugin or with command line - java -jar /you/path/application-name.war: enter image description here

f) But if you are using multi-module maven project and want to run spring boot application using IntelliJ Idea it is important to change "Working directory" -> $MODULE_DIR$:
enter image description here

Solution 5 - Java

If you are using IDEA development tools, then you can try specifying

Configurations -> Configuration -> environment -> Working directory

The value in $MODULE_DIR$

Solution 6 - Java

My issue was that I was using @RestController instead of @Controller as the annotation in my controller class. Hope this can help someone out.

Solution 7 - Java

my issue was Spring vesrion : I found that since 1.4.3 version and above stops supporting the embedded JSPs . So I change version to 1.4.1 , it's worked for me.

an other thing take off :

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

it will not work with it .

Solution 8 - Java

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

Solution 9 - Java

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).

Solution 10 - Java

You can use thymeleaf with jsp but you have to write:

spring.thymeleaf.excluded-view-names=#jsp file without extension

in application.properties file

Solution 11 - Java

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/

Solution 12 - Java

For me, it seemed to be an eclipse issue. I was able to start the springboot app using java -jar outside the IDE.

Solution 13 - Java

Make sure your .jsp page is under WebContent:

WebContent|-
          |-Jsp
             |-home.jsp

Solution 14 - Java

Try and add your error jsp files under error folder.

application.properties
spring.mvc.view.prefix=/views/jsp/
spring.mvc.view.suffix=.jsp

jsp files :
/views/jsp/error/401.jsp
/views/jsp/error/404.jsp - to display 404 instead of default whitelabel page

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondephineraView Question on Stackoverflow
Solution 1 - JavaBiju KunjummenView Answer on Stackoverflow
Solution 2 - JavaAjiteshView Answer on Stackoverflow
Solution 3 - JavaKyung Hwan MinView Answer on Stackoverflow
Solution 4 - JavaMaksymView Answer on Stackoverflow
Solution 5 - JavamercuryView Answer on Stackoverflow
Solution 6 - JavajovanchohanView Answer on Stackoverflow
Solution 7 - JavaOmar B.View Answer on Stackoverflow
Solution 8 - JavaSam2016View Answer on Stackoverflow
Solution 9 - JavaMCHAppyView Answer on Stackoverflow
Solution 10 - JavaKike LebowskiView Answer on Stackoverflow
Solution 11 - JavaSurasin TancharoenView Answer on Stackoverflow
Solution 12 - JavaPraveesh PView Answer on Stackoverflow
Solution 13 - JavaAvinash KhadsanView Answer on Stackoverflow
Solution 14 - JavaGovindaRajuView Answer on Stackoverflow