前面两篇分析已经将FLEX部分的内容都介绍完毕,其中有个HttpService控件对象是用来和服务器端做交互的,那么服务器端到底该怎么处理呢?简单的理解就是FLEX发送一个带参数的请求到服务器的某个页面文件,然后该页面获取参数值,并根据参数值来进行相应的操作,操作完成后将自定义的操作结果返回给FLEX,最后FLEX根据获取到的操作结果进行相关操作。其实这个过程和我们平时浏览网页过程一样,比方说登陆:在文本框中输入账号密码,点击登陆按钮,提交账号密码到指定页面,指定页面获取账号密码后做相应操作(判断是否正确),然后返回结果给我们,我们就可以看到登陆页上显示登陆成功获得登陆失败了。
和FLEX做交互的服务器端技术有很多:ASP、ASP.NET、JSP、PHP等都可以,我比较熟悉ASP.NET,但是很不幸的同学们应该对JSP熟悉点,而我对JSP又很不熟,所以只能靠自己摸索了。不过总的思路上是大体一致的,无非就是实现技术上有所差异而已,理论正确,那理论所指导的实践总是能得到结果的。下面把ASP.NET的代码贴下:
private XmlDocument xmlDoc = null;//声明一个XML文档对象,下面用来加载XML文件
protected void Page_Load(object sender, EventArgs e)//pageLoad事件,表示页面加载
{
if (Request.Form["mode"] != null)//获取FLEX端传递过来的MODE参数,如果不为空的话才做下面的操作
{
string mode = Request.Form["mode"];//MODE不为空,那么就获取MODE的值
string xmlStr = "";
string goodID="",goodName="",goodIMG="",goodPrice="";
int goodNum=1;//这三行声明了些下面代码中会用到的变量
switch (mode)//switch...case...模式来匹配mode的值
{
case "readGoods"://如果是readGoods表示读取商品列表数据
xmlStr=readXML(Server.MapPath("dataFiles/goodsList.xml"));//自定义函数readXML,参数一个,就是要读取的XML文件路径,返回值是该文件的具体内容,赋给xmlStr自定义变量。
Response.Write(xmlStr);//输出xmlStr变量(就是将这个变量发送给FLEX)
break;
case "writeGoods"://如果是writeGoods表示将商品数据写入到购物车数据文件中
goodID = Request.Form["goodID"];
goodName = Request.Form["goodName"];
goodIMG = Request.Form["goodIMG"];
goodPrice = Request.Form["goodPrice"];
goodNum = 1;//这五行表示获取和商品有关的数据
string resultStr=writeCartXML(Server.MapPath("dataFiles/cartInfo.xml"),goodID, goodName, goodIMG, goodPrice, goodNum);//自定义函数writeCartXML,参数六个,第一个表示要写入的XML文件路径,后面五个表示要写入的数据,返回值是自定义的一个文本writeOK,赋给xmlStr
Response.Write(resultStr);//同上
break;
case "readCartInfo"://如果是readCartInfo表示读取购物车数据,类似readGoods,所以调用同一个readXML函数。
xmlStr=readXML(Server.MapPath("dataFiles/cartInfo.xml"));
Response.Write(xmlStr);
break;
case "delCartInfo"://如果是delCartInfo表示删除购物车中某条商品数据
goodID=Request.Form["goodID"];//获取传递过来的要删除的商品的编号
xmlStr=delCartXML(Server.MapPath("dataFiles/cartInfo.xml"),goodID);//调用自定义函数delCartXML,参数两个,要操作的XML文件路径,要删除的商品的编号值。返回值是delOK,赋给xmlStr
Response.Write(xmlStr);//同上
break;
case "editCartInfo"://前面分析了这么多,这里就略了……
goodID = Request.Form["goodID"];
goodNum = Convert.ToInt32(Request.Form["goodNum"]);
xmlStr = editCartXML(Server.MapPath("dataFiles/cartInfo.xml"), goodID,goodNum);
Response.Write(xmlStr);
break;
}
}
}
//下面就是上面要调用的几个自定义函数
//readXML函数,读取指定文件路径的XML文件的内容,将文件内容作为返回值
protected string readXML(string filePath)
{
xmlDoc = new XmlDocument();
xmlDoc.Load(filePath);//加载指定的XML文件
return xmlDoc.OuterXml;
}
//writeCartXML函数,往指定的购物车XML文件中写商品数据,要注意的是如果已有同类商品,则不添加而是修改原有的数量和价格,如果没有则添加,返回writeOK
protected string writeCartXML(string filePath, string goodID, string goodName, string goodIMG, string goodPrice, int goodNum)
{
xmlDoc = new XmlDocument();
xmlDoc.Load(filePath);
XmlNode rootNode = xmlDoc.SelectSingleNode("//cartInfo");//获取XML文件中的根节点
XmlNode theNode = rootNode.SelectSingleNode("//cartItem[theID=" + goodID + "]");//在根节点的子节点中找名为cartItem,且里面包含值为商品编号的theID节点的子节点。
if (theNode==null)//如果没找到,表示没有该商品,那么添加
{
XmlNode goodNode = xmlDoc.CreateNode(XmlNodeType.Element, "cartItem", "");
XmlNode goodIDNode = xmlDoc.CreateNode(XmlNodeType.Element, "theID", "");
goodIDNode.InnerText = goodID;
XmlNode goodNameNode = xmlDoc.CreateNode(XmlNodeType.Element, "theName", "");
goodNameNode.InnerText = goodName;
XmlNode goodIMGNode = xmlDoc.CreateNode(XmlNodeType.Element, "theIMG", "");
goodIMGNode.InnerText = goodIMG;
XmlNode totalPriceNode = xmlDoc.CreateNode(XmlNodeType.Element, "totalPrice", "");
totalPriceNode.InnerText = goodPrice.ToString();
XmlNode totalNumNode = xmlDoc.CreateNode(XmlNodeType.Element, "totalNum", "");
totalNumNode.InnerText = goodNum.ToString();
goodNode.AppendChild(goodIDNode);
goodNode.AppendChild(goodNameNode);
goodNode.AppendChild(goodIMGNode);
goodNode.AppendChild(totalPriceNode);
goodNode.AppendChild(totalNumNode);
rootNode.AppendChild(goodNode);
}
else//如果找到了,那么修改已有的商品总价和数量
{
theNode.ChildNodes.Item(3).InnerText = (Convert.ToDouble(theNode.ChildNodes.Item(3).InnerText) + Convert.ToDouble(goodPrice)).ToString();//theNode表示找到的cartItem这个商品节点,ChildNodes表示它的子节点集合,Item(n)表示第n个子节点,总价是第四个,数量是第五个,所以n分别是3和4。
theNode.ChildNodes.Item(4).InnerText = (Convert.ToInt32(theNode.ChildNodes.Item(4).InnerText) + Convert.ToInt32(goodNum)).ToString();
}
xmlDoc.Save(filePath); //改完之后要save回XML文件,这样才能保存最新数据。
return "writeOK";
}
//delCartXML函数,用来删除指定编号的商品,返回delOK
protected string delCartXML(string filePath,string goodID)
{
xmlDoc = new XmlDocument();
xmlDoc.Load(filePath);
XmlNode rootNode = xmlDoc.SelectSingleNode("//cartInfo");
XmlNode theNode = rootNode.SelectSingleNode("//cartItem[theID=" + goodID + "]");
if (theNode != null)
{
rootNode.RemoveChild(theNode);//找到商品子节点就remove删除
}
xmlDoc.Save(filePath);//存回去
return "delOK";
}
//editCartXML函数,用来修改已有商品的数量及总价,返回editOK
protected string editCartXML(string filePath, string goodID, int goodNum)
{
xmlDoc = new XmlDocument();
xmlDoc.Load(filePath);
XmlNode rootNode = xmlDoc.SelectSingleNode("//cartInfo");
XmlNode theNode = rootNode.SelectSingleNode("//cartItem[theID=" + goodID + "]");
if (theNode != null)
{
double goodPriceOld = Convert.ToDouble(theNode.ChildNodes.Item(3).InnerText);
double goodNumOld = Convert.ToDouble(theNode.ChildNodes.Item(4).InnerText);
double thePrice = goodPriceOld / goodNumOld;//这三行先算出该商品的单价
theNode.ChildNodes.Item(3).InnerText = (thePrice*(double)goodNum).ToString();
theNode.ChildNodes.Item(4).InnerText = goodNum.ToString();//这两行修改该商品的新总价和新数量
}
xmlDoc.Save(filePath);
return "editOK";
}
从这些代码里我们可以了解到我们还需要两个XML文件,而且这两个XML文件是放在和当前这个网页文件同级的dataFiles文件夹中的,文件名分别是goodsList.xml和cartInfo.xml。文件内容示例如下:
<?xml version="1.0" encoding="utf-8"?>
<goodsList>
<goodItem>
<theID>1</theID>
<theName>网页设计</theName>
<theIMG>webDesign.jpg</theIMG>
<thePrice>54.00</thePrice>
<theDescription>网页设计入门级教材</theDescription>
</goodItem>
<goodItem>
<theID>2</theID>
<theName>数据结构</theName>
<theIMG>dataStruct.jpg</theIMG>
<thePrice>30.00</thePrice>
<theDescription>数据结构入门级教材</theDescription>
</goodItem>
<goodItem>
<theID>3</theID>
<theName>平面设计</theName>
<theIMG>pageDesign.jpg</theIMG>
<thePrice>75.00</thePrice>
<theDescription>平面设计入门级教材</theDescription>
</goodItem>
<goodItem>
<theID>4</theID>
<theName>java编程</theName>
<theIMG>javaProgramming.jpg</theIMG>
<thePrice>88.00</thePrice>
<theDescription>JAVA编程入门级教材</theDescription>
</goodItem>
</goodsList>
<?xml version="1.0" encoding="utf-8"?>
<cartInfo>
<cartItem>
<theID>1</theID>
<theName>网页设计</theName>
<theIMG>webDesign.jpg</theIMG>
<totalPrice>54.00</totalPrice>
<totalNum>1</totalNum>
</cartItem>
</cartInfo>
好了,像服务器的环境配置啊、localhost是什么啊这种就不提了,否则就是重开动态网站这门课了。祝好运!
本文详细解析了使用ASP.NET技术在服务器端处理与FLEX的交互流程,包括请求处理、商品数据读取与写入、购物车操作等关键步骤,并提供了具体的代码实现。

4789

被折叠的 条评论
为什么被折叠?



