Skip to content
This repository has been archived by the owner on May 31, 2024. It is now read-only.

Commit

Permalink
linh fix add customer page when duplicate code and email
Browse files Browse the repository at this point in the history
  • Loading branch information
linhlm0210 committed Mar 8, 2024
1 parent 411b22b commit 929adad
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import jakarta.servlet.*;
import jakarta.servlet.annotation.*;
import jakarta.servlet.http.*;
import java.sql.SQLException;

@WebServlet(name = "AddCustomerServlet", urlPatterns = {"/add-customer"})
public class AddCustomerServlet extends HttpServlet {
Expand All @@ -30,18 +31,35 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
String address = request.getParameter("address");
String email = request.getParameter("email");

if (name.length() > 0 && code.length() > 0 && address.length() > 0 && email.length() > 0) {
try {
if (name.length() > 0 && code.length() > 0 && address.length() > 0 && email.length() > 0) {
Customer customer = new Customer();
customer.setName(name);
customer.setCode(code);
customer.setAddress(address);
customer.setEmail(email);
Singleton.customerDAO.save(customer);

response.sendRedirect(request.getContextPath() + "/customer");
return;
}
} catch (Exception e) {
Customer customer = new Customer();
customer.setName(name);
customer.setCode(code);
customer.setAddress(address);
customer.setEmail(email);
Singleton.customerDAO.save(customer);
StringBuilder message = new StringBuilder("Tạo khách hàng không thành công: ");
if (e.getMessage().contains(code)) {
message.append("Không được trùng mã khách hàng.");
}
if (e.getMessage().contains(email)) {
message.append("Không được trùng email.");
}
request.setAttribute("error", message.toString());
request.setAttribute("appendCustomer", customer);
RequestDispatcher requestDispatcher = request.getRequestDispatcher("/view/jsp/management/customer/add-customer.jsp");
requestDispatcher.forward(request, response);

response.sendRedirect(request.getContextPath() + "/customer");
return;
}

response.sendRedirect(request.getContextPath() + "/add-customer");
}
}
38 changes: 32 additions & 6 deletions src/main/webapp/view/jsp/management/customer/add-customer.jsp
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
<%@ page contentType="text/html" pageEncoding="UTF-8" %>

<%@page import="io.hardingadonis.saledock.utils.Singleton" %>

<%@page import="io.hardingadonis.saledock.model.Customer" %>
<%
String error = (String)request.getAttribute("error");
Customer customer = (Customer)request.getAttribute("appendCustomer");
%>
<!DOCTYPE html>
<html data-bs-theme="light" lang="en">

Expand Down Expand Up @@ -39,16 +43,38 @@
<form action="./add-customer" method="post">
<div class="row">
<div class="col">
<div class="mb-3"><label class="form-label" for="name"><strong>Tên khách hàng</strong></label><input class="form-control" type="text" id="name" placeholder="Nhập tên khách hàng" name="name"></div>
<div class="mb-3">
<label class="form-label" for="name">
<strong>Tên khách hàng</strong>
</label>
<input class="form-control" type="text" id="name" placeholder="Nhập tên khách hàng" name="name" value="<%= (error != null && customer != null) ? customer.getName() : ""%>">
</div>
</div>
<div class="col">
<div class="mb-3"><label class="form-label" for="code"><strong>Mã khách hàng</strong></label><input class="form-control" type="text" id="code" placeholder="Mã khách hàng" name="code"></div>
<div class="mb-3">
<label class="form-label" for="code">
<strong>Mã khách hàng</strong>
</label>
<input class="form-control" type="text" id="code" placeholder="Mã khách hàng" name="code">
</div>
</div>

</div>
<div class="mb-3">
<div class="mb-3"><label class="form-label" for="address"><strong>Địa chỉ</strong></label><input class="form-control" type="text" id="address" placeholder="Nhập địa chỉ của khách hàng" name="address"></div>
<div class="mb-3"><label class="form-label" for="email"><strong>Email</strong></label><input class="form-control" type="email" id="email" placeholder="Nhập email khách hàng" name="email"></div>

<div class="mb-3">
<div class="mb-3">
<label class="form-label" for="address">
<strong>Địa chỉ</strong>
</label>
<input class="form-control" type="text" id="address" placeholder="Nhập địa chỉ của khách hàng" name="address" value="<%= (error != null && customer != null) ? customer.getAddress() : ""%>"></div>
<div class="mb-3"><label class="form-label" for="email"><strong>Email</strong></label><input class="form-control" type="email" id="email" placeholder="Nhập email khách hàng" name="email"></div>
<% if(error != null) {
%>
<span class="form-message text-danger"><%=error%></span>
<%
}
%>
</div>
<a class="btn btn-primary btn-sm" href="customer">Quay lại</a>
<button class="btn btn-primary btn-sm" type="submit">Lưu lại</button>
</div>
Expand Down

0 comments on commit 929adad

Please sign in to comment.