3

Can't load a very simple JSP page with spring-boot, getting 404 Not Found.

src/main/java/SampleWebJspApplication.java

@Configuration
@EnableAutoConfiguration
@ComponentScan
public class SampleWebJspApplication extends SpringBootServletInitializer {
    public static void main(String[] args) throws Exception {
        SpringApplication.run(SampleWebJspApplication.class, args);
    }
}

src/main/java/WebController.java

@Controller
public class WebController {
    @RequestMapping("/")
    public String welcome() {
        return "welcome";
    }
}

src/main/webapp/WEB-INF/jsp/welcome.jsp

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

Getting 404 even though debugger shows that "welcome" is being returned from the Controller RequestMapping.

Whitelabel Error Page

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

Sat Mar 07 19:35:15 EST 2015 There was an unexpected error (type=Not Found, status=404).

szxnyc
  • 2,495
  • 5
  • 35
  • 46

4 Answers4

8

I have already tried many solutions, but couldn't find a working one.

If you are using Intellij IDEA and reading this while you are pulling your hair: do not try to run Spring Boot application(with dynamic .jsp views) with the IDE's run ▶︎ button.

$ cd {your_project_folder_path}

$ ls //Check if you are in the same place with the pom.xml

and then type

$ mvn spring-boot:run

Now your application is served at localhost:8080.

gokcand
  • 6,694
  • 2
  • 22
  • 38
  • that means that problem is with your build tool, not IDE itself – fg78nc Jul 23 '17 at 14:28
  • 1
    @fg78nc Please look at https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-developing-web-applications.html#boot-features-jsp-limitations – gokcand Jul 23 '17 at 14:30
4

Normally Spring boot doesnot require any configuration besides adding dependency while working with Thymeleaf.

To run JSP in Spring Boot, you have to have some configurations:

Dependencies needed:

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

If you are connecting with MySQL Database then:

 <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>

In Application.Properties file:

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

Now your Good to go !!! :D

suman heuju
  • 121
  • 3
  • I was missing the dependency. Thanks for the help. Also, `javax.servlet` was giving an error. Removing it also worked so I think it is not necessary. – Eatsam ul haq Dec 26 '22 at 09:03
2

In latest spring versions, the properties to be put in application.properties file look like following:

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

Note "mvc" included in "spring.view.prefix"

Also, in newer versions of Spring, there is an annotation @SpringBootApplication for @Configuration, @EnableAutoConfiguration, @ComponentScan

noufalcep
  • 3,446
  • 15
  • 33
  • 51
Ajitesh
  • 956
  • 10
  • 14
1

I needed to add this to my application.properties file:

spring.view.prefix: /WEB-INF/jsp/
spring.view.suffix: .jsp
szxnyc
  • 2,495
  • 5
  • 35
  • 46