Spring Boot with JSP Integration
Spring Boot simplifies the development of web applications by offering support for JSP (Java Server Pages) for view rendering. This guide explains how to integrate JSP with Spring Boot, including configuration, setup, and usage examples.
1. Overview of JSP in Spring Boot
- JSP is a server-side view rendering technology for creating dynamic web pages.
- Spring Boot supports JSP, but requires some additional configuration compared to Thymeleaf.
2. Application Setup
2.1. Required Dependencies
Add the following dependencies in your pom.xml
file:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
<groupId>jakarta.servlet.jsp.jstl</groupId>
<artifactId>jakarta.servlet.jsp.jstl-api</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>jakarta.servlet.jsp.jstl</artifactId>
</dependency>
</dependencies>
2.2. Directory Structure
Ensure your JSP files are placed under the src/main/webapp/WEB-INF/views
directory.
src/
├── main/
│ ├── java/
│ ├── resources/
│ │ ├── application.properties
│ ├── webapp/
│ ├── WEB-INF/
│ ├── views/
│ ├── index.jsp
│ ├── about.jsp
2.3. Configuration in application.properties
Configure JSP view resolver properties:
spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp
3. Example Implementation
3.1. Controller
Create a Spring MVC controller to handle requests and return JSP views.
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class HomeController {
@GetMapping("/")
public String home(Model model) {
model.addAttribute("message", "Welcome to Spring Boot with JSP!");
return "index";
}
@GetMapping("/about")
public String about(Model model) {
model.addAttribute("info", "This is a JSP-based web application.");
return "about";
}
}
3.2. Sample JSP Files
index.jsp
<!DOCTYPE html>
<html>
<head>
<title>Home</title>
</head>
<body>
<h1>${message}</h1>
<p>Navigate to <a href="about">About</a> page.</p>
</body>
</html>
about.jsp
<!DOCTYPE html>
<html>
<head>
<title>About</title>
</head>
<body>
<h1>About Us</h1>
<p>${info}</p>
<a href="/">Go Back</a>
</body>
</html>
4. Running the Application
After completing the setup, run the Spring Boot application. Access the application at http://localhost:8080
to view the JSP pages.
5. Summary
Spring Boot supports JSP as a view technology, allowing developers to build dynamic web pages. While it requires additional setup, the flexibility and power of JSP can be leveraged effectively in a Spring Boot application.
Spring Boot JSP CRUD Application
This guide demonstrates how to build a CRUD (Create, Read, Update, Delete) application using Spring Boot and JSP. You'll learn how to set up the project, create a controller, JSP views, and perform CRUD operations.
1. Setup
1.1. Maven Dependencies
Add the required dependencies in your pom.xml
:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
<groupId>jakarta.servlet.jsp.jstl</groupId>
<artifactId>jakarta.servlet.jsp.jstl-api</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>jakarta.servlet.jsp.jstl</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
1.2. Configuration
Update the application.properties
file:
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp
2. Directory Structure
Your project should have the following structure:
src/
├── main/
│ ├── java/
│ │ ├── com.example.demo/
│ │ ├── controller/
│ │ ├── model/
│ │ ├── repository/
│ │ ├── service/
│ ├── resources/
│ │ ├── application.properties
│ ├── webapp/
│ ├── WEB-INF/
│ ├── views/
│ ├── list.jsp
│ ├── add.jsp
│ ├── edit.jsp
3. Implementation
3.1. Model
package com.example.demo.model;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
@Entity
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String department;
private Double salary;
// Getters and setters
}
3.2. Repository
package com.example.demo.repository;
import com.example.demo.model.Employee;
import org.springframework.data.jpa.repository.JpaRepository;
public interface EmployeeRepository extends JpaRepository<Employee, Long> {
}
3.3. Controller
package com.example.demo.controller;
import com.example.demo.model.Employee;
import com.example.demo.repository.EmployeeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@Controller
@RequestMapping("/employees")
public class EmployeeController {
@Autowired
private EmployeeRepository repository;
@GetMapping
public String listEmployees(Model model) {
List employees = repository.findAll();
model.addAttribute("employees", employees);
return "list";
}
@GetMapping("/add")
public String addEmployeeForm(Model model) {
model.addAttribute("employee", new Employee());
return "add";
}
@PostMapping
public String saveEmployee(@ModelAttribute Employee employee) {
repository.save(employee);
return "redirect:/employees";
}
@GetMapping("/edit/{id}")
public String editEmployeeForm(@PathVariable Long id, Model model) {
Employee employee = repository.findById(id).orElseThrow(() -> new IllegalArgumentException("Invalid employee Id:" + id));
model.addAttribute("employee", employee);
return "edit";
}
@PostMapping("/update/{id}")
public String updateEmployee(@PathVariable Long id, @ModelAttribute Employee employee) {
employee.setId(id);
repository.save(employee);
return "redirect:/employees";
}
@GetMapping("/delete/{id}")
public String deleteEmployee(@PathVariable Long id) {
repository.deleteById(id);
return "redirect:/employees";
}
}
3.4. JSP Views
list.jsp
<!DOCTYPE html>
<html>
<head>
<title>Employee List</title>
</head>
<body>
<h1>Employees</h1>
<a href="add">Add New Employee</a>
<table border="1">
<tr>
<th>ID</th>
<th>Name</th>
<th>Department</th>
<th>Salary</th>
<th>Actions</th>
</tr>
<c:forEach var="employee" items="${employees}">
<tr>
<td>${employee.id}</td>
<td>${employee.name}</td>
<td>${employee.department}</td>
<td>${employee.salary}</td>
<td>
<a href="edit/${employee.id}">Edit</a> |
<a href="delete/${employee.id}">Delete</a>
</td>
</tr>
</c:forEach>
</table>
</body>
</html>
add.jsp
<!DOCTYPE html>
<html>
<head>
<title>Add Employee</title>
</head>
<body>
<h1>Add Employee</h1>
<form action="/employees" method="post">
Name: <input type="text" name="name" required><br>
Department: <input type="text" name="department" required><br>
Salary: <input type="number" step="0.01" name="salary" required><br>
<button type="submit">Save</button>
</form>
</body>
</html>
edit.jsp
<!DOCTYPE html>
<html>
<head>
<title>Edit Employee</title>
</head>
<body>
<h1>Edit Employee</h1>
<form action="/employees/update/${employee.id}" method="post">
Name: <input type="text" name="name" value="${employee.name}" required><br>
Department: <input type="text" name="department" value="${employee.department}" required><br>
Salary: <input type="number" step="0.01" name="salary" value="${employee.salary}" required><br>
<button type="submit">Update</button>
</form>
</body>
</html>
4. Running the Application
Start the Spring Boot application and navigate to http://localhost:8080/employees
to manage employees.
5. Summary
Using Spring Boot with JSP, you can efficiently build CRUD applications with minimal configuration and clear separation of concerns.