Reputation: 354
I have a Spring Boot web application that reads index.jsp and uses tomcat jasper to support JSP. However, the page is showing up as "There was an unexpected error (type=Not Found, status=404)." I suspect either the issue lies with the dependency.
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.3.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.Alex</groupId>
<artifactId>WebApp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>WebApp</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>14</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-jasper -->
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jasper</artifactId>
<version>9.0.35</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
Hello this shit works
</body>
</html>
The method runs as the print statement executes
@Controller
public class indexController {
// Tomcat Jasper to return jsp files
@RequestMapping("index")
public String getIndex() {
System.out.println("test1");
return "index.jsp";
}
File structure
Upvotes: 0
Views: 924
Reputation: 1
if you are using spring starter project then that won't find the path you have to create new project as "dynamic web project" then you will get the folder web-inf and inside you can create views and the set path in properties file ... spring.mvc.view.prefix = /WEB-INF/views/ spring.mvc.view.suffix =.jsp
Upvotes: 0
Reputation: 5460
Add this dependency with other ( like web ) in your pom file.
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
In application.properties file add this config properties for file location (Your .jsp file location in class path)
Properties config:
spring.mvc.view.prefix = /WEB-INF/views/
spring.mvc.view.suffix =.jsp
spring.mvc.static-path-pattern=/resources/**
OR
Class level config:
@Configuration
@EnableWebMvc
@ComponentScan(basePackages= {"com.imran"})
public class WebConfig implements WebMvcConfigurer {
public void configureViewResolvers(ViewResolverRegistry registry) {
registry.jsp().prefix("/WEB-INF/views/").suffix(".jsp");
}
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}
}
and your controller will be like this (with out .jsp extension, just file name):
@Controller
public class indexController {
@RequestMapping(value = "/index", method = RequestMethod.GET)
public String getIndex() {
System.out.println("test1");
return "index";
}
}
You can get some help from here if you need.
Upvotes: 1