首页 > 代码库 > AJAX实现导航式多条件搜索

AJAX实现导航式多条件搜索

   导航式搜索在实际网站开发中有很多应用,其实现原理也不复杂,关键是如何记忆所选的条件。常见的方式有存入session、存入数组等。本文采用的是AJAX+数组的方式,在不跳转,不刷新整个页面的条件下动态返回查询结果。

效果图如下:

wKioL1N4a-ORIaweAAEkfNBl1OQ857.jpg

1.search.jsp

   通过将所选的查询条件存入数组,通过AJAX传到后台,这样在后台利用所得到的查询条件,就可以到数据库进行查询了。代码如下:

<%@ page language="java"  import="java.util.List;" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<link rel="stylesheet" rev="stylesheet" href="http://www.mamicode.com/css/web.css" type="text/css" media="all" />

<title>Insert title here</title>

<script type="text/javascript" src="http://www.mamicode.com/js/jquery.js"></script>

<script type="text/javascript">

   var xmlHttp;

   function createXmlHttpRequest() {

       if (window.ActiveXObject) {

           xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");

       } else if (window.XMLHttpRequest) {

           xmlHttp = new XMLHttpRequest();

           }

   }

//回调

   function handleStateChange() {

       if (xmlHttp.readyState == 4) {

           if (xmlHttp.status == 200) {

               parseResults();

               }

           }

   }

//将后台返回的数据显示在层content中

   function parseResults() {

        document.getElementById("content").innerHTML=xmlHttp.responseText;//将返回的请

求文本Text写入指定的DIV中

   }

</script>

<script type="text/javascript">

   var req;

   var searchChar = new Array();

   for(var i=0;i<3;i++){

       searchChar[i]=0;

   }

   function bb(num,con){

        switch(num){

             case 0: searchChar[0]=con;

             break;

             case 1: searchChar[1]=con;

             break;

             case 2: searchChar[2]=con;

             break;

        }

    for(var i=0;i<13;i++){   //解决选中字段显示颜色的问题

        var region1 = "0" + i;

        document.getElementById(region1).style.color="black";

    }

    for(var i=0;i<5;i++){

        var type1 = "1" + i;

        document.getElementById(type1).style.color="black";

    }

    for(var i=0;i<5;i++){

        var price1 = "2" + i;

        document.getElementById(price1).style.color="black";

    }

    var region="0"+searchChar[0];

    document.getElementById(region).style.color="red";

    var type="1"+searchChar[1];

    document.getElementById(type).style.color="red";

    var price="2"+searchChar[2];

    document.getElementById(price).style.color="red";


   var url = "BuildingServlet";

   createXmlHttpRequest();

   xmlHttp.open("POST", url, true);

   xmlHttp.onreadystatechange = handleStateChange;//回调

   xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;text/xml;charset=utf-8"); //text/xml;charset=utf-8:可以解决汉字封装json问题

       xmlHttp.send("searchChar="+searchChar);

   }

</script>

</head>

<body class="home">

<div id="search">

   <p>区域:

       <a id="00" href="javascript:void(0);" onclick="bb(0,00)">全部</a>

       <a id="01" href="javascript:void(0);" onclick="bb(0,01)">市南</a>

       <a id="02" href="javascript:void(0);" onclick="bb(0,02)">市北</a>

       <a id="03" href="javascript:void(0);" onclick="bb(0,03)">李沧</a>

       <a id="04" href="javascript:void(0);" onclick="bb(0,04)">崂山</a>

       <a id="05" href="javascript:void(0);" onclick="bb(0,05)">城阳</a>

       <a id="06" href="javascript:void(0);" onclick="bb(0,06)">黄岛</a>

       <a id="07" href="javascript:void(0);" onclick="bb(0,07)">即墨市</a>

       <a id="08" href="javascript:void(0);" onclick="bb(0,08)">胶州市</a>

       <a id="09" href="javascript:void(0);" onclick="bb(0,09)">胶南市</a>

       <a id="010" href="javascript:void(0);" onclick="bb(0,10)">平度市</a>

       <a id="011" href="javascript:void(0);" onclick="bb(0,11)">莱西市</a>

       <a id="012" href="javascript:void(0);" onclick="bb(0,12)">其他</a></p>

   <p>户型:

       <a id="10" href="javascript:void(0);" onclick="bb(1,0)">全部</a>

       <a id="11" href="javascript:void(0);" onclick="bb(1,1)">住宅</a>

       <a id="12" href="javascript:void(0);" onclick="bb(1,2)">商用</a>

       <a id="13" href="javascript:void(0);" onclick="bb(1,3)">办公</a>

       <a id="14" href="javascript:void(0);" onclick="bb(1,4)">其他</a></p>

   <p>价格:

       <a id="20" href="javascript:void(0);" onclick="bb(2,0)">全部</a>

       <a id="21" href="javascript:void(0);" onclick="bb(2,1)">6000以下</a>

       <a id="22" href="javascript:void(0);" onclick="bb(2,2)">6000--8000</a>

       <a id="23" href="javascript:void(0);" onclick="bb(2,3)">8000--12000</a>

       <a id="24" href="javascript:void(0);" onclick="bb(2,4)">12000以上</a></p>

</div>

<div id="content" class="content">

   <table width="742">

      <tr>

           <td>楼盘名</td>

           <td>区域</td>

           <td>户型</td>

           <td>价格</td>

      </tr>

     </table>

</div>

</body>

<script type="text/javascript">

   document.getElementById("00").style.color="red";

   document.getElementById("10").style.color="red";

   document.getElementById("20").style.color="red";

</script>

</html>

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页面传来的每个条件的值,即可编写相应类查询出对应的结果。
List<Building> result=buildingaction.buildingSearch(searchchar[0], searchchar[1], searchchar[2]);
if(result.size()>0){
out.println("<table width=‘742‘>"); //将结果返回给jsp页面
out.println("<tr>"
+ "<td>楼盘名</td>"
+ "<td>区域</td>"
+ "<td>户型</td>"
+ "<td>价格</td>");
out.println("</tr>");
for(inti=0;i<result.size();i++){
out.println("<tr>"
+ "<td><span>"+result.get(i).getBuildingName()+"</span> </td>"
+ "<td><span>"+result.get(i).getRegionId()+"</span></td>"
+ "<td><span>"+result.get(i).getUsageId()+"</span></td>"
+ "<td><span>"+result.get(i).getAveragePrice()+"</span></td>"
+"</tr>");
}
out.println("</table>");
out.close();
}
else{
out.println("<table width=‘742‘>"
+"<tr>"
+" <td> <span>没有相应信息 </span></td>"
+"</tr>"
+"</table>");
out.close();
}
}
}


本文出自 “java我的最爱” 博客,请务必保留此出处http://lindianli.blog.51cto.com/7129432/1413201