flex中remoteObject和httpservice的取舍
remoteObject和java交互的时候需要在remoting-config.xml文件中配置,用httpservice和java交互的时候需要在struts文件中配置action或者在web.xml中配置servlet。httpservice在与action或者servlet交互的时候是可以获取request对象的,所以httpservice可以代替所有remoteObject实现的方法,而remoteObject却不能代替httpservice实现的方法。
下面来一个httpservice和java交互的例子。
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:ns1="iman.sps.component.*"
preloader="iman.sps.asfile.MyProgressBar"
creationComplete="bind()"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" width="100%" height="100%">
<fx:Style source="../cssfile/forms.css"/>
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
<s:Animate id="bounceEffect" target="{dg}" repeatCount="1" repeatBehavior="reverse" disableLayout="true" duration="1000">
<s:motionPaths>
<s:SimpleMotionPath property="y" valueFrom="{dg.y+10}" valueTo="{dg.y}">
</s:SimpleMotionPath>
<s:SimpleMotionPath property="alpha" valueFrom="0" valueTo="1">
</s:SimpleMotionPath>
</s:motionPaths>
</s:Animate>
<!--<s:RemoteObject id="allSheet" destination="getAllDoBaseSheet" result="resultHandler(event)"/>-->
<mx:HTTPService
id="allSheet"
method="POST"
url="../../sps/sheet/getAllDoBaseSheet.action"
result="resultHandler(event)"
fault="allSheet_faultHandler(event)"/>
</fx:Declarations>
<fx:Script>
<![CDATA[
import com.adobe.serialization.json.JSON;
import mx.binding.utils.BindingUtils;
import mx.binding.utils.ChangeWatcher;
import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.events.ListEvent;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
[Bindable]
public var ac:ArrayCollection = null;
[Bindable]
private var sheetid:String=null;
private var stuMarkDataArray:ArrayCollection;
[Bindable]
private var workItemId:String = null;
[Bindable]
private var spsCurrentState:String = null;
[Bindable]
private var processDefId:String=null;
[Bindable]
private var activityDefId:String=null;
public function get _processDefId():String{
return processDefId;
}
public function set _processDefId(str:String):void{
processDefId=str;
}
public function get _activityDefId():String{
return activityDefId;
}
public function set _activityDefId(str:String):void{
activityDefId=str;
}
public function get _sheetid():String{
return sheetid;
}
public function set _sheetid(str:String):void{
sheetid=str;
}
public function get _workItemId():String{
return workItemId;
}
public function set _workItemId(str:String):void{
workItemId=str;
}
public function get _spsCurrentState():String{
return spsCurrentState;
}
public function set _spsCurrentState(str:String):void{
spsCurrentState=str;
}
protected function show(event:Event):void
{
bounceEffect.end();
bounceEffect.play();
}
private function toUrl(event:Event):void{
trace("1111");
}
private function init():void{
dg.addEventListener(ListEvent.ITEM_CLICK,handleClickPlay);
}
public function bind():void{
allSheet.send();
init();
}
private function resultHandler(event:ResultEvent):void {
var rawData:String = event.result as String;
trace(rawData);
var rawArray:Array = JSON.decode(rawData) as Array;
stuMarkDataArray = new ArrayCollection(rawArray);
dg.dataProvider = stuMarkDataArray;
// ac = ArrayCollection(event.result);
//dg.dataProvider = ac;
}
private function handleClickPlay(event:ListEvent):void {
//此处得到选中行的对象 selectedItem 返回Object
var obj:Object = (event.currentTarget as DataGrid).selectedItem;
//拿具体值 可根据具体的 dataField 取得 下面是循环拿所有的方法 供参考
//var arr:Array =(event.currentTarget as DataGrid).columns
//for (var i:uint = 0; i < arr.length; i++) {
//var tempCol:DataGridColumn = (event.currentTarget as DataGrid).columns as DataGridColumn;
//trace("属性:"+tempCol.dataField+ " = " + obj[tempCol.dataField]);
//}
//Alert.show(obj.id)
sheetid=(obj.sheetId) as String;
trace("sheetid="+sheetid);
workItemId = (obj.workItemId) as String;
spsCurrentState = (obj.currentState) as String;
activityDefId = (obj.activityDefId) as String;
processDefId = (obj.processDefId) as String;
}
public function label1_clickHandler(event:MouseEvent):void
{
//调用URL
if(this._sheetid!=null){
var url:String="../../sps/sheet/undoSheetDetail.action?sheetId="+ this._sheetid+"&workItemId="+this._workItemId+"¤tState="+this._spsCurrentState;
url += "&processDefId="+this._processDefId+"&activityDefId="+this._activityDefId;
trace("外部参数="+sheetid);
trace("url="+url);
//Alert.show(url);
var javaeyeUrl:URLRequest = new URLRequest(url);
navigateToURL(javaeyeUrl,"_self");
var n:Object = event.currentTarget as Object;
}
}
public function label1_mouseOverHandler(event:MouseEvent):void
{
var la:Label = event.currentTarget as Label;
la.styleName= "cLable";
}
public function label1_mouseOutHandler(event:MouseEvent):void
{
var la:Label = event.currentTarget as Label;
la.styleName= "aLable";
}
protected function allSheet_faultHandler(event:FaultEvent):void
{
Alert.show("调用失败");
}
]]>
</fx:Script>
<mx:Panel title="待办工单" backgroundColor="#D4E1F2" width="100%" height="100%">
<s:VGroup width="100%" height="100%">
<s:HGroup horizontalAlign="right" width="100%">
<mx:Button label="查询"/>
<mx:Button label="清空"/>
</s:HGroup>
<s:HGroup width="100%">
<mx:Form width="80%">
<mx:Grid width="99%">
<mx:GridRow width="100%" >
<mx:GridItem width="40%" horizontalAlign="left" verticalAlign="middle" >
<mx:FormItem label="工单号" indicatorGap="25" width="100%">
<mx:TextInput width="60%" />
</mx:FormItem>
</mx:GridItem>
<mx:GridItem width="60%" horizontalAlign="left" verticalAlign="middle">
<mx:FormItem label="工单类型" indicatorGap="25" width="100%">
<mx:TextInput width="40%" />
</mx:FormItem>
</mx:GridItem>
</mx:GridRow>
<mx:GridRow width="100%" >
<mx:GridItem width="40%" horizontalAlign="left" verticalAlign="middle" >
<mx:FormItem label="主 题" indicatorGap="25" width="100%">
<mx:TextInput width="60%" />
</mx:FormItem>
</mx:GridItem>
<mx:GridItem width="60%" horizontalAlign="left" verticalAlign="middle">
<mx:FormItem label="派单时间" indicatorGap="25" width="100%" direction="horizontal" >
<mx:DateField width="48%" textAlign="center" formatString="YYYY-MM-DD"/>
<mx:Label text="至" width="4%"/>
<mx:DateField width="48%" textAlign="center" formatString="YYYY-MM-DD"/>
</mx:FormItem>
</mx:GridItem>
</mx:GridRow>
</mx:Grid>
</mx:Form>
</s:HGroup>
<s:HGroup width="100%" height="80%">
<mx:DataGrid id="dg" width="100%" doubleClickEnabled="true" doubleClick="label1_clickHandler(event)" creationComplete="show(event)" height="100%">
<mx:columns >
<mx:DataGridColumn id="sheetId" headerText="ID" dataField="id" visible="false">
<mx:itemRenderer>
<fx:Component>
<mx:Label toolTip="双击查看工单" doubleClickEnabled="true" doubleClick="outerDocument.label1_clickHandler(event)">
</mx:Label>
</fx:Component>
</mx:itemRenderer>
</mx:DataGridColumn>
<mx:DataGridColumn headerText="工单号" dataField="sheetId">
<mx:itemRenderer>
<fx:Component>
<mx:Label toolTip="双击查看工单" doubleClickEnabled="true" doubleClick="outerDocument.label1_clickHandler(event)">
</mx:Label>
</fx:Component>
</mx:itemRenderer>
</mx:DataGridColumn>
<mx:DataGridColumn headerText="主题" dataField="title">
<mx:itemRenderer>
<fx:Component>
<mx:Label toolTip="双击查看工单" doubleClickEnabled="true" doubleClick="outerDocument.label1_clickHandler(event)">
</mx:Label>
</fx:Component>
</mx:itemRenderer>
</mx:DataGridColumn>
<mx:DataGridColumn headerText="工单类型" dataField="type">
<mx:itemRenderer>
<fx:Component>
<mx:Label toolTip="双击查看工单" doubleClickEnabled="true" doubleClick="outerDocument.label1_clickHandler(event)">
</mx:Label>
</fx:Component>
</mx:itemRenderer>
</mx:DataGridColumn>
<mx:DataGridColumn id="sheetStep" headerText="当前环节" dataField="currentState">
<mx:itemRenderer>
<fx:Component>
<mx:Label toolTip="双击查看工单" doubleClickEnabled="true" doubleClick="outerDocument.label1_clickHandler(event)">
</mx:Label>
</fx:Component>
</mx:itemRenderer>
</mx:DataGridColumn>
<mx:DataGridColumn headerText="派单时间" dataField="createTime">
<mx:itemRenderer>
<fx:Component>
<mx:Label toolTip="双击查看工单" doubleClickEnabled="true" doubleClick="outerDocument.label1_clickHandler(event)">
</mx:Label>
</fx:Component>
</mx:itemRenderer>
</mx:DataGridColumn>
<mx:DataGridColumn headerText="要求完成时间" dataField="createTime">
<mx:itemRenderer>
<fx:Component>
<mx:Label toolTip="双击查看工单" doubleClickEnabled="true" doubleClick="outerDocument.label1_clickHandler(event)">
</mx:Label>
</fx:Component>
</mx:itemRenderer>
</mx:DataGridColumn>
<mx:DataGridColumn headerText="派单部门" dataField="createUser">
<mx:itemRenderer>
<fx:Component>
<mx:Label toolTip="双击查看工单" doubleClickEnabled="true" doubleClick="outerDocument.label1_clickHandler(event)">
</mx:Label>
</fx:Component>
</mx:itemRenderer>
</mx:DataGridColumn>
</mx:columns>
</mx:DataGrid>
</s:HGroup>
<!--<ns1:PagingBar width="100%" id="myPageBar" >
</ns1:PagingBar> -->
</s:VGroup>
</mx:Panel>
</s:Application>
struts配置
<action name="getAllDoBaseSheet" method="getAllDoBaseSheet">
java代码
该action实现了ServletRequestAware,ServletResponseAware接口
public void getAllDoBaseSheet() throws Exception{
String account = this.getSessionValue(SessionUtil.accountName);
String userName = this.getSessionValue(SessionUtil.userName);
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=utf-8;");
log.info("查询代办工单方法执行了");
try{
baseSheetDtolist = baseSheetService.getAllDoBaseSheet(account,userName);
}catch(Exception e){
e.printStackTrace();
}
StringBuffer josn = new StringBuffer();
josn.append("[");
if(baseSheetDtolist!=null && baseSheetDtolist.size()>0){
for(int i=0;i<baseSheetDtolist.size();i++){
BaseSheetDto bsd = (BaseSheetDto)baseSheetDtolist.get(i);
if(i!=baseSheetDtolist.size()-1){
josn.append("{");
josn.append(""id":""+ bsd.getId()+"",");
josn.append(""createTime":""+ DateTools.convertDateToString(bsd.getCreateTime(), "yyyy-MM-dd")+"",");
josn.append(""createUser":""+ bsd.getCreateUser()+"",");
josn.append(""sheetId":""+ bsd.getSheetId()+"",");
josn.append(""title":""+ bsd.getTitle()+"",");
josn.append(""step":""+ bsd.getStep()+"",");
josn.append(""workItemId":""+ bsd.getWorkItemId()+"",");
log.info(bsd.getProcessDefId());
josn.append(""processDefId":""+ bsd.getProcessDefId()+"",");
josn.append(""activityDefId":""+ bsd.getActivityDefId()+"",");
josn.append(""processInstId":""+ bsd.getProcessInstId()+"",");
josn.append(""currentState":""+ bsd.getCurrentState()+""},");
}else{
josn.append("{");
josn.append(""id":""+ bsd.getId()+"",");
josn.append(""createTime":""+ DateTools.convertDateToString(bsd.getCreateTime(), "yyyy-MM-dd")+"",");
josn.append(""createUser":""+ bsd.getCreateUser()+"",");
josn.append(""sheetId":""+ bsd.getSheetId()+"",");
josn.append(""title":""+ bsd.getTitle()+"",");
josn.append(""step":""+ bsd.getStep()+"",");
josn.append(""workItemId":""+ bsd.getWorkItemId()+"",");
josn.append(""processDefId":""+ bsd.getProcessDefId()+"",");
josn.append(""activityDefId":""+ bsd.getActivityDefId()+"",");
josn.append(""processInstId":""+ bsd.getProcessInstId()+"",");
josn.append(""currentState":""+ bsd.getCurrentState()+""}");
}
}
}
josn.append("]");
log.info(josn.toString());
PrintWriter out = response.getWriter();
out.print(josn.toString());
}
1 楼 caomeiliang 2011-11-28 如果仅仅是因为request的话
remote-object也能获取,所以你这个文章没什么参考价值