| 发表于:2007-08-10 16:05:301楼 得分:10 |
//下面是一个用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(); } } | | |
|