日期:2014-05-18  浏览次数:20701 次

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() {
}

pub