Business logic analysis of MVC design pattern (2)

1. Overall architecture
write picture description here

2. Multiple requests correspond to one Servlet
1). Servlet is mapped to *.do: it can receive all requests ending with .do

@WebServlet("*.do")
public class CustomerServlet extends HttpServlet

2). In doGet() and doPost() of Servlet

//1. 获取ServletPath:/edit4.do或者 /addCustomer.do
String servletPath = request.getServletPath();
//2. 去除 / 和 .do,得到类似于 edit 或者 addCustomer 这样的字符串
String methodName = servletPath.substring(1, servletPath.length()-3);

try {
    //3. 利用反射获取 methodName 对应的方法
    Method method = getClass().getDeclaredMethod(methodName, 
            HttpServletRequest.class, HttpServletResponse.class);
    //4. 调动反射对应的方法
    method.invoke(this, request, response);

} catch (Exception e) {
    //若要调用的方法不存在,响应 erro.jsp
    request.sendRedirect("/WEB-INF/erro.jsp");
}

3. Query: the entire process of MVC
query.do -> doPost -> query

code for query method

List<Customer> customers = customerDao.getListWithCriteria(cc);
request.setAttribute("customers", customers);
request.getRequestDispatcher("/index.jsp").forward(request, response);

jsp: Get the customers attribute in the request and traverse the display

<%
    List<Customer> customers = (List<Customer>) request.getAttribute("customers");
    if(customers != null && customers.size() > 0){
%>
<%
    for(Customer customer:customers){
%>      
<tr>
    <td><%=customer.getId()%></td>
    <td><%=customer.getName()%></td>
    <td><%=customer.getAddress()%></td>
    <td><%=customer.getPhone()%></td>
    <td>
    <a href = "edit.do?id=<%= customer.getId()%>">UPDATE </a>
    <a href = "delete.do?id=<%= customer.getId() %>" class = "delete">DELETE</a>
    </td>
</tr>

<% 
    }

4. Fuzzy query
1). Normal SQL:
String sql = “SELECT id, name, address, phone FROM customers WHERE id = ?”;
2). Tips for filling placeholders: take the name attribute as an example

public String getName() {
    if(name == null)
        name = "%%";
    else
        name = "%"+name+"%";
    return name;
}

3). Encapsulate the query condition as a JavaBean

public class CriteriaCustomer {
    private String name;
    private String address;
    private String phone;
    //....
    }

5. Complex validation of forms: validation needs to be done through the business layer, generally through the database server

6. Submit the form, go to the doPost() method of the Servlet, doPost() will forward it back to the original jsp page, until the page is loaded, there is only one HttpservletRequest object in the whole process.
write picture description here

There are two requests above:
①. The request to load the page, the request is sent until the page is loaded, and the request ends
. ②. Click the submit button to send a request to the servlet, and the servlet forwards it to newcustomer.jsp until the page is loaded. The whole process is a request

7. The echo problem of the form (understand)

<input type = "text" name = "name" 
value = "<%=request.getParameter("name")==null?"":request.getParameter("name") %>"/>

8. Modification process: first display the page, modify the fields, submit the form, modify
1). id generally uses hidden fields

<input type = 'hidden' name = "id" value = "<%=id%>"/>

2). Complex validation in modification, if a field does not allow duplicate solutions in the data table
①. Use hidden fields in the form to save the original value in the form

<input type = "hidden" name = "oldName" value = "<%=oldName%>"/>

②. Obtain the original value and the submitted value in the Servlet, compare, if they are consistent, pass, if not, use the newly submitted value area to query the data table

//2. 检验 name 是否已经被占用
//2.1 比较 name 和 oldName 是否相同,若相同 name 可用
//2.2 若不相同,则调用 CustomerDao 的 getCountWithName(String name) 获取name是否存在
if(!oldName.equalsIgnoreCase(name)) {
    long count = customerDao.getCountWithName(name);
    //2.2 若返回值 > 0 ,则响应 updatecustomer.jsp 界面
    //通过转发的方式来响应 updatecustomer.jsp
    if(count > 0) {
        //2.2.1 要求在 updatecustomer.jsp 页面提示一个错误消息:MIKE5 用户名已经被占用
        //在 request 中放入一个属性 message:用户名 name 已经被占用。
        //在页面上通过 request.getAttribute("message") 的方式来显示
        request.setAttribute("message", "用户名"+name+"已经被注册,请重新选择");
        //2.2.2 updatecustomer.jsp 的表单值可以回显
        //address, phone, 显示提交表单新的值,而 name 显示 oldName, 而不是新提交的 name

        //2.2.3 结束方法, return
        request.getRequestDispatcher("/updatecustomer.jsp").forward(request, response);
        return;
    }
}

3). In the modified state, if the verification fails, the form's echo is wrong, and repeated submissions are not allowed to give a prompt. The old field value is displayed in the field, and the modified one is allowed to be changed to a new value. Have a better user experience.

  1. The side effect of the response.sendRedirect() method prevents repeated submissions of the form.
  2. Interface-oriented programming:
private CustomerDAO customerDao = new CustomerDAOJdbcImpl();

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325682555&siteId=291194637