Transitioning to Spring MVC

From Architectural Limitations to a "Happy Workflow"

1. The Concept: From "Many Servlets" to "One Dispatcher"

In your first lab, one Java class mapped to one URL[cite: 5]. However, imagine building a CRM system[cite: 6]. A CRM requires creating, editing, viewing, and deleting customers, leads, and tasks[cite: 7].

The "Servlet Exhaustion" Problem

Using original Servlets, your web.xml becomes a nightmare[cite: 9]:

  • CreateCustomerServlet.java -> /customer/create [cite: 10]
  • EditCustomerServlet.java -> /customer/edit [cite: 11]
  • DeleteCustomerServlet.java -> /customer/delete [cite: 12]

Managing 50+ Servlets leads to massive configuration files and "Boilerplate Code" that is hard to maintain and prone to errors[cite: 14].

2. The Nightmare of Manual Data Binding

The most difficult part of raw Servlet development is capturing data from a web form[cite: 16]. Developers must manually extract every field from the HttpServletRequest[cite: 18].

String name = request.getParameter("name"); [cite: 19]
String email = request.getParameter("email"); [cite: 20]
// Dangerous! What if the user leaves "age" empty? [cite: 21]
int age = Integer.parseInt(request.getParameter("age")); // Crashes on non-numeric input [cite: 23]

Handling all type conversions and validation manually leads to "Spaghetti Code"[cite: 24, 25].

3. The Spring + Thymeleaf "Happy Workflow"

Spring MVC introduces the DispatcherServlet (Front Controller)[cite: 27]. One servlet receives everything and delegates work to "Controllers"[cite: 28].

Automatic Data Binding

You no longer use request.getParameter(). Spring automatically "binds" form data into a Java Object[cite: 31].

@PostMapping("/save")
public String saveCustomer(@ModelAttribute Customer customer) { [cite: 32, 33]
    // Spring has already filled 'customer' with data! [cite: 34]
    return "redirect:/list"; [cite: 35]
}

Thymeleaf Integration: Acts as the bridge, keeping HTML clean without Java code (<% ... %>) inside the page[cite: 37, 38].

1. Khái niệm: Từ "Nhiều Servlet" đến "Một Dispatcher"

Trong bài lab đầu tiên, một lớp Java ánh xạ tới một URL[cite: 5]. Tuy nhiên, hãy tưởng tượng việc xây dựng một hệ thống CRM[cite: 6]. Một CRM yêu cầu tạo, chỉnh sửa, xem và xóa khách hàng, khách hàng tiềm năng và nhiệm vụ[cite: 7].

Vấn đề "Servlet Exhaustion" (Kiệt sức vì Servlet)

Sử dụng Servlet gốc, file web.xml của bạn sẽ trở thành một "cơn ác mộng"[cite: 9]:

  • CreateCustomerServlet.java -> /customer/create [cite: 10]
  • EditCustomerServlet.java -> /customer/edit [cite: 11]
  • DeleteCustomerServlet.java -> /customer/delete [cite: 12]

Quản lý hơn 50 Servlet dẫn đến các tệp cấu hình khổng lồ và "Boilerplate Code" (code lặp lại) khiến dự án khó bảo trì và dễ mắc lỗi[cite: 14].

2. Ác mộng về liên kết dữ liệu thủ công

Phần khó nhất của phát triển Servlet thuần là thu thập dữ liệu từ web form[cite: 16]. Nhà phát triển phải trích xuất thủ công từng trường từ HttpServletRequest[cite: 18].

String name = request.getParameter("name"); [cite: 19]
String email = request.getParameter("email"); [cite: 20]
// Nguy hiểm! Nếu người dùng để trống trường "age"? [cite: 21]
int age = Integer.parseInt(request.getParameter("age")); // Gây crash nếu nhập không phải số [cite: 23]

Việc xử lý thủ công tất cả các chuyển đổi kiểu và xác thực dẫn đến "Spaghetti Code"[cite: 24, 25].

3. "Quy trình hạnh phúc" với Spring + Thymeleaf

Spring MVC giới thiệu DispatcherServlet (Front Controller)[cite: 27]. Thay vì nhiều servlet, một servlet nhận mọi thứ và ủy quyền công việc cho các "Controller"[cite: 28].

Liên kết dữ liệu tự động (Automatic Data Binding)

Bạn không còn sử dụng request.getParameter(). Spring tự động "liên kết" dữ liệu form vào một đối tượng Java[cite: 31].

@PostMapping("/save")
public String saveCustomer(@ModelAttribute Customer customer) { [cite: 32, 33]
    // Spring đã điền dữ liệu từ form vào 'customer'! [cite: 34]
    return "redirect:/list"; [cite: 35]
}

Tích hợp Thymeleaf: Đóng vai trò là cầu nối, giữ cho HTML sạch sẽ mà không có mã Java (<% ... %>) bên trong trang[cite: 37, 38].

Comparison Summary [cite: 39]

Feature [cite: 40] Original Servlet + JSP [cite: 40] Spring MVC + Thymeleaf [cite: 40]
Routing One Servlet per URL (Complex web.xml) [cite: 40] One Controller handles many URLs (Clean) [cite: 40]
Data Capture Manual request.getParameter() [cite: 40] Automatic @ModelAttribute binding [cite: 40]
Validation Manual if/else checks [cite: 40] Automatic @Valid annotations [cite: 40]
Logic High effort, repetitive "Boilerplate" [cite: 40] "Happy" workflow, fast and logical [cite: 40]

5. Pre-Lab Quiz (Self-Check) [cite: 41]

Ensure you can answer these before starting Lab 02[cite: 42]:

  1. Why is a large web.xml file considered a disadvantage in big projects like a CRM? [cite: 43]
  2. What happens in a Native Servlet if a user enters text into a numeric "Age" field? [cite: 44]
  3. What is the primary role of the DispatcherServlet? [cite: 45]