BlazDS与JAVA交互实例

这里只列出代码:

1.依赖包

BlazDS与JAVA交互实例_第1张图片

2.WEB-INF/web.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>

    <display-name>BlazeDS</display-name>
    <description>BlazeDS Application</description>

    <!-- Http Flex Session attribute and binding listener support -->
    <listener>
        <listener-class>flex.messaging.HttpFlexSession</listener-class>
    </listener>

    <!-- MessageBroker Servlet -->
    <servlet>
        <servlet-name>MessageBrokerServlet</servlet-name>
        <display-name>MessageBrokerServlet</display-name>
        <servlet-class>flex.messaging.MessageBrokerServlet</servlet-class>
        <init-param>
            <param-name>services.configuration.file</param-name>
            <param-value>/WEB-INF/flex/services-config.xml</param-value>
       </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>MessageBrokerServlet</servlet-name>
        <url-pattern>/messagebroker/*</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
    </welcome-file-list>


</web-app>

3.Greeting.java


    
package org.wang.flexdemo;

import java.util.Map;

/**
 * TODO
 * @author: wb
 * @update: 2012-3-7 下午4:58:08
 */

public class Greeting {
    public Greeting(){}
    
    public String greeting(String name){
        String returnstr=name+" greeting!";
        System.out.println(returnstr);
        return returnstr;
    }
    
    public void showObject(Person person){
        System.out.println("Method showObject() is start .......");
        if(null==person) System.out.println("person is null");
        System.out.println("person.name="+person.getName());
        System.out.println("person.age="+person.getAge());
        System.out.println("person.gender="+person.getGender());
        System.out.println("person.mobile="+person.getMobile());
        System.out.println("person.address="+person.getAddress());
        System.out.println("Method showObject() is end .......");
    }
    
    public void showMap(Map<String, Object> map){
        System.out.println("Method showMap() is start .......");
        if(null==map) System.out.println("map is null");
        for(Map.Entry<String, Object> entryMap:map.entrySet()){
            System.out.println(entryMap.getKey()+"="+entryMap.getValue());
        }
        System.out.println("Method showMap() is end .......");
    }
}

4.Person.java


    
package org.wang.flexdemo;

import java.io.Serializable;

/**
 * TODO
 * @author wangbo
 * @update 2012-3-7 下午101421
 */

public class Person implements Serializable {
    public String Name="";
    public int Age=0;
    public int Gender=0;
    public String Mobile="";
    public String Address="";
    public Person(){}
    /**                                                                    
     * @return name : return the property name.
     */
    
    public String getName() {
        return Name;
    }
    /**                                                      
     * @param name : set the property name.
     */
    
    public void setName(String name) {
        Name = name;
    }
    /**                                                                    
     * @return age : return the property age.
     */
    
    public int getAge() {
        return Age;
    }
    /**                                                      
     * @param age : set the property age.
     */
    
    public void setAge(int age) {
        Age = age;
    }
    /**                                                                    
     * @return gender : return the property gender.
     */
    
    public int getGender() {
        return Gender;
    }
    /**                                                      
     * @param gender : set the property gender.
     */
    
    public void setGender(int gender) {
        Gender = gender;
    }
    /**                                                                    
     * @return mobile : return the property mobile.
     */
    
    public String getMobile() {
        return Mobile;
    }
    /**                                                      
     * @param mobile : set the property mobile.
     */
    
    public void setMobile(String mobile) {
        Mobile = mobile;
    }
    /**                                                                    
     * @return address : return the property address.
     */
    
    public String getAddress() {
        return Address;
    }
    /**                                                      
     * @param address : set the property address.
     */
    
    public void setAddress(String address) {
        Address = address;
    }
    
}

5./WEB-INF/flex/remoting-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<service id="remoting-service"
    class="flex.messaging.services.RemotingService">

    <adapters>
        <adapter-definition id="java-object" class="flex.messaging.services.remoting.adapters.JavaAdapter" default="true"/>
    </adapters>

    <default-channels>
        <channel ref="my-amf"/>
    </default-channels>
    
    <destination id="greeting">
        <properties>
            <source>org.wang.flexdemo.Greeting</source>
        </properties>
    </destination>
</service>


-------------------------------------------------------

Flex

-------------------------------------------------------

1./flexBlazeDSDemo/src/flexBlazeDSDemo.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">
    <fx:Script>
        <![CDATA[
            import mx.controls.Alert;
            import mx.rpc.events.FaultEvent;
            protected function button1_clickHandler(event:MouseEvent):void{
                var iname:String=tiName.text;
                say.greeting(iname);
            }
            protected function button1_clickHandler2(event:MouseEvent):void{
                var person:Person=new Person;
                person.Name="wan";
                person.Age=26;
                person.Gender=1;
                person.Mobile="13669040351";
                person.Address="湖南长沙";
                say.showObject(person);
            }
            protected function button1_clickHandler3(event:MouseEvent):void{
                var map={Name:"wan",Age:26,Gender:1,Mobile:"13669040351",Address:"湖南长沙"};
                say.showMap(map);
            }
            
            protected function getFaultInfos(event:FaultEvent){
                var str:String=event.fault.toString();
                Alert.show(str);
            }
        ]]>
    </fx:Script>
    <fx:Declarations>
        <!-- 将非可视元素(例如服务、值对象)放在此处 -->
        <s:RemoteObject id="say" destination="greeting" fault="getFaultInfos" />
    </fx:Declarations>
    
    <s:Panel x="190" y="67" width="394" height="228" title="Flex与Java交互实例">
        <s:Button x="265" y="13" label="提交" click="button1_clickHandler(event)" />
        <s:TextInput x="89" y="12.5" id="tiName" />
        <s:Label x="39" y="17.5" text="姓名:" />
        <s:Label text="{say.greeting.lastResult}" x="44" y="85" width="291" height="71" id="lbView" color="#5D44F5" fontSize="20" fontWeight="bold" textDecoration="none" fontStyle="normal" />
        <s:Button x="44" y="136" label="提交2" click="button1_clickHandler2(event)" />
        <s:Button x="185" y="137" label="提交3" click="button1_clickHandler3(event)" />
    </s:Panel>
</s:Application>

2./flexBlazeDSDemo/src/Person.as

package
{

    [Bindable]
    [RemoteClass(alias="org.wang.flexdemo.Person")]

    public class Person
    {
        public function Person()
        {
        }
        
        public var Name:String="";
        public var Age:int=0;
        public var Gender:int=0;
        public var Mobile:String="";
        public var Address:String="";
    }
}


你可能感兴趣的:(java,function,String,application,mobile,button)