1、html lang=true base / titleajaxMenu1.jspmeta http-equiv=pragma content=no-cachecache-controlexpires0keywordskeyword1,keyword2,keyword3descriptionThis is my page-link rel=stylesheet type=text/css href=styles.css -/headbodytable width=200 border= cellspacing= cellpadding=trtd height=20a href=# onclick
2、=showSubMenu(menu_1) 菜单一/a/td/tr id=menu_1 style=display:none menu_2菜单二tr menu_2/table/body/html:html2、再添加JavaScript的文件ajaxMenu.js文件名称为编程该文件var httpRequest;var currentPos;function showSubMenu(newCurrentPos) document.getElementById(newCurrentPos).parentNode.style.display=; currentPos=newCurrentP
3、os; if(document.getElementById(newCurrentPos).style.display = ) document.getElementById(newCurrentPos).style.display = sendRequest(/AJAXWebApp/ajaxmenu.do?playPos=+newCurrentPos); elsefunction sendRequest(requestURL) httpRequest=getHTTPRequestObject(); httpRequest.onreadystatechange = processHttpRes
4、ponse; httpRequest.open(GET, requestURL, true); / Get 请求方式的代码 httpRequest.send(null);function getHTTPRequestObject() var httpRequestObject; if (window.XMLHttpRequest) / Mozilla, Safari, . httpRequestObject = new XMLHttpRequest(); else if (window.ActiveXObject) / IE try httpRequestObject = new Active
5、XObject(Microsoft.XMLHTTP); catch (e) try httpRequestObject= new ActiveXObject(Msxml2.XMLHTTP catch (e2) alert(您的浏览器有问题! return httpRequestObject;function processHttpResponse () if (httpRequest.readyState = 4) if (httpRequest.status = 200) /检查是否成功接收了服务器响应 parseResponseText(); else if(httpRequest.sta
6、tus = 404) alert(没有找到与所请求的文件相匹配的资源!你所请求的页面发生异常,错误代码为:+httpRequest.status); document.getElementById(currentPos).innerHTML = 检测中,请稍等.function parseResponseText() document.getElementById(currentPos).innerHTML=httpRequest.responseText;function LoadJS(file) /动态加载JS文件 var head = document.getElementsByTagN
7、ame(HEAD).item(0); var script = document.createElement(SCRIPT script.src = file; script.type = text/javascript head.appendChild(script);3、添加一个Action类(1)类名称为AjaxMenuAction,包名称为com.px1987.webajax.action /ajaxmenu将产生出下面的Action类(2)编程该Action 类package com.px1987.webajax.action;import javax.servlet.http.Ht
8、tpServletRequest;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 java.io.*;public class AjaxMenuAction extends Action pub
9、lic ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) response.setContentType(text/html; charset=GBK PrintWriter out = response.getWriter(); String playPos = request.getParameter(playPos if(playPos.equals() /实际可以访问数据库表来获得各个子菜单项目 S
10、tring subMenuText= &a href=#菜单一中的子菜单一+ 菜单一中的子菜单二菜单一中的子菜单三 out.write(subMenuText); out.close(); else if(playPos.equals() ) 菜单二中的子菜单一菜单二中的子菜单二菜单二中的子菜单三菜单二中的子菜单四 catch(IOException e) System.out.println(e.getMessage(); return null;4、执行该功能(1)部署它(2)输入http:/127.0.0.1:8080/StrutsAJAXWebApp/ajaxMenu.jsp点击上面的
11、菜单的链接,将展开对应的菜单项目如果再次点击上面的菜单的链接,将关闭对应的菜单项目从 Java 类产生 XML把 Ajax 响应作为 XML 来传递有许多原因:每个支持 Ajax 的浏览器都有导航 XML 文档的方法,也有许多服务器端技术可以处理 XML 数据。通过制定一个方案,描述要交换的文档类型,在 Ajax 客户端和服务器端之间很容易定义合约,而且如果服务器端架构采用面向服务的方式,那么使用 XML 也可以允许非 Ajax 客户机使用您提供的数据。1、自行进行序列化首先,可以从对象图以编程的方式生成 XML。这种方式可以简单到只是在每个 JavaBean 类中实现 toXml() 方法即
12、可。然后就可以选择合适的 XML API,让每个 JavaBean提供表示自己状态的元素,并递归地对自己的成员调用对象图。2、Order 类的 toXml() 的 JDOM 实现(1)在线商店中的模型下面的这个模型代表在线商店中的顾客帐户。顾客拥有以前订单的集合,每个订单包含几个商品。(2)代码 public Element toXml() Element elOrder = new Element(order elOrder.setAttribute(id,id);cost,getFormattedCost(); Element elDate = new Element(date).addC
13、ontent(date); elOrder.addContent(elDate); Element elItems = new Element(items for (Iterator iter = items.iterator() ; iter.hasNext() ; ) elItems.addContent(iter.next().toXml(); elOrder.addContent(elItems); return elOrder;(3)从 JDOM 元素生成 XML 响应 public void doGet(HttpServletRequest req, HttpServletResp
14、onse res) throws java.io.IOException, ServletException String custId = req.getParameter(username Customer customer = getCustomer(custId); Element responseElem = customer.toXml(); Document responseDoc = new Document(responseElem); res.setContentType(application/xml new XMLOutputter().output(responseD
15、oc,res.getWriter();JDOM 再次把工作变得非常简单。只需要在对象图返回的 XML 元素外面包装一个 Document,然后用 XMLOutputter 把文档写入 servlet 响应即可。(4)代表顾客的 XML 文档下面显示了用这种方式生成的 XML 示例,用 JDOM Format.getPrettyFormat() 对 XMLOutputter 进行初始化,格式化得非常好。在这个示例中,顾客只做了一个订单,包含两个商品。 ?xml version=1.0 encoding=UTF-8customer username=jimmy66realnameJames Hyr
16、axordersorder id=o-11123 cost=$349.98date08-26-2005itemsitem id=i-55768nameOolong 512MB CF Carddescription512 Megabyte Type 1 CompactFlash card. Manufactured by Oolong Industriesprice$49.99/itemi-74491Fujak Superpix72 Camera7.2 Megapixel digital camera featuring six shooting modes and 3x optical zoom. Silver.$299.99/order/orders/customer
copyright@ 2008-2023 冰点文库 网站版权所有
经营许可证编号:鄂ICP备19020893号-2