導(dǎo)航式搜索在實(shí)際網(wǎng)站開(kāi)發(fā)中有很多應(yīng)用,其實(shí)現(xiàn)原理也不復(fù)雜,關(guān)鍵是如何記憶所選的條件。常見(jiàn)的方式有存入session、存入數(shù)組等。本文采用的是AJAX+數(shù)組的方式,在不跳轉(zhuǎn),不刷新整個(gè)頁(yè)面的條件下動(dòng)態(tài)返回查詢(xún)結(jié)果。
創(chuàng)新互聯(lián)建站主營(yíng)鶴崗網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,app軟件定制開(kāi)發(fā),鶴崗h5重慶小程序開(kāi)發(fā)搭建,鶴崗網(wǎng)站營(yíng)銷(xiāo)推廣歡迎鶴崗等地區(qū)企業(yè)咨詢(xún)
效果圖如下:
1.search.jsp
通過(guò)將所選的查詢(xún)條件存入數(shù)組,通過(guò)AJAX傳到后臺(tái),這樣在后臺(tái)利用所得到的查詢(xún)條件,就可以到數(shù)據(jù)庫(kù)進(jìn)行查詢(xún)了。代碼如下:
<%@ page language="java" import="java.util.List;" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>Insert title here 區(qū)域: 全部 市南 市北 李滄 嶗山 城陽(yáng) 黃島 即墨市 膠州市 膠南市 平度市 萊西市 其他
價(jià)格: 全部 6000以下 6000--8000 8000--12000 12000以上
樓盤(pán)名 區(qū)域 戶(hù)型 價(jià)格
2.BuildingServlet.java
得到j(luò)sp頁(yè)面?zhèn)鱽?lái)的數(shù)組,并解析得到對(duì)應(yīng)的條件,調(diào)用相關(guān)的方法得到查詢(xún)結(jié)果,并將結(jié)果返回給前臺(tái)。
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");//解決亂碼問(wèn)題,沒(méi)有這句,回調(diào)函數(shù)的內(nèi)容可能亂碼 String searchcharg = request.getParameterValues("searchChar")[0].trim();//得到j(luò)sp頁(yè)面數(shù)組的內(nèi)容,但是以String形式。 String[] searchchars = searchcharg.split(","); int[] searchchar = { 0, 0, 0, 0}; //切分String,將各個(gè)值存入新數(shù)組中。 //String to int for(inti = 0; i < searchchars.length; i++) { searchchar[i] = Integer.parseInt(searchchars[i]); } BuildingAction buildingaction=newBuildingAction();//根據(jù)jsp頁(yè)面?zhèn)鱽?lái)的每個(gè)條件的值,即可編寫(xiě)相應(yīng)類(lèi)查詢(xún)出對(duì)應(yīng)的結(jié)果。 Listresult=buildingaction.buildingSearch(searchchar[0], searchchar[1], searchchar[2]); if(result.size()>0){ out.println(" "); //將結(jié)果返回給jsp頁(yè)面 out.println("
"); out.close(); } else{ out.println("" + " "); for(inti=0;i樓盤(pán)名 " + "區(qū)域 " + "戶(hù)型 " + "價(jià)格 "); out.println("" + " "+result.get(i).getBuildingName()+" " + ""+result.get(i).getRegionId()+" " + ""+result.get(i).getUsageId()+" " + ""+result.get(i).getAveragePrice()+" " +""); } out.println("" +"
"); out.close(); } } }" +" " +"沒(méi)有相應(yīng)信息 " +"