Web级联菜单StrutsWord格式.docx

上传人:b****4 文档编号:7874676 上传时间:2023-05-09 格式:DOCX 页数:16 大小:184.90KB
下载 相关 举报
Web级联菜单StrutsWord格式.docx_第1页
第1页 / 共16页
Web级联菜单StrutsWord格式.docx_第2页
第2页 / 共16页
Web级联菜单StrutsWord格式.docx_第3页
第3页 / 共16页
Web级联菜单StrutsWord格式.docx_第4页
第4页 / 共16页
Web级联菜单StrutsWord格式.docx_第5页
第5页 / 共16页
Web级联菜单StrutsWord格式.docx_第6页
第6页 / 共16页
Web级联菜单StrutsWord格式.docx_第7页
第7页 / 共16页
Web级联菜单StrutsWord格式.docx_第8页
第8页 / 共16页
Web级联菜单StrutsWord格式.docx_第9页
第9页 / 共16页
Web级联菜单StrutsWord格式.docx_第10页
第10页 / 共16页
Web级联菜单StrutsWord格式.docx_第11页
第11页 / 共16页
Web级联菜单StrutsWord格式.docx_第12页
第12页 / 共16页
Web级联菜单StrutsWord格式.docx_第13页
第13页 / 共16页
Web级联菜单StrutsWord格式.docx_第14页
第14页 / 共16页
Web级联菜单StrutsWord格式.docx_第15页
第15页 / 共16页
Web级联菜单StrutsWord格式.docx_第16页
第16页 / 共16页
亲,该文档总共16页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

Web级联菜单StrutsWord格式.docx

《Web级联菜单StrutsWord格式.docx》由会员分享,可在线阅读,更多相关《Web级联菜单StrutsWord格式.docx(16页珍藏版)》请在冰点文库上搜索。

Web级联菜单StrutsWord格式.docx

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>

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 自然科学 > 物理

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2