[Thực hành] Form và Data Binding
NỘI DUNG BÀI VIẾT
Mục tiêu
Luyện tập sử dụng form và data binding trong Spring MVC.
Mô tả
Trong phần này, chúng ta sẽ phát triển một tính năng nhỏ của ứng dụng quản lý nhân viên.
Tính năng này cho phép nhập vào dữ liệu của một nhân viên và hiển thị dữ liệu đó.
Hướng dẫn
Bước 1: Tạo project đặt tên spring-mvc-form-tutorial
Cấu trúc ứng dụng như sau:

Bước 2: Cài đặt lớp Employee
Ở đây chú ý là sử dụng javax.validation và org.hibernate để kiểm tra id, name, contactNumber không được phép để trống hoặc null. Lý do sẽ được trình bày ở phần sau.
package model; public class Employee { private String id; private String name; private String contactNumber; public Employee() { } public Employee(String id, String name, String contactNumber) { this.id = id; this.name = name; this.contactNumber = contactNumber; } public String getId() { return id; } public String getName() { return name; } public String getContactNumber() { return contactNumber; } public void setId(String id) { this.id = id; } public void setName(String name) { this.name = name; } public void setContactNumber(String contactNumber) { this.contactNumber = contactNumber; } }
Bước 3: Cài đặt lớp EmployeeController
URI | Method | Action |
/showform | GET | Hiển thị trang tạo nhân viên |
/addEmployee | POST | Gọi phương thức thêm mới nhân viên |
Mã nguồn cụ thể
package controller; import model.Employee; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller @RequestMapping("employee/") public class EmployeeController { @RequestMapping(value = "showform", method = RequestMethod.GET) public String showForm(ModelMap model) { model.addAttribute("employee", new Employee()); return "employee/create"; } @RequestMapping(value = "/addEmployee", method = RequestMethod.POST) public String submit(@ModelAttribute("employee") Employee employee, BindingResult result, ModelMap model) { model.addAttribute("name", employee.getName()); model.addAttribute("contactNumber", employee.getContactNumber()); model.addAttribute("id", employee.getId()); return "employee/info"; } }
Bước 4: Cài đặt file create.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> <html> <head> <title>Create a new employee</title> </head> <body> <h3>Welcome, Enter The Employee Details</h3> <form:form method="POST" action="addEmployee" modelAttribute="employee"> <table> <tr> <td><form:label path="id">Employee ID: </form:label></td> <td><form:input path="id"/></td> </tr> <tr> <td><form:label path="name">Employee's name: </form:label></td> <td><form:input path="name"/></td> </tr> <tr> <td><form:label path="contactNumber">Contact's number: </form:label></td> <td><form:input path="contactNumber"/></td> </tr> <tr> <td><input type="submit" value="Submit"/></td> </tr> </table> </form:form> </body> </html>
Bước 5: Cài đặt file info.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Infor</title> </head> <body> <h2>Submitted Employee Information</h2> <table> <tr> <td>Name :</td> <td>${name}</td> </tr> <tr> <td>ID :</td> <td>${id}</td> </tr> <tr> <td>Contact Number :</td> <td>${contactNumber}</td> </tr> </table> </body> </html>
Bước 6: Thực thi và chạy ứng
Mã nguồn tham khảo tại: https://github.com/codegym-vn/spring-mvc-form-tutorial
Leave a Reply