1. 在Flex页面有个请求,在<mx:Application>中定义 creationComplete="initApp();"
// 页面加载完成后初始化
internal function initApp():void {
// 随机查找30道试题,作为游戏练习使用
vocabulary.url="../../../../vocabularymgr/vocabularymgr!getVocabularyByRand.action?time="+new Date().time;
// 向服务器请求
vocabulary.send();
}
以HTTPService的方式发出请求:
<mx:HTTPService
id="vocabulary"
result="resultVocabularyHandler(event)"
fault="faultVocabularyHandler(event)"
method="POST"
resultFormat="text"
showBusyCursor="true">
</mx:HTTPService>
2. 在后台java代码中查询数据库,返回一个List对象,循环List,取出List中对象的属性值,以XML的方式传到Flex页面
HttpServletResponse response = ServletActionContext.getResponse();
PrintWriter out = null;
response.setContentType("text/html;charset=UTF-8");
out = response.getWriter();
StringBuffer respMsg = new StringBuffer("<list>");
// 随机查询30条的词汇
List<DcVocabularyPO> list = vocabularymgrService.getByRand(
30);
Iterator<DcVocabularyPO> it = list.iterator();
DcVocabularyPO po = null;
while (it.hasNext()) {
po = it.next();
respMsg.append("<Vocabulary>");
respMsg.append("<vocabularyName>")
.append(po.getVocabularyName())
.append("</vocabularyName>");
respMsg.append("<vocabularyType>")
.append(po.getVocabularyType())
.append("</vocabularyType>");
respMsg.append("<imageUrl>").append(po.getImageUrl())
.append("</imageUrl>");
respMsg.append("<word>").append(po.getWord())
.append("</word>");
respMsg.append("<prompt>").append(po.getPrompt())
.append("</prompt>");
respMsg.append("</Vocabulary>");
}
respMsg.append("</list>");
logger.info("get Vocabulary By Rand,the result is:\n"
+ respMsg);
// 返回页面
out.print(respMsg.toString());
if (null != out) {
out.close();
}
3. 在前端Flex页面接收后台传回的xml
<mx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.controls.Alert;
// 试题
var _list:String;
// 词汇名称
private var vocabularyName:String;
// 词汇类型
private var vocabularyType:String;
// 图片URL
private var imageUrl:String;
// 单词(答案)
private var word:String;
// 提示
private var _prompt:String;
//
var ss:ArrayCollection = new ArrayCollection();
//题目序号
var index2:int;
// "随机查找30道试题"服务器响应
private function resultVocabularyHandler(event:ResultEvent):void{
_list = event.result.toString();
var xml:XML = XML(_list);
for each(var nodeXML:XML in xml.Vocabulary){
ss.addItem(nodeXML);
}
// 取出第一条
vocabularyName = ss.getItemAt(0).vocabularyName;
vocabularyType = ss.getItemAt(0).vocabularyType;
imageUrl = ss.getItemAt(0).imageUrl;
word = ss.getItemAt(0).word;
_prompt = ss.getItemAt(0).prompt;
question_name.text = vocabularyName;
//给控件赋值
if(vocabularyType=="0"){
myimg.source=imageUrl;
question_name.text="";
}else if(vocabularyType=="1"){
myimg.source="";
question_name.text=vocabularyName;
}else{
myimg.source=imageUrl;
question_name.text=vocabularyName;
question_name.x=384;
question_name.width=144;
myimg.width=191;
}
prompt.text=_prompt;
// 启动定时器
tgame.start();
}
// "随机查找30道试题"服务器响应
private function faultVocabularyHandler(event:FaultEvent):void{
Alert.show("试题加载失败,请稍后再试!");
}
//答题(键盘事件)
protected function textinput1_keyDownHandler(event:KeyboardEvent):void
{
if(event.charCode==13){
checkData();
}
}
//检查是否答对
public function checkData():void{
if(userResult.text==""){
Alert.show("答案不能为空");
}else{
if(StringUtil.trim(userResult.text.toLocaleLowerCase())==StringUtil.trim(word.toLocaleLowerCase())){
var y:int=parseInt(myscore.text.toString());
myscore.text=(1+y)+"";
index2++;
preIsOk=true;
//答对后置空相关信息
word="";
question_name.text="";
myimg.source="";
prompt.text="";
userResult.text = "";
next();
//this.parentDocument.sendMessage("6:"+this.parentDocument.playerName+"-"+index2+"-"+(1+y));
}
}
}
// 下一题
public function next():void{
// 取出下一条
vocabularyName = ss.getItemAt(index2).vocabularyName;
vocabularyType = ss.getItemAt(index2).vocabularyType;
imageUrl = ss.getItemAt(index2).imageUrl;
word = ss.getItemAt(index2).word;
_prompt = ss.getItemAt(index2).prompt;
question_name.text = vocabularyName;
//给控件赋值
if(vocabularyType=="0"){
myimg.source=imageUrl;
question_name.text="";
}else if(vocabularyType=="1"){
myimg.source="";
question_name.text=vocabularyName;
}else{
myimg.source=imageUrl;
question_name.text=vocabularyName;
question_name.x=384;
question_name.width=144;
myimg.width=191;
}
prompt.text=_prompt;
}
]]>
</mx:Script>
<mx:Image x="193" y="179" width="335" height="175" id="myimg" />
<mx:TextArea x="193" y="214" width="335" height="103" id="question_name" editable="false" contentBackgroundAlpha="0.0" textAlign="center" fontSize="24" borderVisible="false" borderAlpha="0.0" borderColor="#FDFAFA" fontWeight="bold" contentBackgroundColor="#FFFDFD" color="#FCF9F9">
</mx:TextArea>
<mx:TextInput x="10" y="427" width="204" id="userResult" keyDown="textinput1_keyDownHandler(event)" editable="true" height="30" fontWeight="bold" fontSize="18"/>
Flex具体代码请查看附件 codes.rar 中的 practice.mxml