地球已經調至震動狀態(tài)使用flex中的httpservice方法與java進行交互:
一、寫服務器:
1.在myeclipse中建立web項目
2.寫一個用來打印xml的servlet
3.當使用httpservice與java進行交互的時候不用改寫remoting-config.xml文件
4.web.xml文件中配置servlet的訪問地址(一般不用配置,當一個servlet建好之后myeclipse就會自動配置好web.xml文 件,不用去改動,但是需要注意的是,當你在導入blazeds開發(fā)文件的時候他會改動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.發(fā)布servlet項目
比如講servlet項目發(fā)布到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 的交互工作
您可能感興趣的文章:- 使用Java開發(fā)實現OAuth安全認證的應用
- Java中HttpServletResponse響應中文出現亂碼問題
- java使用HttpSession實現QQ訪問記錄
- 使用httpclient無需證書調用https的示例(java調用https)
- 使用asx3m與xstream配合解決flex與java利用httpservice傳遞xml數據問題
- java struts2學習筆記之線程安全
- JSP學習之Java Web中的安全控制實例詳解
- Java線程安全中的單例模式
- java編譯時出現使用了未經檢查或不安全的操作解決方法
- 深入理解:Java是類型安全的語言,而C++是非類型安全的語言
- Java實現SSL雙向認證的方法