丄諦啲仇魜ヤ
          如 果 敵 人 讓 你 生 氣 , 那 說 明 你 沒 有 勝 他 的 把 握!
          posts - 6,comments - 56,trackbacks - 1

          這是SSH的至于DAO和ApplicationContext.xml我就不貼出來了

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

          <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
          <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
          <%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
          <%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles"%>


          <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
          <html:html lang="true">
          <head>
           <html:base />
           <title>顯示文章</title>
          <link rel="stylesheet"  href="my.css">
           <script type="text/javascript">
           <!--
           ///////////////////////AJAX的動(dòng)態(tài)搜索/////////////// 
                 var xmlhttp=false;
                 var dataDiv;
                 var dataTableBody;
                function createXMLHttpRequest()
                {  
                  if(window.XMLHttpRequest) { //Mozilla 瀏覽器
                            xmlhttp = new XMLHttpRequest();
            }
           else if (window.ActiveXObject){ // IE瀏覽器
             try
             {
                   xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
             } catch (e)
             {
              try {
                           xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
              } catch (e) {}
             }
            }
                }
               
                function SendXMLHttpRequest(url)
                { dataDiv=document.getElementById("popup");
                  dataTableBody = document.getElementById("dataBody");            
                  createXMLHttpRequest();
                  xmlhttp.open("POST",url,true);
                  xmlhttp.onreadystatechange=callbackfunction;
                  xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
                        xmlhttp.send(url);
                 
                }
               
                function callbackfunction()
                {
                  
                   if(xmlhttp.readystate==4)
                   {
                     if(xmlhttp.status==200)
                     {
                      var data=xmlhttp.responseXML;
                      var newItems=data.getElementsByTagName("content");//***********
                       var idItems=data.getElementsByTagName("id");
                      clearData(); //清空原來的
                      for(var i=0;i<newItems.length;i++)
                      {
                        var row, item, idItem,txtNode,txtNode2 ;
                          item=newItems[i];
                          idItem=idItems[i];
                          txtNode=item.text;//////***************
                          txtNode2=idItem.text;
                          row=createRow(txtNode,txtNode2);
                          dataTableBody.appendChild(row);  
                     }
                     }
                   }
                }
               
                function text_change()
                {
                    var url="ajax_find_article.do?name="+window.document.getElementById("Text1").value;
                           // alert(url);
                   SendXMLHttpRequest(url);

                }
               
                //生成表格內(nèi)容行
              function createRow(data,data2) {           
                 var row, cell,cell2, txtNode,txtNode2;
                 row = document.createElement("tr");
                 cell = document.createElement("td");
                 cell2 = document.createElement("td");
                 var a=document.createElement("a")
                 cell.setAttribute("bgcolor", "#FFFAFA");
                 cell.setAttribute("border", "1");
                 cell2.setAttribute("bgcolor", "#FFFAFA");
                 cell2.setAttribute("border", "1");   
                 a.setAttribute("href", "http://localhost:8080/ssh_article/oneArticleList.do?id="+data2);
                                     
                 txtNode = document.createTextNode(data);
                 txtNode2 = document.createTextNode("查看詳情");
                 cell.appendChild(txtNode);
                 a.appendChild(txtNode2);
                 row.appendChild(cell);
                 cell2.appendChild(a); 
                 row.appendChild(cell2);
                 return row; 
              }
               
                // 清除提示框******
              function clearData() {           
                 var ind = dataTableBody.childNodes.length;           
                 for (var i = ind - 1; i >= 0 ; i--) {
                     dataTableBody.removeChild(dataTableBody.childNodes[i]);      
                 }
                 dataDiv.style.border = "none";
              }        

           //-->
           </script>
          </head>
          <body>
           關(guān)鍵字搜索:<input type="text" id="Text1" size="10"  onkeyup="return text_change()" />
            <hr style="color:blue">
            <div style="position:absolute; left: 0px; width: 200px; top: 50px; height: 233px; " id="popup">
             <table border="0" cellspacing="2" cellpadding="2"  bgcolor="#E0F0F8">
              <tbody id="dataBody">
              </tbody>
             </table>
            </div>
            <br />
          </body>
          </html:html>
          *********************************************
          SERVLET

          package com.wsq.struts.action;

          import java.io.IOException;
          import java.io.PrintWriter;
          import java.util.Iterator;
          import java.util.List;import javax.servlet.http.HttpServletRequest;
          import javax.servlet.http.HttpServletResponse;import org.apache.struts.action.Action;
          import org.apache.struts.action.ActionForm;
          import org.apache.struts.action.ActionForward;
          import org.apache.struts.action.ActionMapping;
          import com.wsq.dao.ArticleDAO;
          import com.wsq.vo.Article;

          public class Ajax_find_articleAction extends Action {
               private ArticleDAO articleDAO;
               public ArticleDAO getArticleDAO() {
             return articleDAO;
            }
            public void setArticleDAO(ArticleDAO articleDAO) {
             this.articleDAO = articleDAO;
            }
           public ActionForward execute(ActionMapping mapping, ActionForm form,
             HttpServletRequest request, HttpServletResponse response)  throws IOException{
            request.setCharacterEncoding("UTF-8");
            String name = new String(request.getParameter("name").getBytes("ISO-8859-1"),"GB2312");////編碼轉(zhuǎn)換GB2312
            System.out.println(name+"  關(guān)鍵字!");

            if (name.length()<1 ) {
             name ="0";  ////搜索為空(也就是沒數(shù)據(jù)返回)
            }
            System.out.println("+++++++++++++++++++++++++++++");
            List list = articleDAO.findByMyName(name);
            System.out.println("-------------------------------");
            response.setContentType("text/xml; charset=UTF-8");
            response.setHeader("Cache-Control", "no-cache");
            PrintWriter out = response.getWriter();
            out.println("<response>");
            Iterator i = list.iterator();
            while (i.hasNext()) {
             Article a= (Article) i.next();
             out.println("<content>" + a.getTitle() + "</content>");
             out.println("<id>" + a.getId() + "</id>");

             
            }
            out.println("</response>");
            out.close();
            return null;

           }
           
          }

          posted on 2007-09-09 13:21 Crying 閱讀(694) 評論(0)  編輯  收藏 所屬分類: AJAX
          主站蜘蛛池模板: 大冶市| 邻水| 江都市| 鹰潭市| 榆社县| 三都| 宁化县| 增城市| 吴川市| 凤台县| 宜章县| 化州市| 安庆市| 米林县| 澄城县| 东辽县| 新竹县| 郴州市| 维西| 枣庄市| 方正县| 两当县| 淮阳县| 尖扎县| 磐石市| 中山市| 尚义县| 长丰县| 绥阳县| 迁安市| 阿尔山市| 新泰市| 伊宁县| 凉城县| 景德镇市| 武鸣县| 乐平市| 新营市| 荃湾区| 崇文区| 玛纳斯县|