表单数据关联Struts.docx - 冰豆网
表单数据关联Struts.docx
- 文档编号:4240597
- 上传时间:2022-11-28
- 格式:DOCX
- 页数:26
- 大小:430.06KB
表单数据关联Struts.docx
《表单数据关联Struts.docx》由会员分享,可在线阅读,更多相关《表单数据关联Struts.docx(26页珍藏版)》请在冰豆网上搜索。
表单数据关联Struts
利用AJAX技术实现实时(表单数据关联)订单功能的Web应用
1、新建一个Web应用的Project,名称为StrutsAJAXWebApp,同时再添加一个Web组件,其Context的名称为StrutsAJAXWebApp
将产生出下面的状态
2、添加Struts的系统包
2、在前面的WebProject中再添加一个order.jsp
(1)order.jsp
(2)编程设计该页面的内容
<%@pagelanguage="java"pageEncoding="gb2312"%>
<%@tagliburi="http:
//struts.apache.org/tags-bean"prefix="bean"%>
<%@tagliburi="http:
//struts.apache.org/tags-html"prefix="html"%>
<%@tagliburi="http:
//struts.apache.org/tags-logic"prefix="logic"%>
<%@tagliburi="http:
//struts.apache.org/tags-tiles"prefix="tiles"%>
DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">
htmllang="true">
base/>
AJAX技术的实例
varhttpRequest;
functioncheckFormData()
{
varsubscriptionID=document.getElementById("subscriptionID").value;
if((subscriptionID==null)||(subscriptionID==""))
{
alert("您没有输入SubscriptionID!
");
returnfalse;
}
varselect=document.getElementById("select").value;
if((select==null)||(select==""))
{
alert("您没有选择select!
");
returnfalse;
}
varselect=document.getElementById("select").value;
if((select==null)||(select==""))
{
alert("您没有选择select!
");
returnfalse;
}
returntrue;
}
--
-->
formaction="/processorder.do"method="post"onsubmit="returncheckFormData()">
输入您的SubscriptionID(yang):
textproperty="subscriptionID"onblur="validate(this.form)"/>
|
radioproperty="select"onblur="displayList('author')"value="author"/> 根据作者
| radioproperty="select"onblur="displayList('pubs')"value="pubs"/> 根据出版社
|
| SelectTitle:
|
submitproperty="order"value="Order"/>
|
resetproperty="cancel"value="Cancel"/>
|
form>
html>
3、由于在前面的order.jsp中使用了一个外部order.js
(1)右击站点的根目录
输入文件名称为order.js
(2)修改该文件在保存时的编码方式
由于Eclipse在,默认时是以UTF-8的编码方式保存*.js文件,因此在其中的中文将会被改变为乱码。
应该改变为gb2312编码方式。
改变为gb2312
(3)编程该order.js文件
varhttpRequest;
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("Content-Type","application/x-www-form-urlencoded");
}
functionvalidate(formObj)
{
init();
httpRequest.onreadystatechange=processHttpResponseSubscriptionValidator;
httpRequest.send("subscriptionID="+formObj.subscriptionID.value);
}
functiondisplayList(field)
{
init();
titles.innerHTML="";
httpRequest.onreadystatechange=processHttpResponseListHandler;
httpRequest.send("select="+escape(field));
}
functiondisplayTitles(formObj)
{
init();
varindex=formObj.list.selectedIndex;
varval=formObj.list.options[index].value;
httpRequest.onreadystatechange=processHttpResponseTitlesHandler;
httpRequest.send("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=="true")
{
document.getElementById("showErrorText").innerHTML="您输入的SubscriptionID是有效的!
";
document.LibraryForm.order.disabled=false;
}
else
{
document.getElementById("showErrorText").innerHTML="您输入的SubscriptionID是无效的!
";
document.LibraryForm.order.disabled=true;
}
}
elseif(httpRequest.status==404)
{
alert("没有找到与所请求的文件相匹配的资源!
");
}
else
{
alert("你所请求的页面发生异常,错误代码为:
"+httpRequest.status);
}
}
else
{
document.getElementById("showErrorText").innerHTML="检测中,请稍等...";
}
}
functionprocessHttpResponseTitlesHandler()
{
if(httpRequest.readystate==4)
{
if(httpRequest.status==200)
{
document.getElementById("showErrorText").innerHTML="";//除掉状态提示文字
vartitles=document.getElementById("titles");
varindexObj=httpRequest.responseXML.getElementsByTagName("index")[0];
varindex=indexObj.childNodes[0].nodeValue;
vartemp="";
for(vari=0;ivarlistObj=httpRequest.responseXML.getElementsByTagName("list")[i];
temp=temp+""+listObj.childNodes[0].nodeValue+"
}
temp=temp+"";
titles.innerHTML=temp;
}
elseif(httpRequest.status==404)
{
alert("没有找到与所请求的文件相匹配的资源!
");
}
else
{
alert("你所请求的页面发生异常,错误代码为:
"+httpRequest.status);
}
}
else
{
document.getElementById("showErrorText").innerHTML="检测中,请稍等...";
}
}
functionprocessHttpResponseListHandler()
{
varprefix;
if(httpRequest.readystate==4)
{
if(httpRequest.status==200)
{
document.getElementById("showErrorText").innerHTML="";//除掉状态提示文字
varlist=document.getElementById("list");
varauthorOption=document.getElementById("select")
if(authorOption.checked){
prefix="A";
}else{
prefix="P";
}
varlist=document.getElementById("selectionList");
varindexObj=httpRequest.responseXML.getElementsByTagName("index")[0];
varindex=indexObj.childNodes[0].nodeValue;
vartemp="";
for(vari=0;ivarlistObj=httpRequest.responseXML.getElementsByTagName("list")[i];
temp=temp+""+listObj.childNodes[0].nodeValue+"";
}
temp=temp+"";
list.innerHTML=temp;
}
elseif(httpRequest.status==404)
{
alert("没有找到与所请求的文件相匹配的资源!
");
}
else
{
alert("你所请求的页面发生异常,错误代码为:
"+httpRequest.status);
}
}
else
{
document.getElementById("showErrorText").innerHTML="检测中,请稍等...";
}
}
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("subscriptionID");
if(subscriptionID!
=null)
{
Stringstatus=""+this.checkValidID(subscriptionID)+"";
this.writeResponse(response,status);
}
//下面的代码是实现根据用户对“作者”或者“出版社”的类型选择来动态地获得对应的信息以放到第一下拉列表框中
option=request.getParameter("select");
key=request.getParameter("list");
if(option!
=null)
{
if(option.equals("author"))
{
list=this.populateAuthors();
xmlString=this.getXMLData(list);
this.writeResponse(response,xmlString);
}
elseif(option.equals("pubs"))
{
list=this.populatePubs();
xmlString=this.getXMLData(list);
this.writeResponse(response,xmlString);
}
}
//下面的代码是实现根据用户对第一下拉列表框中的选择的项目,对应地获得相应的信息并在第二个下拉列表框中显示输出
if(key!
=null)
{
list=this.getTitle(key);
xmlString=this.getXMLData(list);
this.writeResponse(response,xmlString);
}
returnnull;
}
publicvoidwriteResponse(HttpServletResponseresp,Stringoutput)
{
resp.setContentType("text/xml;charset=GBK");
resp.setHeader("Cache-Control","no-cache");
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
冰豆网所有资源均是用户自行上传分享,仅供网友学习交流,未经上传用户书面授权,请勿作他用。