Web级联菜单StrutsWord格式.docx
《Web级联菜单StrutsWord格式.docx》由会员分享,可在线阅读,更多相关《Web级联菜单StrutsWord格式.docx(16页珍藏版)》请在冰点文库上搜索。
![Web级联菜单StrutsWord格式.docx](https://file1.bingdoc.com/fileroot1/2023-5/9/e41204b7-9265-40ac-b843-8fa0bed137f0/e41204b7-9265-40ac-b843-8fa0bed137f01.gif)
htmllang="
true"
<
head>
base/>
<
scriptlanguage="
javascript"
src="
ajaxMenu.js"
/script>
title>
ajaxMenu1.jsp<
/title>
metahttp-equiv="
pragma"
content="
no-cache"
cache-control"
expires"
0"
keywords"
keyword1,keyword2,keyword3"
description"
Thisismypage"
--
linkrel="
stylesheet"
type="
text/css"
href="
styles.css"
-->
/head>
body>
tablewidth="
200"
border="
cellspacing="
cellpadding="
tr>
tdheight="
20"
ahref="
#"
onclick="
showSubMenu('
menu_1'
)"
>
菜单一
/a>
/td>
/tr>
id="
menu_1"
style="
display:
none"
&
nbsp;
menu_2'
菜单二
tr>
menu_2"
/table>
/body>
/html:
html>
2、再添加JavaScript的文件ajaxMenu.js
文件名称为
编程该文件
varhttpRequest;
varcurrentPos;
functionshowSubMenu(newCurrentPos)
{
document.getElementById(newCurrentPos).parentNode.style.display="
"
;
currentPos=newCurrentPos;
if(document.getElementById(newCurrentPos).style.display=="
)
{
document.getElementById(newCurrentPos).style.display="
sendRequest("
/AJAXWebApp/ajaxmenu.do?
playPos="
+newCurrentPos);
}
else
}
functionsendRequest(requestURL)
httpRequest=getHTTPRequestObject();
httpRequest.onreadystatechange=processHttpResponse;
httpRequest.open("
GET"
requestURL,true);
//Get请求方式的代码
httpRequest.send(null);
functiongetHTTPRequestObject()
varhttpRequestObject;
if(window.XMLHttpRequest)
{//Mozilla,Safari,...
httpRequestObject=newXMLHttpRequest();
elseif(window.ActiveXObject)
{//IE
try
httpRequestObject=newActiveXObject("
Microsoft.XMLHTTP"
);
catch(e)
try
{
httpRequestObject=newActiveXObject("
Msxml2.XMLHTTP"
}
catch(e2)
alert("
您的浏览器有问题!
returnhttpRequestObject;
functionprocessHttpResponse()
if(httpRequest.readyState==4)
if(httpRequest.status==200)
{//检查是否成功接收了服务器响应
parseResponseText();
elseif(httpRequest.status==404)
alert("
没有找到与所请求的文件相匹配的资源!
你所请求的页面发生异常,错误代码为:
+httpRequest.status);
document.getElementById(currentPos).innerHTML="
检测中,请稍等..."
functionparseResponseText()
document.getElementById(currentPos).innerHTML=httpRequest.responseText;
functionLoadJS(file)//动态加载JS文件
varhead=document.getElementsByTagName('
HEAD'
).item(0);
varscript=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类
packagecom.px1987.webajax.action;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importorg.apache.struts.action.Action;
importorg.apache.struts.action.ActionForm;
importorg.apache.struts.action.ActionForward;
importorg.apache.struts.action.ActionMapping;
importjava.io.*;
publicclassAjaxMenuActionextendsAction
publicActionForwardexecute(ActionMappingmapping,ActionFormform,
HttpServletRequestrequest,HttpServletResponseresponse)
response.setContentType("
text/html;
charset=GBK"
PrintWriterout=response.getWriter();
StringplayPos=request.getParameter("
playPos"
if(playPos.equals("
))
{//实际可以访问数据库表来获得各个子菜单项目
StringsubMenuText="
&
ahref='
#'
菜单一中的子菜单一<
br>
+
"
菜单一中的子菜单二<
菜单一中的子菜单三<
out.write(subMenuText);
out.close();
}
elseif(playPos.equals("
))
{
菜单二中的子菜单一<
菜单二中的子菜单二<
菜单二中的子菜单三<
菜单二中的子菜单四<
catch(IOExceptione)
System.out.println(e.getMessage());
returnnull;
4、执行该功能
(1)部署它
(2)输入http:
//127.0.0.1:
8080/StrutsAJAXWebApp/ajaxMenu.jsp
点击上面的菜单的链接,将展开对应的菜单项目
如果再次点击上面的菜单的链接,将关闭对应的菜单项目
从Java类产生XML
把Ajax响应作为XML来传递有许多原因:
每个支持Ajax的浏览器都有导航XML文档的方法,也有许多服务器端技术可以处理XML数据。
通过制定一个方案,描述要交换的文档类型,在Ajax客户端和服务器端之间很容易定义合约,而且如果服务器端架构采用面向服务的方式,那么使用XML也可以允许非Ajax客户机使用您提供的数据。
1、自行进行序列化
首先,可以从对象图以编程的方式生成XML。
这种方式可以简单到只是在每个JavaBean类中实现toXml()方法即可。
然后就可以选择合适的XMLAPI,让每个JavaBean提供表示自己状态的元素,并递归地对自己的成员调用对象图。
2、Order类的toXml()的JDOM实现
(1)在线商店中的模型
下面的这个模型代表在线商店中的顾客帐户。
顾客拥有以前订单的集合,每个订单包含几个商品。
(2)代码
publicElementtoXml()
ElementelOrder=newElement("
order"
elOrder.setAttribute("
id"
id);
cost"
getFormattedCost());
ElementelDate=newElement("
date"
).addContent(date);
elOrder.addContent(elDate);
ElementelItems=newElement("
items"
for(Iterator<
Item>
iter=
items.iterator();
iter.hasNext();
){
elItems.addContent(iter.next().toXml());
elOrder.addContent(elItems);
returnelOrder;
(3)从JDOM元素生成XML响应
publicvoiddoGet(HttpServletRequestreq,HttpServletResponseres)
throwsjava.io.IOException,ServletException
StringcustId=req.getParameter("
username"
Customercustomer=getCustomer(custId);
ElementresponseElem=customer.toXml();
DocumentresponseDoc=newDocument(responseElem);
res.setContentType("
application/xml"
newXMLOutputter().output(responseDoc,res.getWriter());
JDOM再次把工作变得非常简单。
只需要在对象图返回的XML元素外面包装一个Document,然后用XMLOutputter把文档写入servlet响应即可。
(4)代表顾客的XML文档
下面显示了用这种方式生成的XML示例,用JDOMFormat.getPrettyFormat()对XMLOutputter进行初始化,格式化得非常好。
在这个示例中,顾客只做了一个订单,包含两个商品。
?
xmlversion="
1.0"
encoding="
UTF-8"
customerusername="
jimmy66"
realname>
JamesHyrax<
/realname>
orders>
orderid="
o-11123"
cost="
$349.98"
date>
08-26-2005<
/date>
items>
itemid="
i-55768"
name>
Oolong512MBCFCard<
/name>
description>
512MegabyteType1CompactFlashcard.
ManufacturedbyOolongIndustries<
/description>
price>
$49.99<
/price>
/item>
i-74491"
FujakSuperpix72Camera<
7.2Megapixeldigitalcamerafeaturingsix
shootingmodesand3xopticalzoom.Silver.<
$299.99<
/items>
/order>
/orders>
/customer>