这是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的动态搜索///////////////
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);
}
//生成表格内容行
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>
关键字搜索:<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");////编码转换GB2312
System.out.println(name+" 关键字!");
if (name.length()<1 ) {
name ="0"; ////搜索为空(也就是没数据返回)
}
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 阅读(687)
评论(0) 编辑 收藏 所属分类:
AJAX