AJAX实现导航式多条件搜索
导航式搜索在实际网站开发中有很多应用,其实现原理也不复杂,关键是如何记忆所选的条件。常见的方式有存入session、存入数组等。本文采用的是AJAX+数组的方式,在不跳转,不刷新整个页面的条件下动态返回查询结果。
创新互联建站主营鹤岗网站建设的网络公司,主营网站建设方案,app软件定制开发,鹤岗h5重庆小程序开发搭建,鹤岗网站营销推广欢迎鹤岗等地区企业咨询
效果图如下:
1.search.jsp
通过将所选的查询条件存入数组,通过AJAX传到后台,这样在后台利用所得到的查询条件,就可以到数据库进行查询了。代码如下:
<%@ page language="java" import="java.util.List;" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>Insert title here
楼盘名 区域 户型 价格
2.BuildingServlet.java
得到jsp页面传来的数组,并解析得到对应的条件,调用相关的方法得到查询结果,并将结果返回给前台。
packagecom.realty.servlet; importjavax.servlet.ServletException; importjavax.servlet.annotation.WebServlet; importjavax.servlet.http.HttpServlet; importjavax.servlet.http.HttpServletRequest; importjavax.servlet.http.HttpServletResponse; importjavax.swing.JOptionPane; importcom.realty.base.model.*; importcom.realty.base.action.BuildingAction; /** * Servlet implementation class BuildingServlet */ @WebServlet("/BuildingServlet") publicclassBuildingServlet extendsHttpServlet { privatestaticfinallongserialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ publicBuildingServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protectedvoiddoGet(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException { // TODO Auto-generated method stub doPost(request,response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protectedvoiddoPost(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException { // TODO Auto-generated method stub request.setCharacterEncoding("utf-8"); response.setHeader("Cache-Control", "no-store"); response.setHeader("Pragma", "no-cache"); response.setDateHeader("Expires", 0); response.setContentType("text/html;charset=UTF-8");//解决乱码问题,没有这句,回调函数的内容可能乱码 String searchcharg = request.getParameterValues("searchChar")[0].trim();//得到jsp页面数组的内容,但是以String形式。 String[] searchchars = searchcharg.split(","); int[] searchchar = { 0, 0, 0, 0}; //切分String,将各个值存入新数组中。 //String to int for(inti = 0; i < searchchars.length; i++) { searchchar[i] = Integer.parseInt(searchchars[i]); } BuildingAction buildingaction=newBuildingAction();//根据jsp页面传来的每个条件的值,即可编写相应类查询出对应的结果。 Listresult=buildingaction.buildingSearch(searchchar[0], searchchar[1], searchchar[2]); if(result.size()>0){ out.println(" "); //将结果返回给jsp页面 out.println("
"); out.close(); } else{ out.println("" + " "); for(inti=0;i楼盘名 " + "区域 " + "户型 " + "价格 "); out.println("" + " "+result.get(i).getBuildingName()+" " + ""+result.get(i).getRegionId()+" " + ""+result.get(i).getUsageId()+" " + ""+result.get(i).getAveragePrice()+" " +""); } out.println("" +"
"); out.close(); } } }" +" " +"没有相应信息 " +"
网站栏目:AJAX实现导航式多条件搜索
文章转载:http://ybzwz.com/article/pjsjec.html