Integrating JSP with Spring Boot Applications
This guide demonstrates how to incorporate JSP (JavaServer Pages) into a Spring Boot project as an alternative view technology.
- Dependency Configuration First, remove any existing view technology dependencies (like Thymeleaf) and add these JSP-specific dependencies to your pom.xml:
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<scope>provided</scope>
</dependency>
- JSP Page Setup Create your JSP files in the /src/main/webapp/views directory. Here's a sample login.jsp that displays data from the controller:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>User Profile</title>
</head>
<body>
Welcome: ${userName}
</body>
</html>
- View Configuration Configure the view resolver in application.properties:
spring.mvc.view.prefix=/views/
spring.mvc.view.suffix=.jsp
- Controller Implementation Create a controller that serves the JSP view with model data:
@Controller
@RequestMapping("/auth")
public class AuthController {
@GetMapping("/profile")
public String showProfile(Model model) {
model.addAttribute("userName", "JohnDoe");
return "login";
}
}
- Testing the Integration Acces the endpoint at http://localhost:8080/auth/profile to verify the JSP rendering.