使用flex中的httpservice方法與java進行交互

這篇文章主要介紹了使用flex中的httpservice方法與java進行交互,需要的朋友可以參考下 地球已經調至震動狀態使用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 name='林沖' age='23' class='水泊梁山一班'/>");
out.write("<student name='李逵' age='24' class='水泊梁山二班'/>");
out.write("<student name='扈三娘' age='23' class='水泊梁山一班'/>");
out.write("<student name='時遷' age='24' class='水泊梁山二班'/>");
out.write("<student name='武松' age='23' class='水泊梁山一班'/>");
out.write("<student name='燕青' age='24' class='水泊梁山二班'/>");
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
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<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):http://localhost: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 id="myService"
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 id="gendertype" itemClick="filterHandler(event);"/>
<mx:RadioButton groupName="gendertype" id="rbMale" value="1" label="男" />
<mx:RadioButton groupName="gendertype" id="rbFemale" value="0" label="女" />
<mx:RadioButton groupName="gendertype" id="rbAll" value="2" label="所有" />
</mx:HBox>
<mx:DataGrid id="myDG" width="100%" 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 id="bt1" label="提交" click="bt1_clickHandler()"/>
</mx:HBox>
</mx:Application>

這樣就實現了flex到javaee 的交互工作
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章