地球已经调至震动状态利用flex中的httpservice要领与java举办交互:
一、写处事器:
1.在myeclipse中成立web项目
2.写一个用来打印xml的servlet
3.当利用httpservice与java举办交互的时候不消改写remoting-config.xml文件
4.web.xml文件中设置servlet的会见解点(一般不消设置,当一个servlet建好之后myeclipse就会自动设置好web.xml文 件,不消去窜改,可是需要留意的是,当你在导入blazeds开拓文件的时候他会窜改web.xml文件,这时候你必然要将web.xml文件悔改来,要 否则flex在会见处事器的时候则反问不到你的servlet)。
5.当你的处事器端的servlet写好之后再去成立flex 项目,而且按照你之前建好的servlet项目在flex项目标属性项中设置flex处事器的会见解点。
6.写flex的mxml文件。
二、一个简朴案例:
1.servlet源代码
复制代码 代码如下:
package com.wle.flex.HttpService.java文件源代码
package com.wle.flex;
import java.io.IOException;
import java.io.Writer;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HttpService01 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/xml;charset=utf-8");
Writer out = response.getWriter();
out.write("<?xml version='1.0' encoding='utf-8'?>");
out.write("<item>");
out.write("<student age='23'/>");
out.write("<student age='24'/>");
out.write("<student age='23'/>");
out.write("<student age='24'/>");
out.write("<student age='23'/>");
out.write("<student age='24'/>");
out.write("</item>");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
}
2.servlet对应的web.xml文件
复制代码 代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
">
<display-name></display-name>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>HttpService01</servlet-name>
<servlet-class>com.wle.flex.HttpService01</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HttpService01</servlet-name>
<url-pattern>/wl/HttpService01</url-pattern>
</servlet-mapping>
</web-app>
3.在webroot目次下导入blazeds文件
4.宣布servlet项目
好比讲servlet项目宣布到D:\Tomcat 6.0\webapps\flex_service_HttpService这个文件下
5.成立flex项目
建好flex项目之后,在项目上右键单击选择属性,在属性中的flex处事器选项中配置flex处事器的地点
设置信息如下:
文件根目次:D:\Tomcat 6.0\webapps\flex_service_HttpService
根URL(U)::8089/flex_service_HttpService
上下文根目次:/flex_service_HttpService
6.设置好flex项目标处事器之后就开始写flex源文件,如下:
复制代码 代码如下:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="vertical"
fontSize="12" viewSourceURL="srcview/index.html" xmlns:s="library://ns.adobe.com/flex/spark">
<!--<mx:Style source="DGtoExcel.css"/>-->
<mx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.events.FlexEvent;
import mx.events.ItemClickEvent;
import mx.rpc.events.FaultEvent;
[Bindable]
private var myAC:ArrayCollection;
private function faultHandler(event:FaultEvent):void
{
Alert.show(event.fault.faultString, event.fault.message);
}
// Function to filter out all items with gender
private function maleFilterFunc(item:Object):Boolean {
return item.gender == 1;
}
// Function to apply the filter function the ICollectionView.
private function filterMale():void {
myAC.filterFunction = maleFilterFunc;
//Refresh the collection view to apply the filter.
myAC.refresh();
}
// Function to filter out all items with gender
private function femaleFilterFunc(item:Object):Boolean {
return item.gender == 0;
}
// Function to apply the filter function the ICollectionView.
private function filterFemale():void {
myAC.filterFunction = femaleFilterFunc;
//Refresh the collection view to apply the filter.
myAC.refresh();
}
// Function to Reset the view to its original state.
private function resetAC():void {
myAC.filterFunction = null;
//Refresh the collection view.
myAC.refresh();
}
// Event handler function to display the selected button
private function filterHandler(event:ItemClickEvent):void {
switch(event.currentTarget.selectedValue){
case 1:
filterMale();
break;
case 0:
filterFemale();
break;
case 2:
resetAC();
break;
default:
break;
}
}
protected function bt1_clickHandler():void
{
myService.send();
}
]]>
</mx:Script>
<mx:HTTPService
showBusyCursor="true"
url="http://localhost:8089/flex_service_HttpService/wl/HttpService01"
result="myAC = event.result.item.student"
fault="faultHandler(event)"
contentType="application/xml"
/>
<mx:HBox>
<mx:RadioButtonGroup itemClick="filterHandler(event);"/>
<mx:RadioButton groupName="gendertype" value="1" label="男" />
<mx:RadioButton groupName="gendertype" value="0" label="女" />
<mx:RadioButton groupName="gendertype" value="2" label="所有" />
</mx:HBox>
<mx:DataGrid rowCount="20"
dataProvider="{myAC}" >
<mx:columns>
<mx:DataGridColumn headerText="姓名" dataField="name"/>
<mx:DataGridColumn headerText="年数" dataField="age" />
<mx:DataGridColumn headerText="班级" dataField="class"/>
</mx:columns>
</mx:DataGrid>
<mx:HBox>
<s:Button label="提交"/>
</mx:HBox>
</mx:Application>
这样就实现了flex到javaee 的交互事情
您大概感乐趣的文章: