<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    內蒙古java團隊

    j2se,j2ee開發組
    posts - 139, comments - 212, trackbacks - 0, articles - 65
      BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

    ajax 入門 4

    Posted on 2008-04-25 14:16 帥子 閱讀(243) 評論(0)  編輯  收藏
    這次我們來一步一步的仿造一個google的搜索欄,由于本人學的也很淺相信大家不會看得很迷糊,由于我們沒有鏈接數據庫,我采用一個硬編碼來編寫被匹配的內容,正常情況下應該是從數據庫中取出一個表的”被搜索最多次數”的10個內容然后進行匹配
    import?java.util.ArrayList;
    import?java.util.List;


    public?class?ListFactory?{
    ????public?static?List?getList(){
    ????????List?list?=?new?ArrayList();
    ????????list.add("ibm");
    ????????list.add("hp");
    ????????list.add("dell");
    ????????list.add("desk");
    ????????return?list;
    ????}

    }

    這個工廠生成了一個list,里面存儲了需要匹配的內容
    有了匹配信息我們還需要一個servlet來對它進行匹配

    新建一個servlet
    映射地址?searchAction

    import?java.io.IOException;
    import?java.io.PrintWriter;
    import?java.util.List;

    import?javax.servlet.ServletException;
    import?javax.servlet.http.HttpServlet;
    import?javax.servlet.http.HttpServletRequest;
    import?javax.servlet.http.HttpServletResponse;


    public?class?SearchAction?extends?HttpServlet?{

    ????/**
    ?????*?Constructor?of?the?object.
    ?????*/
    ????public?SearchAction()?{
    ????????super();
    ????}

    ????/**
    ?????*?Destruction?of?the?servlet.?<br>
    ?????*/
    ????public?void?destroy()?{
    ????????super.destroy();?//?Just?puts?"destroy"?string?in?log
    ????????//?Put?your?code?here
    ????}

    ????/**
    ?????*?The?doGet?method?of?the?servlet.?<br>
    ?????*
    ?????*?This?method?is?called?when?a?form?has?its?tag?value?method?equals?to?get.
    ?????*?
    ?????*?@param?request?the?request?send?by?the?client?to?the?server
    ?????*?@param?response?the?response?send?by?the?server?to?the?client
    ?????*?@throws?ServletException?if?an?error?occurred
    ?????*?@throws?IOException?if?an?error?occurred
    ?????*/
    ????public?void?doGet(HttpServletRequest?request,?HttpServletResponse?response)
    ????????????throws?ServletException,?IOException?{
    ????????//轉發至doPost();
    ????????doPost(request,response);
    ????}

    ????/**
    ?????*?The?doPost?method?of?the?servlet.?<br>
    ?????*
    ?????*?This?method?is?called?when?a?form?has?its?tag?value?method?equals?to?post.
    ?????*?
    ?????*?@param?request?the?request?send?by?the?client?to?the?server
    ?????*?@param?response?the?response?send?by?the?server?to?the?client
    ?????*?@throws?ServletException?if?an?error?occurred
    ?????*?@throws?IOException?if?an?error?occurred
    ?????*/
    ????public?void?doPost(HttpServletRequest?request,?HttpServletResponse?response)
    ????????????throws?ServletException,?IOException?{
    ????????//用于緩存匹配對象的字符串,正常應該是個數組
    ????????String?temps="";
    ????????response.setContentType("text/html");
    ????????PrintWriter?out?=?response.getWriter();
    ????????//從工廠類中取出要匹配的list
    ????????List?list?=?ListFactory.getList();
    ????????//ajax發送過來的請求值,也就是頁面上現在輸入的內容
    ????????String?inputtext?=?request.getParameter("inputtext");
    ????????//遍歷list
    ????????for(int?i=0;i<list.size();i++){
    ????????????String?temp?=?(String)?list.get(i);
    ????????????//如果在匹配內容頭中找到當前輸入的字符串,且輸入不是空串
    //indexOf?返回字串的位置,為0?則表示?123中找到了12?,而找不到23因為23的indexOf
    //為1
    ????????????if(temp.indexOf(inputtext)==0?&&?inputtext!=null?&&?inputtext.trim().length()!=0){
    ????????????????//將匹配上的list內容添加到緩存字符串
    ????????????????temps=temps+temp+"<br>";
    ????????????}
    ????????}
    ????????//輸出緩存字符串
    ????????out.write(temps);
    ????????out.flush();
    ????????out.close();
    ????}

    ????/**
    ?????*?Initialization?of?the?servlet.?<br>
    ?????*
    ?????*?@throws?ServletException?if?an?error?occure
    ?????*/
    ????public?void?init()?throws?ServletException?{
    ????????//?Put?your?code?here
    ????}

    }

    有了工廠類(替代數據庫),有了控制器,現在開始寫前臺的頁面和ajax

    <%@?page?language="java"?import="java.util.*"?pageEncoding="utf-8"%>
    <%
    String?path?=?request.getContextPath();
    String?basePath?=?request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>

    <!DOCTYPE?HTML?PUBLIC?"-//W3C//DTD?HTML?4.01?Transitional//EN">
    <html>
    ??<head>
    ????<base?href="<%=basePath%>">
    ????
    ????<title>My?JSP?'index.jsp'?starting?page</title>
    ????<meta?http-equiv="pragma"?content="no-cache">
    ????<meta?http-equiv="cache-control"?content="no-cache">
    ????<meta?http-equiv="expires"?content="0">????
    ????<meta?http-equiv="keywords"?content="keyword1,keyword2,keyword3">
    ????<meta?http-equiv="description"?content="This?is?my?page">
    ????<!--
    ????<link?rel="stylesheet"?type="text/css"?href="styles.css">
    ????-->
    ??</head>
    ??<script?src="js/prototype.js"></script>
    ??<script?src="js/test.js"?></script>
    ??<body>
    ??<table?width="100%"?border="1"?bordercolor="#000000">
    ????<tr>
    ??????<td>
    ????????<input?name="text"?type="text"?id="itext"?onKeyUp="getXML()"/>
    ????????<input?name="button"?type="button"??value="搜索"/>
    ????????<div?id="outdiv"?style="?display:none;?width:119px;?height:20;?position:absolute;?left:?16px;?top:?41px;?background-color:#ECEDFF">111</div>
    ??????</td>
    ????</tr>
    ????<tr>
    ??????<td>?</td>
    ????</tr>
    ??</table>
    ??</body>
    </html>

    這個頁面中唯一需要注意的是?作為輸出匹配框的?div?層
    <div?id="outdiv"?style="?display:none;?width:119px;?height:20;?position:absolute;?left:?16px;?top:?41px;?background-color:#ECEDFF">111</div>
    此層一開始被設置為隱藏,111可以不寫,其實寫什么都看不到,因為它根本沒有被顯示
    Display:none?類似的屬性還有?visible,它們的區別在此不說了,百度一下,你就知道?
    文本框設置一個鍵盤事件
    onKeyUp="getXML()"
    每次鍵盤抬起就調用一次函數

    在開始寫腳本文件之前需要先導入prototype庫,在我的ajax入門3?里有提及

    正常導入它以后我們就開始?編寫?test.js?文件

    //鍵盤抬起時激活的函數
    function?getXML(){
    ????//局部請求地址
    ????var?url="searchAction";
    ????//獲取用戶當前輸入的內容
    ????var?inputvalue=$("itext").value;
    ????//使用prototype函數構造xmlhttprequest對象
    ????var?myAjax?=?new?Ajax.Request(
    ????url,
    ????{
    ????????//請求方法為post
    ????????method:'post',
    ????????//設置參數為?inputtext=inputvalue
    ????????parameters:"inputtext="+inputvalue,
    ????????//設置回調函數
    ????????onComplete:showResponse,
    ????????//是否異步
    ????????asynchronous:true
    ????}
    ????);
    }

    function?showResponse(xmlrequest){
    //還是需要注意回調函數的參數,使用此參數的responseText屬性獲取服務器//servlet返回的文本內容,要取得XML請參考我之前的?ajax?入門文章
    ????var?text?=?xmlrequest.responseText;
    ????//如果返回的被匹配上的內容不為空
    ????if(text!=""){
    ????????//顯示該層,關于element.show也是prototype的函數
    ????????Element.show("outdiv");
    ????}else{
    //如果沒匹配上就隱藏該層,注意我們的思路是每次鍵盤抬起都進行一次請求,
    //然后進行判斷,不匹配就隱藏
    ????????Element.hide("outdiv");
    ????}
    ????//將匹配的內容輸出到?div?層
    ????$("outdiv").innerHTML=xmlrequest.responseText;
    }


    以下內容為更新:



    這里我們可以再稍微豐富一下比如將servlet的doPost改寫成

    ????public?void?doPost(HttpServletRequest?request,?HttpServletResponse?response)
    ????????????throws?ServletException,?IOException?{
    ????????String?temps="";
    ????????response.setContentType("text/html");
    ????????PrintWriter?out?=?response.getWriter();
    ????????List?list?=?ListFactory.getList();
    ????????String?inputtext?=?request.getParameter("inputtext");
    ????????for(int?i=0;i<list.size();i++){
    ????????????String?temp?=?(String)?list.get(i);
    ????????????if(temp.indexOf(inputtext)==0?&&?inputtext!=null?&&?inputtext.trim().length()!=0){
    ????????????????temps=temps+temp+"$";
    ????????????}
    ????????}
    ????????out.write(temps);
    ????????out.flush();
    ????????out.close();
    ????}

    也就是使用"$"字符來將返回的幾個匹配分割

    然后在javascript中對其進行解析

    function?showResponse(xmlrequest){
    ????var?text?=?xmlrequest.responseText;
    ????var?texts?=?text.split("$");
    ????if(text!=""){
    ????????Element.show("outdiv");
    ????}else{
    ????????Element.hide("outdiv");
    ????}
    ????var?temp?=?"";
    ????var?outdiv?=?$("outdiv");
    ????for(var?i?=?0;i?<?texts.length-1;i++){
    ????????temp?=?temp?+?"<span?style=cursor:hand?onclick='inMessage(this)'>"?+texts[i]+?"</span>"?+"<br>";
    ????}
    ????outdiv.innerHTML?=?temp;
    }


    function?inMessage(obj){
    ????//alert(obj.innerHTML);
    ????$(itext).value?=?obj.innerHTML;
    ????Element.hide("outdiv");
    }

    這樣每次出現下拉列表之后列表中的項目都可以被選擇,點擊之后內容就會錄入到搜索框中了?

    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 美女被暴羞羞免费视频| 亚洲人成免费电影| 又长又大又粗又硬3p免费视频| 永久免费毛片在线播放| 亚洲精品乱码久久久久久下载| 最刺激黄a大片免费网站| 亚洲综合精品香蕉久久网97| 2019中文字幕免费电影在线播放| 亚洲天堂中文资源| 37pao成人国产永久免费视频| 亚洲神级电影国语版| 中文字幕无码视频手机免费看 | 国产AV无码专区亚洲AV男同| 中国人免费观看高清在线观看二区| 中文字幕亚洲乱码熟女一区二区| 最近中文字幕免费大全| 亚洲AV永久无码精品水牛影视| 182tv免费观看在线视频| 亚洲一卡2卡3卡4卡国产网站| 成人免费在线视频| 国产亚洲精品美女| 亚洲国产另类久久久精品黑人| 久久99精品视免费看| 亚洲一卡2卡3卡4卡国产网站| 在线日韩av永久免费观看| 一级毛片免费在线| 亚洲av无码专区国产乱码在线观看| 4444www免费看| 亚洲欧美日韩综合俺去了| AV在线播放日韩亚洲欧| 久久久久久夜精品精品免费啦| 久久精品国产亚洲av麻豆图片| 国产精品国产免费无码专区不卡 | 色婷婷亚洲十月十月色天| 毛色毛片免费观看| 色老头综合免费视频| 久久久国产精品亚洲一区| 午夜无遮挡羞羞漫画免费| 中文字幕免费在线看| 亚洲精品国产日韩| 亚洲一区无码中文字幕 |