天天看點

Flex與SSH內建

Flex與SSH內建

2010-06-26 11:00:36| 分類: flex |舉報|字号 訂閱

1,下載下傳blazeds_bin_3-0-0-544.zip 包,将其解壓 取下blazeds.war包 更改為blazeds.rar ,并解壓

2 将上一步解壓的web-inf/lib/下的包複制到工程的lib下

3,将flex檔案夾 複制到工程的web-inf下

4 将classes下的檔案複制到工程的src下

5在web.xml中加入

<!-- flex -->

<servlet>

<servlet-name>flex</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<init-param>

<param-name>contextConfigLocation</param-name>

<param-value>/WEB-INF/classes/flex-application-config.xml</param-value>

</init-param>

<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

<url-pattern>/ssh/*</url-pattern>

</servlet-mapping>

<!-- end flex -->

6,在src下建一個flex-application-config.xml檔案

加入

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:flex="http://www.springframework.org/schema/flex"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="

http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

http://www.springframework.org/schema/flex

http://www.springframework.org/schema/flex/spring-flex-1.0.xsd">

<flex:message-broker/>

<!— 下面是表示 在spring配置檔案中配置的bean-->

<flex:remoting-destination ref="flowServer"/>

</beans>

以下内容

6,加入flex與spring所依賴的包

Flex與SSH內建 - 花落誰家 - zhangcb666的部落格

7 編寫 java 類

1> 編寫類體DeptNode.class

package com.svse.entity.bean;

import java.util.ArrayList;

import java.util.List;

/***

* 注: 此類可以不用和Flex映射 ,因為在Flex端,我們不用把Object對象強轉為此對象

**/

public class DeptNode {

private int id;

private String name;

private DeptNode parent;

private List<DeptNode> children = null;

public DeptNode(){}

public DeptNode(int id ,String name){

this.id = id;

this.name = name;

}

/*************get and set 省略****************/

2> 編寫接口IflowServer.class

package com.svse.server;

import com.svse.entity.bean.DeptNode;

public interface IFlowServer {

//得到部門

List<DeptNode> getDeparts(int deptId);

3> 編寫實作類

package com.svse.server.impl;

import com.svse.server.IFlowServer;

public class FlowServer implements IFlowServer {

public List<DeptNode> getDeparts(int deptId){

List<DeptNode> list = new ArrayList<DeptNode>();

DeptNode node = new DeptNode();

node.setId(00);

node.setName("父節點11");

node.setChildren(this.getChild(3, "aa"));

list.add(node);

DeptNode node2= new DeptNode();

node2.setId(11);

node2.setName("父節點22");

node2.setChildren(this.getChild(5, "bb"));

list.add(node2);

return list;

private List<DeptNode> getChild(int count,String name){

for(int i = 0; i < count; i++){

DeptNode node3 = new DeptNode();

node3.setId(i);

node3.setName(name+""+i);

list.add(node3);

8 在spring中注入

<bean id="flowServer" class="com.svse.server.impl.FlowServer"/>

注:此處的Id,就是在flex-application-config.xml中的

<flex:remoting-destination ref="flowServer"/>配置的ref值

9 編寫flex代碼

<?xml version="1.0" encoding="utf-8"?>

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()">

<mx:Script>

<![CDATA[

import mx.rpc.remoting.mxml.RemoteObject;

import bean.FileAction;

import mx.collections.ArrayCollection;

import mx.rpc.events.FaultEvent;

import mx.rpc.events.ResultEvent;

import mx.controls.Alert;

/**

* 從伺服器請示資料

* */

function loadDept():void{

var remote:RemoteObject = new RemoteObject();

remote.destination="flowServer";

remote.endpoint="/ssh2-flex/messagebroker/amf";

remote.getDeparts(1);

remote.addEventListener(ResultEvent.RESULT,resultHander1);

remote.addEventListener(FaultEvent.FAULT,fault);

* 請示成功後,調用的方法

function resultHander1(event:ResultEvent):void{

//在此隻用轉化為集合對象,不用考慮集合對象中的對象

var list:ArrayCollection = event.result as ArrayCollection;

this.deptList.dataProvider = list;

* 初 始化

function init():void{

var xmlArray:ArrayCollection = new ArrayCollection([

{"name":"上海","selected":false,"children":[{"name":"黃浦","selected":false},

{"name":"浦東","selected":false}]},

{"name":"北京1","selected":false,"children":null}

]);

//Tree對象預設會取chilren為子節點

deptList.dataProvider = xmlArray;

* 出錯後調用的方法

function fault(event:FaultEvent):void{

Alert.show("error");

* 輕按兩下調用的方法

function showMsg():void{

var st:String = deptList.selectedItem.id +" "+deptList.selectedItem.name;

Alert.show(st);

]]>

</mx:Script>

<mx:Label />

<mx:HBox>

<mx:Button label="加載資料" click="loadDept()" fontSize="14" />

<mx:Tree id="deptList" labelField="name" width="200" height="300" itemDoubleClick="showMsg()" />

</mx:HBox>

</mx:Application>

注:Java類加也可傳回xml格的資料,供flex調用,具體請檢視相關文檔

<a></a>

本文轉自農夫山泉别墅部落格園部落格,原文連結:http://www.cnblogs.com/yaowen/p/4176759.html,如需轉載請自行聯系原作者