客户端flex利用blazeds调用后台AO实现编辑数据
-->
测试
环境:
Eclipse Java EE
平台、
flex builder
、
Tomcat6.x
、
Adobe BlazeDS\
Arcserver for java platform
-->
创建名为
“FlexToRemote”
的
Web
项目(基于BlazeDS,具体步骤掠过参加另一篇日志);
-->
导入
ArcObject.jar
(路径:
%ARCGIS_HOME%java/lib
)
;
服务器
后台用AO
添加point
的代码
两个java类(PointDS.java和AOEditDS.java)如下:
package
wlh;
import
com.esri.arcgis.server.ServerConnection;
import
com.esri.arcgis.server.*;
import
com.esri.arcgis.system.ServerInitializer;
import
com.esri.arcgis.carto.*;
import
com.esri.arcgis.geodatabase.*;
import
com.esri.arcgis.geometry.*;
import
com.esri.arcgis.system.*;
public
class
AOEditDS {
private
String
password
=
"1314525"
;
private
String
user
=
"Administrator"
;
private
String
domain
=
"pcwlhgis"
;
private
String
host
=
"pcwlhgis"
;
private
String
servicesName
=
"XIAMEN"
;
private
static
ServerConnection
conn
=
null
;
private
static
IServerContext
serverContext
=
null
;
private
static
IServerObjectManager
som
=
null
;
private
static
MapServer
mapserver
=
null
;
private
static
IMap
maps
=
null
;
public
void
initServer(){
/**
*
连接到
ArcGIS
Sever
*/
try
{
new
ServerInitializer().initializeServer(
domain
,
user
,
password
);
conn
=
new
ServerConnection();
conn
.connect(
host
);
som
=
conn
.getServerObjectManager();
serverContext
=
som
.createServerContext(
servicesName
,
"MapServer"
);
mapserver
=(MapServer)
serverContext
.getServerObject();
maps
=
mapserver
.getMap(
mapserver
.getDefaultMapName());
System.
out
.print(
"
已连接到
ArcGIS Sever"
);
}
catch
(Exception e){
e.printStackTrace();
mapserver
=
null
;
maps
=
null
;
som
=
null
; } }
public
boolean
AddPoint(PointDS pointds){
/*
执行添加点的操作
,
传入的参数对象
PointDS * */
//
先判断
Server
的连接状态
if
(
maps
==
null
||
mapserver
==
null
||
som
==
null
||
serverContext
==
null
||
conn
==
null
)
this
.initServer();
if
(
maps
==
null
||
mapserver
==
null
||
som
==
null
||
serverContext
==
null
||
conn
==
null
)
{
System.
out
.print(
"
不能连接到
ArcGIS Server!"
);
return
false
;
}
try
{
IFeatureLayer pFeatLayer=(IFeatureLayer)
serverContext
.createObject(FeatureLayer.getClsid());
pFeatLayer=(IFeatureLayer)
maps
.getLayer(pointds.getLayerIndex());
IFeatureClass pfeatureclass=pFeatLayer.getFeatureClass();
IPoint point=(IPoint)
serverContext
.createObject(Point.getClsid());
point.setX(pointds.getX());
point.setY(pointds.getY());
point.setSpatialReferenceByRef(
maps
.getSpatialReference());
IDataset dataset=
new
IDatasetProxy(pfeatureclass);
IWorkspaceEdit pWorkspaceEdit=
new
IWorkspaceEditProxy(dataset.getWorkspace());
pWorkspaceEdit.startEditing(
true
);
pWorkspaceEdit.startEditOperation();
IFeatureBuffer FeatureBuffer = pfeatureclass.createFeatureBuffer();
IFeatureCursor featureCursor =
new
FeatureCursor(pfeatureclass.IFeatureClass_insert(
true
));
FeatureBuffer.setShapeByRef((IGeometry) point);
java.util.Map hashmap=pointds.getHashmap();
if
(hashmap !=
null
){
for
(Object o : hashmap.keySet()){
String field = String.valueOf(o);
FeatureBuffer.setValue(FeatureBuffer.getFields().findField(field), hashmap.get(o));
}
}
featureCursor.insertFeature(FeatureBuffer);
featureCursor.flush();
pWorkspaceEdit.stopEditOperation();
pWorkspaceEdit.stopEditing(
true
);
System.
out
.print(
"\n
点已添加!
"
);
Cleaner.release(point);
// release Point of AO
mapserver
.refreshServerObjects();
serverContext
.releaseContext();
}
catch
(Exception e) {
e.printStackTrace();
mapserver
=
null
;
maps
=
null
;
som
=
null
;
return
false
; }
return
true
; } }
package
wlh;
public
class
PointDS {
public
double
x
;
public
double
y
;
public
int
layerIndex
;
public
java.util.Map
hashmap
;
public
java.util.Map getHashmap() {
return
hashmap
;
}
public
void
setHashmap(java.util.Map hashmap) {
this
.
hashmap
= hashmap;
}
public
double
getX() {
return
x
;
}
public
void
setX(
double
x) {
this
.
x
= x;
}
public
double
getY() {
return
y
;
}
public
void
setY(
double
y) {
this
.
y
= y;
}
public
int
getLayerIndex() {
return
layerIndex
;
}
public
void
setLayerIndex(
int
layerIndex) {
this
.
layerIndex
= layerIndex;
}
}
配置remoting-config.xml
客户端Flex
部分就两个文件,首先是和Java
相对应的“PointDS.as
”:然后是界面主文件mxml
package
wlh
{
import
mx.controls.List;
[
Bindable
]
[RemoteClass(alias=
"wlh.PointDS"
)]
public
class
PointDS
{
public
function
PointDS()
{ }
public
var
x:Number;
public
var
y:Number;
public
var
layerIndex:int;
public
var
hashmap:Object;
}
}
利用[RemoteClass
(alias=""
)]
元数据映射Java
端的FlexGroup
对象,注意这里的成员名一定要一致。
主界面FlexToRemote.mxml
<?xml version="1.0" encoding="utf-8"?>
<mx:Application
xmlns:mx="
http://www.adobe.com/2006/mxml
" xmlns:esri="
http://www.esri.com/2008/ags
"
pageTitle="
RPCcallAO
" creationComplete="init()"
>
<mx:Script>
<![CDATA[
import
mx.rpc.events.FaultEvent;
import
mx.rpc.Fault;
import
mx.rpc.events.ResultEvent;
import
wlh.PointDS;
import
com.esri.ags.events.MapMouseEvent;
import
mx.controls.Alert;
import
mx.messaging.ChannelSet;
import
mx.messaging.channels.*;
private
var
myPointDS:PointDS=
new
PointDS();
public
function
init():
void
{
/*
添加上去,以防万一
*/
Security.loadPolicyFile(
"http://pcwlhgis:8081/crossdomain.xml"
);
Security.loadPolicyFile(
"http://pcwlhgis:8399/crossdomain.xml"
);
}
private
function
onMapClick(event:MapMouseEvent):
void
{
myMap.infoWindow.label =
"You clicked here"
;
myMap.infoWindow.show(event.mapPoint);
myPointDS.x=event.mapPoint.x; myPointDS.y=event.mapPoint.y; myPointDS.layerIndex=0;
var
cs:ChannelSet =
new
ChannelSet();
cs.addChannel(
new
AMFChannel(
"myAMF"
,
"http://localhost:8081/FlexToRemote/messagebroker/amf"
));
//ro.channelSet = cs;//ok
AOEditDSInJava.endpoint=
"http://localhost:8081/FlexToRemote/messagebroker/amf"
;
//ok
//
以上
2
个只要指定一个就可以
AOEditDSInJava.AddPoint(myPointDS);
//
调用
java
类的
addpoint
方法
}
private
function
onResult(evt:ResultEvent):
void
{
Alert.show(
"Success to add point to mapservice! good"
);
}
private
function
onFault(evt:FaultEvent):
void
{
Alert.show(
"Sorry ,failed to add point to mapservice"
);
Alert.show(evt.fault.message.toString());
}
]]>
</mx:Script>
<esri:Map
id="
myMap
" mapClick="onMapClick(event)"
>
<esri:ArcGISDynamicMapServiceLayer
url="
http://pcwlhgis:8399/arcgis/rest/services/XIAMEN/MapServer
"
/>
</esri:Map>
<mx:RemoteObject
id="
AOEditDSInJava
" destination="
AOEditDS
" result="onResult(event)" fault="onFault(event)"
/>
<mx:TraceTarget
includeCategory="
true
" includeLevel="
true
"
/>
<!-- The output from TraceTarget is available when running Debug within Flex Builder -->
</mx:Application>
运行出现
SecurityError:Error#2048:
安全沙箱冲
突
:http://localhost:8081/FlexToRemote/FlexToRemote-debug/FlexToRemote.swf
不能从
http://pcwlhgis:8399/arcgis/rest/services/XIAMEN/MapServer?f=json
加载数据。
解决
运行成功