如何使用flex调用java函数

时间:2013-12-22 06:18:38

标签: java spring flex flex4.5 blazeds

我使用spring创建了java web项目,并使用adobe flex设计了前端。当我运行flex项目时有一个例外,我无法解决它。

这是我的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="
        http://java.sun.com/xml/ns/javaee
        http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">

    <servlet>
        <servlet-name>blazeds</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>blazeds</servlet-name>
        <url-pattern>/messageBroker/*</url-pattern>
    </servlet-mapping>

    <servlet>
        <servlet-name>RDSDispatchServlet</servlet-name>
        <servlet-class>flex.rds.server.servlet.FrontEndServlet</servlet-class>
        <init-param>
            <param-name>useAppserverSecurity</param-name>
            <param-value>false</param-value>
        </init-param>
        <init-param>
            <param-name>messageBrokerId</param-name>
            <param-value>_messageBroker</param-value>
        </init-param>
        <load-on-startup>10</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>RDSDispatchServlet</servlet-name>
        <url-pattern>/CFIDE/main/ide.cfm</url-pattern>
    </servlet-mapping>

</web-app>

的BlazeDS-selvlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:flex="http://www.springframework.org/schema/flex" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    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-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/flex
        http://www.springframework.org/schema/flex/spring-flex-1.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

        <context:annotation-config />
        <context:component-scan base-package="org.com.rss" />
        <mvc:annotation-driven/>

        <flex:message-broker/>

</beans>

服务-config.xml中

<?xml version="1.0" encoding="UTF-8"?>
<services-config>
    <channels>
        <channel-definition id="my-amf" class="mx.messaging.channels.AMFChannel">
            <endpoint url="http://{server.name}:{server.port}/{context.root}/messageBroker/amf"
                        class="flex.messaging.endpoints.AMFEndpoint"/>
            <properties>
                <polling-enabled>true</polling-enabled>
            </properties>
        </channel-definition>
    </channels>
</services-config>

这是我的java类

package org.com.rss;

import java.util.ArrayList;
import java.util.List;

import org.springframework.flex.remoting.RemotingDestination;
import org.springframework.flex.remoting.RemotingInclude;
import org.springframework.stereotype.Service;

@Service("mapManagerService")
@RemotingDestination(channels={"my-amf"})
public class MapManager {

    @RemotingInclude
    public List<String> getCountryList(){

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

        list.add("USA");
        list.add("China");
        list.add("Russia");
        list.add("France");
        list.add("Germany");
        list.add("Japan");
        list.add("India");
        list.add("UK");

        return list;
    }
}

之后我创建了以下mxml文件来测试我的flex项目。

<?xml version="1.0" encoding="utf-8"?>
<s:Group xmlns:fx="http://ns.adobe.com/mxml/2009"
         xmlns:s="library://ns.adobe.com/flex/spark"
         xmlns:mx="library://ns.adobe.com/flex/mx"
         xmlns:services="services.*"
         width="400" height="300">

    <fx:Script>
        <![CDATA[
            import mx.collections.ArrayList;
            import mx.controls.Alert;

            protected function btnFillCombo_clickHandler(event:MouseEvent):void
            {
                getCountryListResult.token = mapManagerService.getCountryList();
            }

        ]]>
    </fx:Script>

    <fx:Declarations>
        <s:CallResponder id="getCountryListResult"/>
        <services:MapManagerService id="mapManagerService"
                                    fault="Alert.show(event.fault.faultString + '\n' + event.fault.faultDetail)"
                                    showBusyCursor="true"/>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>

    <s:Button id="btnFillCombo" x="189" y="36" label="Button" click="btnFillCombo_clickHandler(event)"/>
    <s:ComboBox id="cmbCountry" x="26" y="34"/>
</s:Group>

首先我运行我的java项目,然后运行我的flex项目。当我点击按钮时出现以下错误。

[MessagingError message='Destination 'mapManagerService' either does not exist or the destination has no channels defined (and the application does not define any default channels.)']
Couldn't establish a connection to 'mapManagerService'

但我通过数据服务调用该函数正确返回国家/地区列表。但我无法通过我的代码调用该函数。请帮我解决这个问题。

0 个答案:

没有答案