地球已经调至震动状态使用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 的交互工作

点赞(0)

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部