類比Google首頁(dwr實現ajax)

來源:互聯網
上載者:User

 後台資料庫為Oracle:

--建立查詢資訊表
create table searchInfo
(
id number not null primary key,--編號
content varchar2(100) not null,--查詢內容
count number not null--查詢次數
)
--建立序列
create sequence seq_searchInfo;
--建立插入資料的預存程序
create or replace procedure proc_add(vContent varchar2,vResult out varchar2)
as
vCount number;
begin
select count(*) into vCount from searchInfo where content = vContent;
if vCount = 0 then
insert into searchInfo values(seq_searchInfo.Nextval,vContent,1);
else
update searchInfo set count = count + 1 where content = vContent;
end if;
vResult := 'success';
exception
when others then
vResult := 'fail';
end; 首先需要把dwr.jar匯入到WEB-INF\lib目錄下,然後在web.xml檔案中配置DWRServlet
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<servlet>
<servlet-name>dwr-invoker</servlet-name>
<servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
</servlet>
<servlet>
<description>
This is the description of my J2EE component
</description>
<display-name>
This is the display name of my J2EE component
</display-name>
<servlet-name>ServletX</servlet-name>
<servlet-class>control.ServletX</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>dwr-invoker</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>ServletX</servlet-name>
<url-pattern>/ServletX</url-pattern>
</servlet-mapping>
</web-app>

接著需要在WEB-INF\lib目錄下建立dwr.xml檔案,並對javascript要調用的類進行聲明並公開方法,當然預設公開全部方法,需要提到的是,若類方法的參數或傳回值為Bean,則還需要使用convert標籤

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN" "http://getahead.org/dwr/dwr20.dtd">
<dwr>
<allow>
<create creator="new" javascript="history">
<param name="class" value="operation.OperSearchInfo" />
<include method="getHistory" />
</create>
<convert converter="bean" match="entity.SearchInfo">
<param name="include" value="content,count" />
</convert>
</allow>
</dwr>

兩個xml檔案配置好後,可以在地址欄中輸入http://localhost:9527/工程名/dwr進行測試,若測試成功,將顯示可用的類及其方法
建立控制器ServletX.java,本例中只是判斷使用者的搜尋操作是否成功,並以控制台的形式輸出
package control;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import operation.*;

public class ServletX extends HttpServlet {

private static final long serialVersionUID = 1L;

public ServletX() {
super();
}

public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String model = request.getParameter("model");
if(model.equals("search")){
String content = Translation.transCode(request.getParameter("content"));
OperSearchInfo obj = new OperSearchInfo();
if(obj.search(content)){
System.out.println("success");
}else{
System.out.println("fail");
}
}
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}

public void init() throws ServletException {
// Put your code here
}
}

建立與資料庫表對應的實體Bean,SearchInfo.java檔案
package entity;

/** *//**
* 搜尋資訊表實體
* @author 非凡DZ
*
*/
public class SearchInfo {
private int id; //編號
private String content; //查詢內容
private int count; //查詢次數

public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}

建立對資料庫表searchInfo進行操作的類OperSearchInfo.java及方法
package operation;

import java.sql.*;
import java.util.*;
import db.DataBase;
import entity.SearchInfo;
/** *//**
* 該類包含對searchInfo表所有操作
* @author 非凡DZ
*
*/
public class OperSearchInfo {

/** *//**
* 使用者點擊搜尋按鈕後執行
* @param content
* @return
*/
public boolean search(String content){
boolean flag = false;
DataBase db = new DataBase();
Connection con = db.getConnection();
CallableStatement cs = null;
try{
cs = con.prepareCall("{call proc_add(?,?)}");
cs.setString(1, content);
cs.registerOutParameter(2, java.sql.Types.CHAR);
cs.execute();
if(cs.getString(2).equals("success")){
flag = true;
}
}catch(Exception e){
System.out.println("proc異常"+e.getMessage());
e.printStackTrace();
}finally{
try{
con.close();
}catch(Exception ex){
System.out.println("關閉串連異常"+ex.getMessage());
ex.printStackTrace();
}
}
return flag;
}

/** *//**
* 獲得與介面文字框中資訊相似的前10條資訊
* @param content 介面文字框中的資料
* @return 相似資訊
*/
public ArrayList getHistory(String content){
DataBase db = new DataBase();
Connection con = db.getConnection();
ResultSet rs = null;
ArrayList<SearchInfo> aryResult = new ArrayList<SearchInfo>();
String sql = "select content,count from searchInfo where content"
+" like ? and rownum <= 10 order by count desc";
try{
if(!content.equals("")){
PreparedStatement pstn = con.prepareStatement(sql);
pstn.setString(1, content+"%");
rs = pstn.executeQuery();
while(rs.next()){
SearchInfo info = new SearchInfo();
info.setContent(rs.getString(1));
info.setCount(rs.getInt(2));
aryResult.add(info);
}
}
}catch(Exception e){
System.out.println("獲得曆史查詢資訊異常"+e.getMessage());
e.printStackTrace();
}finally{
try{
con.close();
}catch(Exception ex){
System.out.println("關閉串連異常"+ex.getMessage());
ex.printStackTrace();
}
}
return aryResult;
}
}
Translation類用於處理資料轉送的編碼問題
package operation;

import java.io.*;

/** *//**
* 該類用於解決編碼問題
* @author 非凡DZ
*
*/
public class Translation {
public Translation() {
}

public static String transCode(String str){
String temp = null;
if(str == null){
temp = "";
}
try {
temp = new String(str.getBytes("iso8859-1"), "utf-8");
} catch (UnsupportedEncodingException ex) {
}
return temp;
}
}

DataBase用於獲得資料庫連接
package db;

import java.sql.*;
/** *//**
* 該類用於擷取資料庫連接
* @author 非凡DZ
*
*/
public class DataBase {
private Connection con;
private String driver = "oracle.jdbc.driver.OracleDriver";
private String url = "jdbc:oracle:thin:@localhost:1521:daizhenghenry";
private String uid = "daizheng";
private String pwd = "daizheng";

public Connection getConnection(){
try{
Class.forName(driver);
con = DriverManager.getConnection(url, uid, pwd);
}catch(Exception e){
System.out.println("串連異常"+e.getMessage());
e.printStackTrace();
}
return con;
}
}

最後是jsp頁面

<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>類比搜尋引擎(From:網路大本營 Http://www.QQview.com)</title>
<style type="text/css">
html
{}{
filter:progid:DXImageTransform.Microsoft.BasicImage(grayscale=1);
}
</style>
<script type='text/javascript' src='/ajaxTest/dwr/interface/history.js'></script>
<script type='text/javascript' src='/ajaxTest/dwr/engine.js'></script>
<script type='text/javascript' src='/ajaxTest/dwr/util.js'></script>
<script language="javascript">
/**//*處理使用者相關搜尋*/
function change(data){
//得到表格中的行數
var count = document.getElementById('tab').rows.length;
//如果表中存在行,將所有行刪除
if(count >0){
for(var i=count-1; i>=0; i--){
document.getElementById('tab').deleteRow(i);
}
}
//如果存在相關搜尋記錄
if(data.length > 0){
document.getElementById('Related').style.display = '';
document.getElementById('x').style.display = '';
for(var i=0; i<data.length; i++){
var objTr = document.getElementById('tab').insertRow();
var objTd1 = objTr.insertCell(0);
objTd1.innerHTML = "<input readonly type='text' "
+"size='35' name='txtHistory' style='border:none; background:#FFFFFF'"
+" value='"+data[i].content+"' onmouseover='overChangeColor(this)'"
+" onmouseleave='leaveChangeColor(this)' "
+"onclick='clickHistory(this)'>";
var objTd2 = objTr.insertCell(1);
objTd2.innerHTML = "<input type='text' name='result' readonly"
+" size='15' style='border:none; background:#FFFFFF; text-align:right'"
+" value='"+data[i].count+"結果"+"' align='right'/>";
objTd2.align = 'right';
}
}else{
document.getElementById('Related').style.display = 'none';
}
}
/**//*關閉曆史查詢記錄*/
function myClose(){
document.getElementById('Related').style.display = 'none';
}
/**//*滑鼠在相關搜尋內容上方時執行*/
function overChangeColor(object){
var histories = document.getElementsByName('txtHistory');
for(var i=0; i<histories.length; i++){
//如果當前滑鼠停留在某一行上
if(histories[i].style.background == '#ccffcc'){
histories[i].style.background = '#FFFFFF';
var tdObj1 = histories[i].parentElement; //td
var trObj1 = tdObj1.parentElement; //tr
var childObj1 = trObj1.childNodes(1);
var x1 = childObj1.childNodes(0);
x1.style.background = '#FFFFFF';
break;
}
}
object.style.background = '#CCFFCC';
var tdObj = object.parentElement; //td
var trObj = tdObj.parentElement; //tr
var childObj = trObj.childNodes(1);
var x = childObj.childNodes(0);
x.style.background = '#CCFFCC';
}
/**//*滑鼠離開相關搜尋內容上方時執行*/
function leaveChangeColor(object){
object.style.background = '#FFFFFF';
var tdObj = object.parentElement; //td
var trObj = tdObj.parentElement; //tr
var childObj = trObj.childNodes(1); //td
var x = childObj.childNodes(0); //input
x.style.background = '#FFFFFF';
}
/**//*滑鼠點擊相關搜尋內容時執行*/
function clickHistory(object){
document.frm.content.value = object.value;
document.getElementById('Related').style.display = 'none';
frm.submit();
}
/**//*使用者在搜尋方塊中按鍵事件處理*/
function keySelectHistory(){
var nKeyCode = window.event.keyCode;
if(nKeyCode == 38 || nKeyCode == 40){
var count = document.getElementById('tab').rows.length;
var tempRowId; //記錄滑鼠懸浮所在行
var flag = false; //標識是否有已經變色的行
if(count > 0 && (nKeyCode == 38 || nKeyCode == 40)){//如果存在相關搜尋資訊
var histories = document.getElementsByName('txtHistory');
for(var i=0; i<histories.length; i++){
//如果當前滑鼠停留在某一行上
if(histories[i].style.background == '#ccffcc'){
tempRowId = i;
flag = true;
break;
}
}
if(!flag){
tempRowId = 0;
}
if(nKeyCode == 38){//向上鍵
if(tempRowId > 0){
leaveChangeColor(histories[tempRowId]);
overChangeColor(histories[tempRowId - 1]);
document.frm.content.value = (histories[tempRowId - 1]).value;
}else{
leaveChangeColor(histories[0]);
overChangeColor(histories[count - 1]);
document.frm.content.value = (histories[count - 1]).value;
}

}else if(nKeyCode == 40){//向下鍵
if(tempRowId == 0 && histories[0].style.background != '#ccffcc'){
overChangeColor(histories[0]);
document.frm.content.value = histories[0].value;
}else if(tempRowId < count -1){
leaveChangeColor(histories[tempRowId]);
overChangeColor(histories[tempRowId + 1]);
document.frm.content.value = (histories[tempRowId + 1]).value;
}else{
leaveChangeColor(histories[tempRowId]);
overChangeColor(histories[0]);
document.frm.content.value = histories[0].value;
}
}
}
}else{//搜尋方塊內容發生改變時(手動使其變化,而非通過上下鍵)
var str = document.frm.content.value;
history.getHistory(str,change);
}
}
</script>
</head>
<body>
<b>類比搜尋引擎</b>
<br />
<form action="ServletX" name="frm" method="post">
<img alt="逝者安息,生者堅強" src="images\daonian.gif" />
<br />
<br />
<input type="hidden" name="model" value="search" />
<input type="text" size="55" name="content"
onkeyup="keySelectHistory()" />
<input type="submit" value="搜尋" />
<div id="Related"
style="border:1px solid #f990033; display:'none'; width:335; ">
<table id="tab" cellpadding="0" border="0" cellspacing="0">
</table>
               
                   
                   
                   
                   
                   
                   
<a id="x" href='javascript:; ' onclick='myClose()'
style="display:none">關閉</a>
</div>
</form>
</body>
</html>

該文章轉載自網路大本營:http://www.qqview.com/Dev/AJAX/200852719810.Html

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.