From Architectural Limitations to a "Happy Workflow"
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].
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].
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].
Handling all type conversions and validation manually leads to "Spaghetti Code"[cite: 24, 25].
Spring MVC introduces the DispatcherServlet (Front Controller)[cite: 27]. One servlet receives everything and delegates work to "Controllers"[cite: 28].
You no longer use request.getParameter(). Spring automatically "binds" form data into a Java Object[cite: 31].
Thymeleaf Integration: Acts as the bridge, keeping HTML clean without Java code (<% ... %>) inside the page[cite: 37, 38].
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].
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].
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].
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].
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].
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].
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].
| 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] |
Ensure you can answer these before starting Lab 02[cite: 42]:
web.xml file considered a disadvantage in big projects like a CRM? [cite: 43]