1.index.jsp
1.獲取 request 中的 customers 屬性
2.遍歷顯示
<%@ page import="MVCCases.Customer" %> <%@ page import="java.util.List" %><%-- Created by IntelliJ IDEA. User: Skye Date: 2017/12/8 Time: 9:45 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>$Title$</title> </head> <body> <form action="query.do", method="post "> <table> <tr> <td>Name:</td> <td><input type="text", name="name"/></td> </tr> <tr> <td>Address:</td> <td><input type="text", name="address"/></td> </tr> <tr> <td>Phone:</td> <td><input type="text", name="phone"</td> </tr> <tr> <td><input type="submit", value="Query"/></td> <td><a href="">Create New Customer</a></td> </tr> </table> </form> <br><br> <% List<Customer> customers = (List<Customer>)request.getAttribute("customers"); if(customers != null && customers.size() != 0){ %> <br><br> <hr> <table border="1" cellpadding="10" cellspacing="0"> <tr><%--tr表示行,td表示列,th表示表頭--%> <th>ID</th> <th>NAME</th> <th>Address</th> <th>Phone</th> <th>Delete/Update</th> </tr> <% for(Customer customer: customers){ %> <tr> <td><%= customer.getId()%></td> <td><%= customer.getName()%></td> <td><%= customer.getAddress()%></td> <td><%= customer.getPhone()%></td> <td> <a href="">UPDATE</a> <a href="">DELETE</a> </td> </tr> <% } %> </table> <% } %> </body> </html>
查詢操作:
1.Servlet
//1. 調用 CustomerDAO 的 getAll() 得到 Customer 的集合
List<Customer> customers = customerDAO.getAll();
//2. 把 Customer 的集合放入 request 中
request.setAttribute("customers", customers);
//3. 轉發頁面到 index.jsp(不能使用重定向)
request.getRequestDispatcher("/index.jsp").forward(request, response);
2.JSP
獲取 request 中的 customers 屬性
遍歷顯示
模糊查詢
根據傳入的 name, address, phone 進行模糊查詢
例子: name: a、address: b、phone: 3 則 SQL 語句的樣子為: SELECT id, name, address, phone FROM customers WHERE name LIKE ‘%a%’ AND address LIKE ‘%b%’ AND phone LIKE ‘%3%’
需要在 CustomerDAO 接口中定義一個 getForListWithCriteriaCustomer(CriteriaCustomer cc)。 其中 CriteriaCustomer 用於封裝查詢條件:name, address, phone。因為查詢條件很多時候和 domain 類並不相同,所以要做成一個單獨的類
拼 SQL:
SQL: "SELECT id, name, address, phone FROM customers WHERE " +
"name LIKE ? AND address LIKE ? ANDphone LIKE ?";
為了正確的填充占位符時,重寫了 CriteriaCustomer 的 getter:
修改 Servlet:獲取請求參數;把請求參數封裝為
CriteriaCustomer 對象,再調用 getForListWithCriteriaCustomer(CriteriaCustomer cc) 方法
CriteriaCustomer 中
package MVCCases; public class CriteriaCustomer { private String name; private String address; private String phone; public String getName() { if(name == null){ name = "%%"; }else{ name = "%" + name + "%"; } return name; } public void setName(String name) { this.name = name; } public String getAddress() { if(address == null){ address = "%%"; }else{ address = "%" + address + "%"; } return address; } public void setAddress(String address) { this.address = address; } public String getPhone() { if(phone == null){ phone = "%%"; }else{ phone = "%" + phone + "%"; } return phone; } public void setPhone(String phone) { this.phone = phone; } public CriteriaCustomer(String name, String address, String phone) { this.name = name; this.address = address; this.phone = phone; } }
CustomerDAO中
/** * 返回滿足查詢條件的List * @param cc:封裝了查詢條件 * @return */ public List<Customer> getForListWithCriteriaCustomer(CriteriaCustomer cc);
CustomerDAOImpl中
@Override public List<Customer> getForListWithCriteriaCustomer(CriteriaCustomer cc) { String sql = "SELECT id, name, address, phone FROM customer WHERE " + "name LIKE ? AND address LIKE ? AND phone LIKE ?"; //修改了CriteriaCustomer的getter()方法,使其返回字符串中有%%(模糊查詢) //若返回值為null,則返回“%%”, 否則返回"%" + value + "%" return getForList(sql, cc.getName(), cc.getAddress(), cc.getPhone()); }
CustomerServ中的轉發與查詢
package MVCCases; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.lang.reflect.Method; import java.util.List; public class CustomerServlet extends HttpServlet { private CustomerDAO customerDAO = new CustomerDAOImpl(); @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //super.doGet(req, resp); doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //super.doPost(req, resp); //1.獲取servletPath:/add.do 或 /delete.do等 String servletPath = req.getServletPath(); //2.取出/和.do得到方法名 String methodName = servletPath.substring(1); methodName = methodName.substring(0, methodName.length() - 3); //3.利用反射獲取methodName對應的方法 Method method = null; try { method = getClass().getDeclaredMethod(methodName, HttpServletRequest.class, HttpServletResponse.class); //4.利用發射調用對應的方法 method.invoke(this, req, resp); } catch (Exception e) { e.printStackTrace(); resp.sendRedirect("error.jsp"); } } private void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("add"); } private void query(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //獲取模糊查詢的請求參數 String name = req.getParameter("name"); String address = req.getParameter("address"); String phone = req.getParameter("phone"); //把請求參數封裝為一個CriteriaCustomer對象 CriteriaCustomer cc = new CriteriaCustomer(name, address, phone); //1.調用CustomerDAOImpl類的getForListWithCriteriaCustomer方法 List<Customer> customers = customerDAO.getForListWithCriteriaCustomer(cc); //2.把customers放入req中 req.setAttribute("customers", customers); //3.轉發到index.jsp req.getRequestDispatcher("/index.jsp").forward(req, resp); } private void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("delete"); } }