上次讲过简单的分页查询,就是简单的查询所有信息,不带其他条件,而复杂分页查询就是在简单查询的基础上,加一些条件。
我们以搜索为例(搜索完成后带分页效果),这里我们就在上一次的基础上进行修改
首先是我们的servlet层:
@WebServlet("/searchStudentServlet")
public class SearchStudentServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//创建StudentServiceImpl对象
StudentService service = new StudentServiceImpl();
//beanUtils工具包:将map集合封装成Java对象
//将前台的数据装入map集合中
Map<String, String[]> map = new HashMap<String,String[]>(request.getParameterMap());
//创建一个list集合,用来装查询的信息
ArrayList<String[]> list = new ArrayList<>();
int currentPage = 0;
//判断map中是否包含id/name/age
if (map.containsKey("id") || map.containsKey("name") || map.containsKey("age")){//如果包含
list.add(map.get("id"));
list.add(map.get("name"));
list.add(map.get("age"));
request.getSession().setAttribute("mapInfo",list);//将list集合存入session “mapInfo”中
}else {//如果不包含
//获取session “mapInfo”
ArrayList<String[]> attribute = (ArrayList<String[]>) request.getSession().getAttribute("mapInfo");
//如果得到的mapInfo中不为空,将值取出并装入map中
if (attribute != null){
map.put("id",attribute.get(0));
map.put("name",attribute.get(1));
map.put("age",attribute.get(2));
}
}
//遍历map
Set<Map.Entry<String, String[]>> entries = map.entrySet();
Iterator<Map.Entry<String, String[]>> iterator = entries.iterator();
while (iterator.hasNext())