Spring MVC Tutorial
Core Spring MVC
Spring MVC - Annotation
Spring MVC - Form Handling
Spring MVC with JSTL
Spring MVC with REST API
Spring MVC with Database
A CRUD (Create, Read, Update, Delete) application is one of the most common types of web applications. Let's set up a basic Spring MVC application for managing books.
You'd need several dependencies to set this up, including Spring Web MVC, JPA for database interaction, a database driver, and a Spring Data JPA for repository support. Here's a basic set of dependencies for a pom.xml
:
<!-- Spring Web MVC --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.3.10</version> </dependency> <!-- Spring Data JPA --> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-jpa</artifactId> <version>2.5.5</version> </dependency> <!-- H2 Database (for demo purposes) --> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <version>1.4.200</version> <scope>runtime</scope> </dependency> <!-- JPA Implementation (Hibernate) --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>5.5.7.Final</version> </dependency>
Create a Book
entity.
@Entity public class Book { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String title; private String author; private String isbn; // Getters, setters, and other necessary methods }
Create a BookRepository
interface.
public interface BookRepository extends JpaRepository<Book, Long> { }
Implement a service layer for CRUD operations.
@Service public class BookService { @Autowired private BookRepository bookRepository; public List<Book> getAllBooks() { return bookRepository.findAll(); } public Book getBookById(Long id) { return bookRepository.findById(id).orElse(null); } public Book saveOrUpdateBook(Book book) { return bookRepository.save(book); } public void deleteBook(Long id) { bookRepository.deleteById(id); } }
Implement the controller for CRUD operations.
@Controller @RequestMapping("/books") public class BookController { @Autowired private BookService bookService; @GetMapping public String listBooks(Model model) { model.addAttribute("books", bookService.getAllBooks()); return "bookList"; } @GetMapping("/edit/{id}") public String editBook(@PathVariable Long id, Model model) { model.addAttribute("book", bookService.getBookById(id)); return "bookForm"; } @GetMapping("/new") public String newBook(Model model) { model.addAttribute("book", new Book()); return "bookForm"; } @PostMapping("/save") public String saveBook(@ModelAttribute Book book) { bookService.saveOrUpdateBook(book); return "redirect:/books"; } @GetMapping("/delete/{id}") public String deleteBook(@PathVariable Long id) { bookService.deleteBook(id); return "redirect:/books"; } }
You'll need JSPs for listing the books (bookList.jsp
) and the book form (bookForm.jsp
). Use form tags and other standard JSP tags to create and edit books.
Ensure you have configurations set up for Spring MVC, JPA (with a data source), and component scanning for services, controllers, and repositories. This can be done either through XML or Java configuration.
Remember, this is a basic setup. In a real-world scenario, you'd want to add error handling, validation, transactions, and other necessary features.
Spring MVC CRUD Example:
Description: This is a basic Spring MVC CRUD application without any specific persistence mechanism. It demonstrates the fundamental Create, Read, Update, and Delete operations using Spring MVC.
Code Snippet: (Assuming you have set up your controllers, services, and JSP views)
// Controller @Controller @RequestMapping("/items") public class ItemController { // CRUD methods } // JSP View <!-- item.jsp --> <form action="/items/save" method="post"> <!-- form fields for item properties --> </form>
Building a CRUD Application with Spring MVC:
Description: This example focuses on building a comprehensive CRUD application using Spring MVC, covering aspects such as validation, exception handling, and user interfaces.
Code Snippet: (Part of the controller)
// Controller @Controller @RequestMapping("/products") public class ProductController { // CRUD methods with validation and exception handling }
Creating a Simple CRUD Application using Spring MVC:
Description: This is a minimalistic Spring MVC CRUD application, emphasizing simplicity and ease of understanding. It's suitable for beginners who want to grasp the basics of Spring MVC CRUD.
Code Snippet: (Controller and JSP)
// Controller @Controller @RequestMapping("/tasks") public class TaskController { // Basic CRUD methods } // JSP View <!-- task.jsp --> <form action="/tasks/save" method="post"> <!-- form fields for task properties --> </form>
Spring MVC CRUD Operations with Hibernate:
Description: This example extends the basic Spring MVC CRUD to incorporate Hibernate for data persistence. It demonstrates the integration of Spring MVC with the Hibernate ORM framework.
Code Snippet: (Entity and Repository)
// Entity @Entity public class Book { // Entity properties and annotations } // Repository public interface BookRepository extends JpaRepository<Book, Long> { // Custom queries if needed }
CRUD Operations in Spring MVC and JSP:
Description: This example specifically uses JSP (JavaServer Pages) for the user interface. It showcases how to integrate Spring MVC with JSP for building a CRUD application.
Code Snippet: (JSP View)
<!-- product.jsp --> <form action="/products/save" method="post"> <!-- form fields for product properties --> </form>
RESTful CRUD with Spring MVC Example:
Description: This example focuses on building a RESTful CRUD application using Spring MVC. It employs HTTP methods (GET, POST, PUT, DELETE) for different CRUD operations.
Code Snippet: (Controller with RESTful mappings)
// Controller @RestController @RequestMapping("/api/books") public class BookRestController { // RESTful CRUD methods }
Spring MVC CRUD Application Step by Step:
Description: This tutorial guides you through building a Spring MVC CRUD application step by step. It's designed for those who prefer a detailed, tutorial-style approach.
Code Snippet: (Controller method from a step)
// Controller @Controller @RequestMapping("/employees") public class EmployeeController { // Step-specific CRUD methods }
Spring MVC CRUD with Thymeleaf Example:
Description: This example uses Thymeleaf as the templating engine for the user interface. It illustrates how to integrate Thymeleaf with Spring MVC for building a CRUD application.
Code Snippet: (Thymeleaf template)
<!-- employee.html --> <form action="/employees/save" method="post" th:object="${employee}"> <!-- Thymeleaf attributes for binding form fields --> </form>