Please enable JavaScript.
Coggle requires JavaScript to display documents.
CRM, Search, Get method, Post method, Update customer, Add Customer,…
CRM
-
Controller
RequestMapping("/customer")
public class CustomerController {
// inject Customer Service
Autowired
private CustomerService customerService;
RequestMapping("/list")
public String listCustomer (Model theModel) {
// Lấy đối tượng customers từ Customer Service.
List<Customer> theCustomers = customerService.getCustomers();
// Thêm đối tượng customer vào đối tượng model
theModel.addAttribute("customers", theCustomers);
return "list-customers";}}
-
-
list-customers.jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%><link type="text/css"
rel="stylesheet" href="${pageContext.request.contextPath}/resources/css/style.css"/><table>
<tr>
<th>First Name</th>
<th>Last Name</th>
</tr>
<c:forEach var="tempCustomer" items="${customers}">
<tr> <td>${tempCustomer.firstName}</td> <td>${tempCustomer.lastName}</td>
</tr>
</c:forEach>
</table>
-
-
-
-
Entity
Table(name = "customer")
public class Customer {
create field, constructor, getter/setter}
-
-
-
Search
list-customers.jsp
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<form:form action="search" method="GET">
Search customer: <input type="text" name="theSearchName" />
<input type="submit" value="Search" class="add-button" />
</form:form>
CustomerController.java
GetMapping("/search")
public String searchCustomers(@RequestParam("theSearchName") String theSearchName, Model theModel) {
// search customers from the service
List<Customer> theCustomers = customerService.searchCustomers(theSearchName);
// add the customers to the model
theModel.addAttribute("customers", theCustomers);
return "list-customers";
-
CustomerServiceImpl.java
Override
Transactional
public List<Customer> searchCustomers(String theSearchName) {
return customerDAO.searchCustomers(theSearchName);}
-
CustomerDAOImpl.java
Override
public List<Customer> searchCustomers(String theSearchName) {
Session currentSession = sessionFactory.getCurrentSession();
Query theQuery = null;
if (theSearchName != null && theSearchName.trim().length() > 0) {
theQuery =currentSession.createQuery("from Customer where lower(firstName) like :theName or lower(lastName) like :theName", Customer.class);
theQuery.setParameter("theName", "%" + theSearchName.toLowerCase() + "%");}
else {
theQuery =currentSession.createQuery("from Customer", Customer.class);}
List<Customer> customers = theQuery.getResultList();
return customers;}
-
-
Update customer
update file Java
public class CustomerController {
PostMapping("showFormForUpdate")
// "@RequestParam" với giá trị là "customerId": dữ liệu được gửi với tên tham số là "customerId" sẽ được gắn kết với biến "theId" của phương thức
// theModel: thêm các thuộc tính vào mô hình dữ liệu được truyền sang view
public String showFormForUpdate(@RequestParam ("customerId") int theId, Model theModel) {
// get the customer from service
Customer theCustomer = customerService.getCustomer(theId);
// set customer as a model attribute to pre-populate in the form
theModel.addAttribute("customer", theCustomer);
return "customer-form";}
public class CustomerServiceImpl implements CustomerService {
Override
Transactional
public Customer getCustomer(int theId) {
return customerDAO.getCustomer(theId);}
public class CustomerDAOImpl implements CustomerDAO {
Override
// get the current session
Session currentSession = sessionFactory.getCurrentSession();
// read from db using primary key
Customer theCustomer = currentSession.get(Customer.class, theId);
return theCustomer;}
-
Update customer-list.jsp
<c:forEach var="tempCustomer" items="${customers}">
<c:url var="updateLink" value="/customer/showFormForUpdate">
<c:param name="customerId" value="${tempCustomer.id}"/>
</c:url>
// display the update link
<td><a href ="${updateLink}">Update</a></td>
-
-
-
Delete customer
-
Add Delete link in JSP
<c:url
var="deleteLink" value="/customer/delete">
<c:param name="customerId" value="${tempCustomer.id}"/>
</c:url>
<a href ="${deleteLink}"
onclick="if(!(confirm('Are you sure you want to delete this customer?'))) return false">Delete</a></td>
-