表单数据关联StrutsWord下载.docx
- 文档编号:17156581
- 上传时间:2022-11-28
- 格式:DOCX
- 页数:26
- 大小:430.06KB
表单数据关联StrutsWord下载.docx
《表单数据关联StrutsWord下载.docx》由会员分享,可在线阅读,更多相关《表单数据关联StrutsWord下载.docx(26页珍藏版)》请在冰豆网上搜索。
base/>
metahttp-equiv="
Content-Type"
content="
text/html;
charset=gb2312"
title>
AJAX技术的实例<
/title>
scripttype="
text/javascript"
src="
order.js"
>
/script>
scriptlanguage="
javascript"
varhttpRequest;
functioncheckFormData()
{
varsubscriptionID=document.getElementById("
subscriptionID"
).value;
if((subscriptionID==null)||(subscriptionID=="
"
))
{
alert("
您没有输入SubscriptionID!
);
returnfalse;
}
varselect=document.getElementById("
select"
if((select==null)||(select=="
您没有选择select!
returntrue;
}
pragma"
no-cache"
cache-control"
expires"
0"
keywords"
keyword1,keyword2,keyword3"
description"
Thisismypage"
--
linkrel="
stylesheet"
type="
text/css"
href="
styles.css"
-->
/head>
bodyonload="
startup()"
formaction="
/processorder.do"
method="
post"
onsubmit="
returncheckFormData()"
tablewidth="
453"
border="
align="
center"
tr>
tdwidth="
228"
输入您的SubscriptionID(yang):
textproperty="
onblur="
validate(this.form)"
/>
215"
bid="
showErrorText"
/b>
/td>
/tr>
td>
<
divalign="
radioproperty="
displayList('
author'
)"
value="
author"
/>
&
nbsp;
根据作者
/div>
pubs'
pubs"
根据出版社
divid="
selectionList"
SelectTitle:
br>
titles"
submitproperty="
order"
Order"
&
&
resetproperty="
cancel"
Cancel"
/table>
/html:
form>
/body>
html>
3、由于在前面的order.jsp中使用了一个外部order.js
(1)右击站点的根目录
输入文件名称为order.js
(2)修改该文件在保存时的编码方式
由于Eclipse在,默认时是以UTF-8的编码方式保存*.js文件,因此在其中的中文将会被改变为乱码。
应该改变为gb2312编码方式。
改变为gb2312
(3)编程该order.js文件
varurl;
functionstartup()
document.forms[0].subscriptionID.focus=true;
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;
functioninit()
httpRequest=getHTTPRequestObject();
varurl="
/StrutsAJAXWebApp/processorder.do"
;
httpRequest.open("
POST"
url,true);
httpRequest.setRequestHeader("
"
application/x-www-form-urlencoded"
functionvalidate(formObj)
init();
httpRequest.onreadystatechange=processHttpResponseSubscriptionValidator;
httpRequest.send("
subscriptionID="
+formObj.subscriptionID.value);
functiondisplayList(field)
titles.innerHTML="
"
httpRequest.onreadystatechange=processHttpResponseListHandler;
select="
+escape(field));
functiondisplayTitles(formObj)
varindex=formObj.list.selectedIndex;
varval=formObj.list.options[index].value;
httpRequest.onreadystatechange=processHttpResponseTitlesHandler;
list="
+val);
functionprocessHttpResponseSubscriptionValidator()//该函数实现对用户的ID进行实时识别的
if(httpRequest.readystate==4)
if(httpRequest.status==200)
varmessageObj=httpRequest.responseXML.getElementsByTagName("
message"
)[0];
varmessage=messageObj.childNodes[0].nodeValue;
if(message=="
)
document.getElementById("
).innerHTML="
您输入的SubscriptionID是有效的!
document.LibraryForm.order.disabled=false;
else
您输入的SubscriptionID是无效的!
document.LibraryForm.order.disabled=true;
elseif(httpRequest.status==404)
没有找到与所请求的文件相匹配的资源!
else
你所请求的页面发生异常,错误代码为:
+httpRequest.status);
document.getElementById("
检测中,请稍等..."
functionprocessHttpResponseTitlesHandler()
document.getElementById("
//除掉状态提示文字
vartitles=document.getElementById("
varindexObj=httpRequest.responseXML.getElementsByTagName("
index"
varindex=indexObj.childNodes[0].nodeValue;
vartemp="
selectname=\"
titles\"
multiple>
for(vari=0;
i<
index;
i++){
varlistObj=httpRequest.responseXML.getElementsByTagName("
list"
)[i];
temp=temp+"
optionvalue="
+i+"
+listObj.childNodes[0].nodeValue+"
/<
option>
temp=temp+"
/select>
titles.innerHTML=temp;
functionprocessHttpResponseListHandler()
varprefix;
varlist=document.getElementById("
varauthorOption=document.getElementById("
)
if(authorOption.checked){
prefix="
A"
}else{
P"
list\"
onchange=\"
displayTitles(this.form)\"
+prefix+i+"
/option>
/select>
list.innerHTML=temp;
4、添加ActionForm
设置该ActionForm的名称为orderActionForm,类名称为com.px1987.webajax.actionform.OrderActionForm
并且添加各个相关的属性(包括所有的属性-----静态和动态)
5、添加Action类
设置该Action类的特征参数/processorder,com.px1987.webajax.action.OrderAction
(1)将产生出下面的
(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;
importcom.px1987.webajax.actionform.OrderActionForm;
importjava.util.*;
importjava.io.*;
publicclassOrderActionextendsAction
privateHashtabletitles;
finalprivateStringvalidID="
yang"
//实际改变为对数据库表访问
publicActionForwardexecute(ActionMappingmapping,ActionFormform,
HttpServletRequestrequest,HttpServletResponseresponse)
OrderActionFormorderActionForm=(OrderActionForm)form;
//TODOAuto-generatedmethodstub
this.populateTitles();
Stringoption=null;
Stringkey=null;
String[]list=null;
StringxmlString=null;
StringsubscriptionID=null;
//下面的代码是实现实时校验
subscriptionID=request.getParameter("
if(subscriptionID!
=null)
Stringstatus="
message>
+this.checkValidID(subscriptionID)+"
/message>
this.writeResponse(response,status);
//下面的代码是实现根据用户对“作者”或者“出版社”的类型选择来动态地获得对应的信息以放到第一下拉列表框中
option=request.getParameter("
key=request.getParameter("
if(option!
if(option.equals("
list=this.populateAuthors();
xmlString=this.getXMLData(list);
this.writeResponse(response,xmlString);
elseif(option.equals("
list=this.populatePubs();
//下面的代码是实现根据用户对第一下拉列表框中的选择的项目,对应地获得相应的信息并在第二个下拉列表框中显示输出
if(key!
list=this.getTitle(key);
returnnull;
publicvoidwriteResponse(HttpServletResponseresp,Stringoutput)
resp.setContentType("
text/xml;
charset=GBK"
resp.setHeader("
Cache-Control"
try
resp.getWriter().write(output);
catch(IOExceptione)
System.out.println(e.getMessage());
publicString[]getTitle(Stringkey)
return(String[])titles.get(key);
privateString[]populateAuthors()
{//在实际应用中,该数据可以来源于某个数据库表的某列
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 表单 数据 关联 Struts