posts - 431,  comments - 344,  trackbacks - 0

HTML源代码
<%@ page language="Java" contentType="text/html; charset=UTF-8"%>
<html>
  <head>
    <title>article.html</title>
  <script type="text/javascript" src="js/common.js"></script>
  <script type="text/javascript">
  var completeDiv;
  var inputField;
  var nameTable;
  var nameTableBody;
  function initVars() {
     inputField = document.getElementById("title");           
     nameTable = document.getElementById("name_table");
     completeDiv = document.getElementById("popup");
     nameTableBody = document.getElementById("name_table_body");
    
  }
 
  function findNames(){
   initVars();
   if (inputField.value.length > 0) {
       createXMLHttpRequest(); 
       var url = "article.do?method=search&title="+encodeURI(inputField.value);  
       xmlHttp.open("GET", url, true);
       xmlHttp.onreadystatechange = callback;
       xmlHttp.send(null);
    } else {
       clearNames();
    }
  }
  function callback() {
            if (xmlHttp.readyState == 4) {
                if (xmlHttp.status == 200) {
               
                    var name = xmlHttp.responseXML.getElementsByTagName("title")[0].firstChild.data;
                    setNames(xmlHttp.responseXML.getElementsByTagName("title"));
                } else if (xmlHttp.status == 204){
                    clearNames();
                  
                }
            }
        }
       
        function setNames(the_names) {           
            clearNames();
            var size = the_names.length;
            setOffsets();

            var row, cell, txtNode;
            for (var i = 0; i < size; i++) {
                var nextNode = the_names[i].firstChild.data;
                row = document.createElement("tr");
                cell = document.createElement("td");
               
                cell.onmouseout = function() {this.className='mouseOver';};
                cell.onmouseover = function() {this.className='mouseOut';};
                cell.setAttribute("bgcolor", "#FFFAFA");
                cell.setAttribute("border", "0");
                cell.onclick = function() { populateName(this); } ;                            

                txtNode = document.createTextNode(nextNode);
                cell.appendChild(txtNode);
                row.appendChild(cell);
                nameTableBody.appendChild(row);
               
            }
        }

        function setOffsets() {
            var end = inputField.offsetWidth;
            var left = calculateOffsetLeft(inputField);
            var top = calculateOffsetTop(inputField) + inputField.offsetHeight;

            completeDiv.style.border = "black 1px solid";
            completeDiv.style.left = left + "px";
            completeDiv.style.top = top + "px";
            nameTable.style.width = end + "px";
        }
       
        function calculateOffsetLeft(field) {
          return calculateOffset(field, "offsetLeft");
        }

        function calculateOffsetTop(field) {
          return calculateOffset(field, "offsetTop");
        }

        function calculateOffset(field, attr) {
          var offset = 0;
          while(field) {
            offset += field[attr];
            field = field.offsetParent;
          }
          return offset;
        }

        function populateName(cell) {
            inputField.value = cell.firstChild.nodeValue;
            clearNames();
        }
  function clearNames() {
     var ind = nameTableBody.childNodes.length;
     for (var i = ind - 1; i >= 0 ; i--) {
       nameTableBody.removeChild(nameTableBody.childNodes[i]);
     }
    completeDiv.style.border = "none";
  }
  </script>
  </head>
 
  <body>
    <h3>Ajax Auto Complete Example</h3>
    请输入名称:<input type="text" name="title" id="title" size="30" width="100" height="20" onkeyup="findNames();">
    <div style="position:absolute;" id="popup">
        <table id="name_table" bgcolor="#FFFAFA" border="0" cellspacing="0" cellpadding="0"/>           
            <tbody id="name_table_body"></tbody>
        </table>
    </div>
  </body>
</html>


Action源代码
/*
 * Generated by MyEclipse Struts
 * Template path: templates/java/JavaClass.vtl
 */
package com.rain.struts.action;

import Java.io.IOException;
import Java.io.PrintWriter;
import Java.io.UnsupportedEncodingException;
import Java.util.Iterator;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.apache.struts.validator.DynaValidatorForm;

import com.rain.bean.ArticleDao;

/**
 * MyEclipse Struts
 * Creation date: 09-12-2006
 *
 * XDoclet definition:
 * @struts.action parameter="method"
 */
public class ArticleAction extends DispatchAction {
 /*
  * Generated Methods
  */

 /**
  * Method execute
  * @param mapping
  * @param form
  * @param request
  * @param response
  * @return ActionForward
  */
 public ActionForward search(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response) {
  DynaValidatorForm articleForm = (DynaValidatorForm)form;
  String title;
  try {
   title = new String(request.getParameter("title").getBytes("ISO8859_1"),"UTF-8");
   System.out.println(title);
   ArticleDao dao=new ArticleDao();
   
   Iterator it=dao.findAllName(title).iterator();
   
   if(it.hasNext()){
    PrintWriter out;
    try {
     
     response.setContentType("text/xml;charset=UTF-8");
     response.setHeader("Cache-Control", "no-cache");
     out = response.getWriter();
     out.println("<response>");
     while(it.hasNext()){
      String name=it.next().toString();
      out.println("<title>"+name+"</title>");
     }
     out.println("</response>");
     it=null;
     out.close();
    } catch (IOException e) {
     // TODO 自动生成 catch 块
     e.printStackTrace();
    }
    
   }else{
    response.setStatus(HttpServletResponse.SC_NO_CONTENT);
   }
  } catch (UnsupportedEncodingException e1) {
   // TODO 自动生成 catch 块
   e1.printStackTrace();
  }
  
  
  return null;
 }
}



Dao源代码
/**
 *@type_name: ArticleDao
 *@date: 2006-9-13
 *@author scott_zhou
 */
package com.rain.bean;

import Java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.rain.HibernateSessionFactory;

/**
 *
 */
public class ArticleDao {

 public List findAllName(String title){
  System.out.println("11");
  String hql="select art.title from Article art where art.title like '"+title+"%'";
     List list=null;
     try{
     
      Session session=HibernateSessionFactory.getCurrentSession();

         Transaction tx=session.beginTransaction();
          Query query=session.createQuery(hql);
           
            list=query.list();
            tx.commit();
            session.close();
     }catch(Exception e){
      System.out.println(e.getMessage());
      e.printStackTrace();
     }
       
        return list;
 }
}

posted on 2006-09-13 09:19 周锐 阅读(907) 评论(7)  编辑  收藏 所属分类: AjaxHibernateStruts

只有注册用户登录后才能发表评论。


网站导航: