JavaWeb--MVC案例1-------(3)模糊查找
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"); } }