实现javaWeb对数据库的增删改查-MVC开发思想-Controller层之模糊查询

  • 前端Web页面index.jsp

<%@page import="com.njupt.javaweb.business.Customer"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form action="query.do" method="post">
		<table>
			<tr>
				<td>名称:</td>
				<td><input type="text" name="name"/></td>
			</tr>
			<tr>
				<td>地址:</td>
				<td><input type="text" name="address"/></td>
			</tr>
			<tr>
				<td>手机号:</td>
				<td><input type="text" name="phone"/></td>
			</tr>
			<tr>
				<td><input type="submit" value="提交"/></td>
				<td><a href="">创建用户</a></td>
			</tr>
		</table>
	</form>
	<br/>
	<br/>
	<hr/>
	<br/>
	<br/>
	<% 
		List<Customer> listCustomer =(List<Customer>)request.getAttribute("listCustomer");
	  	if( listCustomer!=null && listCustomer.size()>0){
	%>
	<table border="1" cellspacing="0px" cellpadding="10px">
		<tr>
			<th>ID</th>
			<th>名称</th>
			<th>地址</th>
			<th>手机号</th>
		</tr>
	
	<%
			for(Customer cus : listCustomer){
	%>
				<tr>
					<td><%= cus.getId() %></td>
					<td><%= cus.getName() %></td>
					<td><%= cus.getAddress() %></td>
					<td><%= cus.getPhone() %></td>
				</tr>
	<%
	  		}
	  	}
	%>
	</table>
</body>
</html>

效果图

  • 定义一个类,用于保存查询的条件,实现模糊查询,该类可以是Customer的子类,也可是不是QueryCustomer.java

package com.njupt.javaweb.business;
/**
 * 
* 项目名称:javaWebMVCProject 
* 类名称:QueryCustomer 
* 类描述: 该对象用于业务查询的对象,
* 	     因为有时候查询的字段不是源对象的属性,所以需要查询对象来保存或者适配查询的属性
*       这个类就是用来适配模糊查询的属性
* 创建人:Administrator 
* 创建时间:2018年8月6日 下午4:39:33 
* 修改人:Administrator 
* 修改时间:2018年8月6日 下午4:39:33 
* 修改备注: 
* @version
 */
public class QueryCustomer extends Customer {
	private String name;
	private String address;
	private String phone;
	public void setName(String name) {
		if(name=="" || name == null) 
			this.name = "%";
		else 
			this.name = "%" + name + "%";
	}
	public void setAddress(String address) {
		if(address=="" || address == null)
			this.address = "%";
		else 
			this.address = "%" + address + "%";
	}
	public void setPhone(String phone) {
		if(phone=="" || phone == null)
			this.phone = "%";
		else 
			this.phone = "%" + phone + "%";
	}
	public String getName() {
		return name;
	}
	public String getAddress() {
		return address;
	}
	public String getPhone() {
		return phone;
	}
	public QueryCustomer() {}
	public QueryCustomer(String name,String address,String phone) {
		if(name=="" || name == null) 
			this.name = "%";
		else 
			this.name = "%" + name + "%";
		if(address=="" || address == null)
			this.address = "%";
		else 
			this.address = "%" + address + "%";
		if(phone=="" || phone == null)
			this.phone = "%";
		else 
			this.phone = "%" + phone + "%";
	}
	@Override
	public String toString() {
		return "QueryCustomer [name=" + name + ", address=" + address + ", phone=" + phone + "]";
	}
	
}

这里有个注意事项:

这几个输入框即使不输入任何内容,直接点击提交,其字段也不为null而是" "空格

  • 在CustomerServlet中编写query函数,实现模糊查询

    	private CustomerDAOJdbcImp customerDAOJdbcImp = new CustomerDAOJdbcImp();
    	private void query(HttpServletRequest request, HttpServletResponse response){	
    		System.out.println("query");
    		try {
    			request.setCharacterEncoding("UTF-8");
    		} catch (UnsupportedEncodingException e1) {
    			// TODO Auto-generated catch block
    			e1.printStackTrace();
    		}
    		String name = request.getParameter("name");
    		String address = request.getParameter("address");
    		String phone = request.getParameter("phone");
    		QueryCustomer queryCustomer = new QueryCustomer(name,address,phone);
    		
    		List<Customer> listCustomer = customerDAOJdbcImp.getForListWithQueryCustomer(queryCustomer);
    		request.setAttribute("listCustomer", listCustomer);
    		try {
    			request.getRequestDispatcher("/index.jsp").forward(request, response);
    		} catch (Exception e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} 
    	}

不出意外,页面就可以使用模糊查询了,如:

                                  

猜你喜欢

转载自blog.csdn.net/qq_23937341/article/details/81459274