首先撰写mxml文件:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml" verticalGap="20">
<mx:HTTPService id="employeeSrv" method="get" url="employee.jsp" resultFormat="xml" result="getMessage()">
<mx:request>
<name>{name.text}</name>
<email>{email.text}</email>
<phone>{phone.text}</phone>
<zipcode>{zipcode.text}</zipcode>
</mx:request>
</mx:HTTPService>
<mx:Form>
<mx:FormHeading label="增加员工"></mx:FormHeading>
<mx:FormItem label="Name">
<mx:TextInput id="name" width="200" />
</mx:FormItem>
<mx:FormItem label="Email">
<mx:TextInput id="email" width="200" />
</mx:FormItem>
<mx:FormItem label="Phone">
<mx:TextInput id="phone" width="200" />
</mx:FormItem>
<mx:FormItem label="ZipCode">
<mx:TextInput id="zipcode" width="60" />
</mx:FormItem>
<mx:HBox width="100%" textAlign="center">
<mx:Button label="Submit" click="employeeSrv.send();" />
<mx:Button label="Reset" click="clearText();" />
</mx:HBox>
</mx:Form>
<mx:Script>
function getMessage()
{
var xmlNodes:Array = employeeSrv.result.childNodes;
mx.controls.Alert.show( (xmlNodes[0].childNodes)[0].toString() , 'Message');
clearText();
}
function clearText()
{
name.text = "";
email.text = "";
phone.text = "";
zipcode.text = "";
}
</mx:Script>
</mx:Application>
然后是JSP文件:
<%@ page contentType="text/xml; charset=utf-8" %>
<%
request.setCharacterEncoding( "utf-8" );
System.out.println( request.getParameter( "name" ) );
System.out.println( request.getParameter( "phone" ) );
System.out.println( request.getParameter( "email" ) );
System.out.println( request.getParameter( "zipcode" ) );
%>
<root>
<msg>Success!</msg>
</root>
注意红字的部分,在那里声明了一个HTTPService,当你点击Submit按钮的时候HTTPService就会把你填写的数据提交到后台的JSP页面,在JSP页面里就可以使用request.getParameter方法取出你填写的内容,JSP最后返回一个XML文档被mxml页面的getMessage()函数所处理,并在屏幕上显示出Success!
特别提示:HTTPService中的method属性在处理中文的时候请使用GET方式,POST方式会出乱码,这个问题很多朋友都遇到过,请注意。