java中ajax如何获得数据库提取出来的多个数据
我是在一个网页弄很多层,比如说有一个日志标题列表,当你点其中的日志标题!会根据ID找到日志的具体信息显示!能给一个完整点的案例吗?谢谢各位...
我是在一个网页弄很多层,比如说有一个日志标题列表,当你点其中的日志标题!会根据ID找到日志的具体信息显示!
能给一个完整点的案例吗?谢谢各位 展开
能给一个完整点的案例吗?谢谢各位 展开
展开全部
dom
下面是一部分程序代码
PreparedStatement ps = con.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
while(rs.next()){
business = document.createElement("business");
root.appendChild(business);
info_id = document.createElement("info_id");
info_id.appendChild(document.createTextNode(rs.getString("info_id")));
business.appendChild(info_id);
create_name = document.createElement("create_name");
create_name.appendChild(document.createTextNode(rs.getString("create_name")));
business.appendChild(create_name);
create_time = document.createElement("create_time");
create_time.appendChild(document.createTextNode(rs.getString("create_time")));
business.appendChild(create_time);
title = document.createElement("title");
title.appendChild(document.createTextNode(rs.getString("title")));
business.appendChild(title);
content = document.createElement("content");
content.appendChild(document.createTextNode(rs.getString("content")));
business.appendChild(content);
}
Element firstPageEL,prePageEL,nextPageEL,lastPageEL;
firstPageEL = document.createElement("firstPage");
root.appendChild(firstPageEL);
firstPageEL.appendChild(document.createTextNode(firstPage));
prePageEL = document.createElement("prePage");
root.appendChild(prePageEL);
prePageEL.appendChild(document.createTextNode(prePage));
nextPageEL = document.createElement("nextPage");
root.appendChild(nextPageEL);
nextPageEL.appendChild(document.createTextNode(nextPage));
lastPageEL = document.createElement("lastPage");
root.appendChild(lastPageEL);
lastPageEL.appendChild(document.createTextNode(lastPage));
/**Element root = document.createElement("root");
document.appendChild(root);
Element title = document.createElement("title");
title.appendChild(document.createTextNode("yaowei"));
root.appendChild(title);
Element content = document.createElement("content");
content.appendChild(document.createTextNode("yaoweijq"));
root.appendChild(content);
*/
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
DOMSource source = new DOMSource(document);
transformer.setOutputProperty(OutputKeys.ENCODING,"GB2312");
transformer.setOutputProperty(OutputKeys.INDENT,"yes");
StringWriter s = new StringWriter();
StreamResult result1 = new StreamResult(new BufferedWriter(s));
transformer.transform(source,result1);
StringBuffer b = s.getBuffer();
con.close();
result = b.toString();
WEB端
function displayTopic(currPageNum){
send_request("get","private/displayTopic.jsp?currPageNum="+currPageNum,null,"text",listTopic);
}
function listTopic(){
if(http_request.readyState == 4){
if(http_request.status == 200){
var doc = http_request.responseText;
//alert(doc);
var xmlDoc = new ActiveXObject("Microsoft.xmldom");
xmlDoc.async = "false";
xmlDoc.loadXML(doc);
var content = xmlDoc.getElementsByTagName("topic");
var html = "";
html += "<table frame=box >";
//alert(html);
//alert(html);
//alert(content[0].getElementsByTagName("create_name")[0].firstChild.data);
for( var i = 0;i<content.length;i++){
html += "<tr>";
html += "<td colspan=2>标题:" +
content[i].getElementsByTagName("title")[0].firstChild.data + "</td></tr>";
html += "<tr>";
html += "<td>发表人:"+content[i].getElementsByTagName("create_name")[0].firstChild.data
+ "</td>";
html += "<td>发表时间:"+content[i].getElementsByTagName("create_time")[0].firstChild.data
+ "</td></tr>";
html += "<tr>";
html += "<td colspan=2>"+content[i].getElementsByTagName("content")[0].firstChild.data
+ "</td></tr>";
}
var firstPage = xmlDoc.getElementsByTagName("firstPage")[0].firstChild.data;
var prePage = xmlDoc.getElementsByTagName("prePage")[0].firstChild.data;
var nextPage = xmlDoc.getElementsByTagName("nextPage")[0].firstChild.data;
var lastPage = xmlDoc.getElementsByTagName("lastPage")[0].firstChild.data;
if(firstPage != "false" || prePage != "false" || nextPage != "false" || lastPage != "false"){
html += "<tr>";
html += "<td colspan = 2>";
if(firstPage != "false"){
html += "<input type=\"button\" value=\"首页\" onClick=\"javascript:displayTopic(0)\"";
html += "/>";
}
if(prePage != "false"){
html += "<input type=\"button\" value=\"前一页\" ";
html += "onClick=\"javascript:displayTopic("+prePage+")\"/>";
}
if(nextPage != "false"){
html += "<input type=\"button\" value=\"下一页\" ";
html += "onClick=\"javascript:displayTopic("+nextPage+")\"/>";
}
if(lastPage != "false"){
html += "<input type=\"button\" value=\"尾页\" ";
//html += "onClick=\"javascript:displayTopic(\"lastPage\");\" />";
html += "onClick=displayTopic(\"lastPage\") />";
}
html += "</td></tr>";
}
html += "</table>";
//alert(html);
document.getElementById("topicList").innerHTML = "";
document.getElementById("topicList").innerHTML = html;
//alert("finish");
}else{
alert("页面异常"+http_request.status);
}
}
}
下面是一部分程序代码
PreparedStatement ps = con.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
while(rs.next()){
business = document.createElement("business");
root.appendChild(business);
info_id = document.createElement("info_id");
info_id.appendChild(document.createTextNode(rs.getString("info_id")));
business.appendChild(info_id);
create_name = document.createElement("create_name");
create_name.appendChild(document.createTextNode(rs.getString("create_name")));
business.appendChild(create_name);
create_time = document.createElement("create_time");
create_time.appendChild(document.createTextNode(rs.getString("create_time")));
business.appendChild(create_time);
title = document.createElement("title");
title.appendChild(document.createTextNode(rs.getString("title")));
business.appendChild(title);
content = document.createElement("content");
content.appendChild(document.createTextNode(rs.getString("content")));
business.appendChild(content);
}
Element firstPageEL,prePageEL,nextPageEL,lastPageEL;
firstPageEL = document.createElement("firstPage");
root.appendChild(firstPageEL);
firstPageEL.appendChild(document.createTextNode(firstPage));
prePageEL = document.createElement("prePage");
root.appendChild(prePageEL);
prePageEL.appendChild(document.createTextNode(prePage));
nextPageEL = document.createElement("nextPage");
root.appendChild(nextPageEL);
nextPageEL.appendChild(document.createTextNode(nextPage));
lastPageEL = document.createElement("lastPage");
root.appendChild(lastPageEL);
lastPageEL.appendChild(document.createTextNode(lastPage));
/**Element root = document.createElement("root");
document.appendChild(root);
Element title = document.createElement("title");
title.appendChild(document.createTextNode("yaowei"));
root.appendChild(title);
Element content = document.createElement("content");
content.appendChild(document.createTextNode("yaoweijq"));
root.appendChild(content);
*/
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
DOMSource source = new DOMSource(document);
transformer.setOutputProperty(OutputKeys.ENCODING,"GB2312");
transformer.setOutputProperty(OutputKeys.INDENT,"yes");
StringWriter s = new StringWriter();
StreamResult result1 = new StreamResult(new BufferedWriter(s));
transformer.transform(source,result1);
StringBuffer b = s.getBuffer();
con.close();
result = b.toString();
WEB端
function displayTopic(currPageNum){
send_request("get","private/displayTopic.jsp?currPageNum="+currPageNum,null,"text",listTopic);
}
function listTopic(){
if(http_request.readyState == 4){
if(http_request.status == 200){
var doc = http_request.responseText;
//alert(doc);
var xmlDoc = new ActiveXObject("Microsoft.xmldom");
xmlDoc.async = "false";
xmlDoc.loadXML(doc);
var content = xmlDoc.getElementsByTagName("topic");
var html = "";
html += "<table frame=box >";
//alert(html);
//alert(html);
//alert(content[0].getElementsByTagName("create_name")[0].firstChild.data);
for( var i = 0;i<content.length;i++){
html += "<tr>";
html += "<td colspan=2>标题:" +
content[i].getElementsByTagName("title")[0].firstChild.data + "</td></tr>";
html += "<tr>";
html += "<td>发表人:"+content[i].getElementsByTagName("create_name")[0].firstChild.data
+ "</td>";
html += "<td>发表时间:"+content[i].getElementsByTagName("create_time")[0].firstChild.data
+ "</td></tr>";
html += "<tr>";
html += "<td colspan=2>"+content[i].getElementsByTagName("content")[0].firstChild.data
+ "</td></tr>";
}
var firstPage = xmlDoc.getElementsByTagName("firstPage")[0].firstChild.data;
var prePage = xmlDoc.getElementsByTagName("prePage")[0].firstChild.data;
var nextPage = xmlDoc.getElementsByTagName("nextPage")[0].firstChild.data;
var lastPage = xmlDoc.getElementsByTagName("lastPage")[0].firstChild.data;
if(firstPage != "false" || prePage != "false" || nextPage != "false" || lastPage != "false"){
html += "<tr>";
html += "<td colspan = 2>";
if(firstPage != "false"){
html += "<input type=\"button\" value=\"首页\" onClick=\"javascript:displayTopic(0)\"";
html += "/>";
}
if(prePage != "false"){
html += "<input type=\"button\" value=\"前一页\" ";
html += "onClick=\"javascript:displayTopic("+prePage+")\"/>";
}
if(nextPage != "false"){
html += "<input type=\"button\" value=\"下一页\" ";
html += "onClick=\"javascript:displayTopic("+nextPage+")\"/>";
}
if(lastPage != "false"){
html += "<input type=\"button\" value=\"尾页\" ";
//html += "onClick=\"javascript:displayTopic(\"lastPage\");\" />";
html += "onClick=displayTopic(\"lastPage\") />";
}
html += "</td></tr>";
}
html += "</table>";
//alert(html);
document.getElementById("topicList").innerHTML = "";
document.getElementById("topicList").innerHTML = html;
//alert("finish");
}else{
alert("页面异常"+http_request.status);
}
}
}
展开全部
建议使用dwr框架,dwr实现ajax很方便的,他可以让你用js远程调用你的java类,以及让你的java类控制你的js
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2009-03-08
展开全部
1:用dom解析
2:用json
3:用dwr
随便哪一种
2:用json
3:用dwr
随便哪一种
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询