BlazeDS整合Spring实现增删改查

    BlazeDS整合Spring,adobe开发者中心有文章介绍:

http://www.adobe.com/devnet/flex/articles/spring_blazeds_integration.html

http://www.adobe.com/devnet/livecycle/articles/blazeds_spring_05.html

     前几天在IBM developerWorks看到一篇文章,自己更改了一下:

1.服务端编程

用myeclipse8.0下载flash build4插件。首先建一个flex project,使用WTP创建flex/java组合项目,点next,在LCDS war文件中,找到自己下载的blazeds-turnkey中的blazeds.war文件。

      完成之后在WEB-INF目录下flex目录中找到services-config.xml添加:

 <factories>
        <factory id="flexFactory" class="org.expressme.employee.mgmt.flex.FlexFactoryImpl"/>
    </factories>

 在remoting-config.xml中添加:

 <destination id="flexService">
        <properties>
            <factory>flexFactory</factory>
            <source>flexService</source>
            <scope>application</scope>
        </properties>
    </destination>

 注意这里的flexservice是Spring中的bean。不用spring需要写全路径。

FlexFactoryImpl.java实现如下:

public class FlexFactoryImpl implements FlexFactory {

    private Log log = LogFactory.getLog(getClass());

    public FactoryInstance createFactoryInstance(String id, ConfigMap properties) {
        log.info("Create FactoryInstance.");
        SpringFactoryInstance instance = new SpringFactoryInstance(this, id, properties);
        instance.setSource(properties.getPropertyAsString(SOURCE, instance.getId()));
        return instance;
    }

    public Object lookup(FactoryInstance instanceInfo) {
        log.info("Lookup service object.");
        return instanceInfo.lookup();
    }

    public void initialize(String id, ConfigMap configMap) {
    }
}
 

web.xml中添加spring支持就可以了,其他的不用更改。spring配置文件和以前的一样。具体代码在附件中。

 

2.客户端实现

EmployeeMgmt.mxml

<?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:mx="library://ns.adobe.com/flex/mx" 
			   minWidth="955" minHeight="600" creationComplete="queryAll();">
	<fx:Script>
		<![CDATA[ 
			import mx.collections.ArrayCollection;
			import mx.controls.Alert;
			import mx.rpc.events.ResultEvent;
			
			import org.expressme.employee.mgmt.Employee;
			
			[Bindable]
			private var employees : ArrayCollection = new  ArrayCollection();
			
			private function queryAll():void {
				ro.queryAll();
			}
			
			private function resultHandler(event:ResultEvent):void {
				employees = new ArrayCollection();
				for each (var o : Employee in event.result as Array) {
					employees.addItem(o);
				}
			} 
			
			private function queryByName():void {
				var name : String = nameText.text;
				if (name.length==0) {
					queryAll();
				}else{
					ro.queryByName(name);
				}
			}
			
			private function preCreateEmployee():void {
				form_name.text = "";
				form_title.selectedIndex = 0;
				form_gender_male.selected = false;
				form_gender_female.selected = false;
				form_birth.text = "";
				form_panel.visible = true;
			}
			
			private function deleteEmployee():void {
				var item : Employee = employeeDataGrid.selectedItem as Employee;
				if (item==null) {
					Alert.show("Please select an employee first!", "Error");
				}else {
					ro.deleteEmployee(item.id);
					employees.removeItemAt(employeeDataGrid.selectedIndex);
				}
				
			}
			
			private function createEmployee():void{
				ro.createEmployee(form_name.text,form_title.selectedItem,
					form_gender_male.selected,form_birth.selectedDate);
			}
		]]> </fx:Script>
	
	<fx:Declarations>
		<s:RemoteObject id="ro" destination="flexService" result="resultHandler(event)"/> 
	</fx:Declarations>
	
	
	
	<s:Label x="10" y="10" text="Employee Management" fontSize="24"/>
	<s:TextInput x="10" y="53" width="322" id="nameText"/>
	<s:Button x="340" y="53" label="Query" click="queryByName()"/>
	<s:Button x="409" y="53" label="Create" click="preCreateEmployee()"/>
	<s:Button x="481" y="53" label="Delete" click="deleteEmployee()"/>
	<mx:DataGrid x="10" y="83" width="535" height="186" dataProvider="{employees}" id="employeeDataGrid">
		<mx:columns>
			<mx:DataGridColumn headerText="ID" dataField="id"/>
			<mx:DataGridColumn headerText="Name" dataField="name"/>
			<mx:DataGridColumn headerText="Title" dataField="title"/>
			<mx:DataGridColumn headerText="Gender" dataField="gender"/>
			<mx:DataGridColumn headerText="Birth" dataField="birth"/>
		</mx:columns>
	</mx:DataGrid>
	<s:Panel x="128" y="160" width="298" height="191"  title="Create New Employee" id="form_panel" visible="false">
		<s:TextInput x="87" y="10" id="form_name"/>
		<s:Label x="37" y="12" text="Name:"/>
		<s:Label x="44" y="38" text="Title:"/>
		<s:Label x="28" y="64" text="Gender:"/>
		<s:Label x="43" y="90" text="Birth:"/>
		<s:ComboBox x="87" y="36" id="form_title">
			<s:dataProvider>
				<mx:ArrayList>
					<fx:String>CEO</fx:String>
					<fx:String>Project Manager</fx:String>
					<fx:String>Architect</fx:String>
					<fx:String>Engineer</fx:String>
					<fx:String>Designer</fx:String>
				</mx:ArrayList>
			</s:dataProvider>
		</s:ComboBox>
		<s:RadioButton x="86" y="62" label="Male" groupName="grp_gender" id="form_gender_male" value="true"/>
		<s:RadioButton x="145" y="62" label="Female" groupName="grp_gender" id="form_gender_female" value="false"/>
		<mx:DateField x="87" y="88" width="160" id="form_birth"/>
		<s:Button x="87" y="118" label="Create" id="form_button_create" click="createEmployee()"/>
		<s:Button x="159" y="118" label="Cancel" id="form_button_cancel" click="form_panel.visible=false"/>
	</s:Panel>
</s:Application>

 Employee.as

package org.expressme.employee.mgmt {

    [RemoteClass(alias="org.expressme.employee.mgmt.Employee")]
    [Bindable]
    public class Employee {

        public var birth : Date;
        public var id : String;
        public var name : String;
        public var title : String;
        public var gender : Boolean;
    }
}

 客户端实现有点小问题,就是每个ro事件处理都是resultHandler处理,导致增删操作后表格没数据显示。我也懒得改了,读者自己试试怎么实现……

 

 

 

 

 

 

你可能感兴趣的:(spring,编程,xml,Flex,Adobe)