读书人

Ajax如何显示数据库的数据在jsp页面上

发布时间: 2012-01-07 21:41:55 作者: rapoo

Ajax怎么显示数据库的数据在jsp页面上?(Ajax那里不能发表)
请各位高手帮帮忙,找了好长时间了,大部分是关于.net的,而关于Java的,
又没讲清楚,如果谁知道怎么做,麻烦把代码贴出来,小弟在这里先谢啦!

[解决办法]
//下面是一个用Ajax实现下拉列表的例子,楼主可以试试

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public abstract class BaseAjaxAction extends Action {

public final ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {

String xml = null;
try {
xml = getXmlContent(mapping, form, request, response);
}
catch (Exception ex) {
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
"Can not create response ");
return null;
}

response.setContentType( "text/xml; charset=UTF-8 ");
response.setHeader( "Cache-Control ", "no-cache ");
PrintWriter pw = response.getWriter();
pw.write(xml);
pw.close();

return null;
}

public abstract String getXmlContent(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception;
}


import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

public class DictionaryAction extends BaseAjaxAction {

public String getXmlContent(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {

String responseId = request.getParameter( "responseId ");
String tableName = request.getParameter( "tableName ");
String columnName = request.getParameter( "columnName ");

String selectColumnValue = request.getParameter( "selectColumnValue ");

ArrayList result = new ArrayList();
//Here get data from db
return new AjaxXmlBuilder(responseId).addItems(result, "label ", "value ").toString();
}

public static void main(String args[]) throws Exception {
DictionaryAction ac = new DictionaryAction();
ac.execute(null, null, null, null);
}
}

import java.util.Collection;
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
import java.lang.reflect.InvocationTargetException;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.util.LabelValueBean;

public class AjaxXmlBuilder {

private String encoding = "UTF-8 ";
private List items = new ArrayList();
private String responseId;

public AjaxXmlBuilder() {
}

public AjaxXmlBuilder(String responseId) {
this.responseId = responseId;
}

public String getResponseId() {
return responseId;
}



public void setResponseId(String responseId) {
this.responseId = responseId;
}

public String getEncoding() {
return encoding;
}

public void setEncoding(String encoding) {
this.encoding = encoding;
}

/**
* Add item to XML.
*
* @param name
* The name of the item
* @param value
* The value of the item
* @return
*/
public AjaxXmlBuilder addItem(String name, String value) {
items.add(new LabelValueBean(name, value));
return this;
}

/**
* Add item wrapped with inside a CDATA element.
*
* @param name
* The name of the item
* @param value
* The value of the item
* @return
*/
public AjaxXmlBuilder addItemAsCData(String name, String value) {
items.add(new LabelValueBean(name, value));
return this;
}

/**
* Add items from a collection.
*
* @param collection
* @param nameProperty
* @param valueProperty
* @return
* @throws IllegalAccessException
* @throws InvocationTargetException
* @throws NoSuchMethodException
*/
public AjaxXmlBuilder addItems(Collection collection, String nameProperty,
String valueProperty) throws IllegalAccessException,
InvocationTargetException, NoSuchMethodException {
return addItems2(collection, nameProperty, valueProperty);
}

/**
* Add items from a collection.
*
* @param collection
* @param nameProperty
* @param valueProperty
* @return
* @throws IllegalAccessException
* @throws InvocationTargetException
* @throws NoSuchMethodException
*/
public AjaxXmlBuilder addItems2(Collection collection, String nameProperty,
String valueProperty)
throws IllegalAccessException, InvocationTargetException,
NoSuchMethodException {
for (Iterator iter = collection.iterator(); iter.hasNext();) {
Object element = (Object) iter.next();
String name = BeanUtils.getProperty(element, nameProperty);
String value = BeanUtils.getProperty(element, valueProperty);
items.add(new LabelValueBean(name, value));
}
return this;
}

/**
* Add items from a collection as CDATA element.
*
* @param collection
* @param nameProperty
* @param valueProperty
* @return
* @throws IllegalAccessException
* @throws InvocationTargetException
* @throws NoSuchMethodException
*/
public AjaxXmlBuilder addItemsAsCData(Collection collection,
String nameProperty, String valueProperty)
throws IllegalAccessException, InvocationTargetException,
NoSuchMethodException {
return addItems2(collection, nameProperty, valueProperty);
}

/**
* @see java.lang.Object#toString()
*/
public String toString() {
StringBuffer xml = new StringBuffer().append( " <?xml version=\ "1.0\ " ");
if (encoding != null) {
xml.append( " encoding=\ " ");
xml.append(encoding);
xml.append( "\ " ");
}
xml.append( " ?> ");

xml.append( " <ajax-response> ");
xml.append( " <response type=\ "object\ " id=\ " "+responseId+ "\ "> ");
for (Iterator iter = items.iterator(); iter.hasNext();) {
LabelValueBean item = (LabelValueBean) iter.next();
xml.append( " <item> ");
xml.append( " <name> ");


// if (item.isAsCData()) {
// xml.append( " <![CDATA[ ");
// }
xml.append(item.getLabel());
// if (item.isAsCData()) {
// xml.append( "]]> ");
// }
xml.append( " </name> ");
xml.append( " <value> ");
// if (item.isAsCData()) {
// xml.append( " <![CDATA[ ");
// }
xml.append(item.getValue());
// if (item.isAsCData()) {
// xml.append( "]]> ");
// }
xml.append( " </value> ");
xml.append( " </item> ");
}
xml.append( " </response> ");
xml.append( " </ajax-response> ");

return xml.toString();

}
}

[解决办法]
来晚了,顶
客户端通过xmlHttp.responseXML进行解析
[解决办法]
简单的方法:
XMLHttprequest即可,不需要什么复杂的框架
[解决办法]
从数据库得到的数据会写在xml中,然后返回给前台。通过xmlHttp.responseXML将数据进行解析。得到解析的结果放在页面中显示。
多看看ajax,我想楼主还没完全理解ajax
[解决办法]
用ajax调用了controller 以后,会根据条件把你所要的数据写成xml ,通过xmlHttp.responseXML 就能得到你所要的了
[解决办法]
xmlHttp.responseXML是一个document对象,要调用它的一些方法才能获得数据,如
var xmlDoc = xmlHttp.responseXML;
//得到父节点
var org = xmlDoc.getElementsByTagName( "parentNode ")[0];
//父节点组织机构代码
var orgCode = org.childNodes[0].firstChild.nodeValue;
//父节点的名字
var name = org.childNodes[1].firstChild.nodeValue;

读书人网 >Java Web开发

热点推荐