ArcGIS API for Flex V, Flex for Java custom Object conversion detailed

Source: Internet
Author: User
Tags java web tomcat server

First, build the project that flex interacts with Java.

The interaction mentioned in this article is to use BlazeDS, because this is free, hehe, I am poor.

The first is to download the BlazeDS compression package, which can be from the official website or CSDN, javaeye up and down. Unpack the package, unzip the Blazeds.war inside, and use it when you build the project.

To create a Web project in MyEclipse named Flextest. (Note: The JDK for this project must be version 1.5 or above, this article uses 6.0)

to copy the "Meta-inf" and "Web-inf" folders and folders in the Blazeds.war package that you just extracted to the webroot of the project. The Web-inf contains a flex profile and a jar package that blazeds needs. Then we can write Java code in this project. This article takes a list of users as an example to establish a user entity class. The code is as follows:

Package com.rocd.flex.entity;

 Import java.io.Serializable;

public class UserBean implements Serializable {
    private String userName;
    private String password;
    private int age;
    private String Email;

    Public String GetUserName () {return
        userName;
    }

    public void Setusername (String userName) {
        this.username = userName;
    }

    Public String GetPassword () {return
        password;
    }

    public void SetPassword (String password) {
        this.password = password;
    }

    public int getage () {return age
        ;
    }

    public void Setage (int age) {
        this.age = age;
    }

    Public String Getemail () {return
        email;
    }

    public void Setemail (String email) {
        This.email = email;
    }
}


Note that because this entity class needs to be passed to the as code in flex as the return value of the method in Java code, the serializable interface needs to be inherited, otherwise it will cause an exception to occur.

Then, create a Java class that is called by Flex. The code is as follows

Package com.rocd.flex.biz;

Import java.util.ArrayList;
Import java.util.List;

Import Com.rocd.flex.entity.UserBean;

public class Usermanager {public
    list<userbean> getuserlist () {
        list<userbean> List = new ArrayList <UserBean> ();
        for (int i = 0; i < i++) {
            UserBean user = new UserBean ();
            User.setusername ("user" + i);
            User.setpassword ("123");
            User.setage (+ i);
            User.setemail ("user" + i + "@aaa. com");
            List.add (user);
        }
        return list;
    }


This is not connected to the database to operate, because this article is focused on the flex call Java, so write a pile of data to test.

Having written these two classes, you need to configure the channel in Webroot/web-inf/flex/remoting-config.xml to invoke this Java class. The specific code is as follows:

Code
<?xml version= "1.0" encoding= "UTF-8"?> <service id= "Remoting-service" 
    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= "Usermanager" >
        <properties>
            <source>com.rocd.flex.biz.usermanager</ source>
        </properties>
    </destination>
</service>

Where destination is the channel, properties can be configured with multiple Java classes, and source is configuring the package path to invoke Java classes. By this, the Java section of the code is finished. Here is the code in flex.

Create a flex project in Flex Builder. Name is Flexapp. As shown in the figure:

Note that Project location's folder selects the Webroot of the Web project that MyEclipse builds. Server Technology Select Java EE
Click on the [Next] button to go to the next page. As shown in figure

Note The server Location root folder to select the Webroot path of the Java Web project. The Root URL is the access path to the Java Web project, and because the Tomcat server is used, the project name is Flextest, so the path is http://localhost:8080/FlexTest/and the context root is the Java The project name of the Web project. When you are done, click on the [Validate Configuration] to verify that if the top of the window shows the "Yellow triangle" shown above, you can proceed to the next step.
After the smooth completion of the Flex project, you need to pay attention to whether the error, if the error, please note that adjust the Flex SDK version.
Once you have no problem, create an as class to receive the return value of the Java program, which is the Userbean. The code is as follows:

Code

Package com.rocd.flex.entity
{
    [bindable]
    [Remoteclass (alias= "Com.rocd.flex.entity.UserBean" )] public 
    class UserBean
    {public
        var username:string;
        public var password:string;
        public var age:int;
        public var email:string;
    }
}

Here [bindable] is bindable, [Remoteclass (alias= "Com.rocd.flex.entity.UserBean")] is associated with the UserBean class in Java code. This allows you to convert Java objects to as objects by using type conversions.

Now let's call this Java code in flex. The code is as follows:

<?xml version= "1.0" encoding= "Utf-8"?> <mx:application xmlns:mx= "Http://www.adobe.com/2006/mxml" Absolute "fontsize=" initialize= "init ()" > <mx:Script> <!
            [cdata[import Com.rocd.flex.entity.UserBean;
            Import mx.collections.ArrayCollection;
            
            Import mx.rpc.events.ResultEvent;
                Private Function init (): void {usermanager.getuserlist ();
            Usermanager.addeventlistener (resultevent.result,getuserlist); Private Function Getuserlist (event:resultevent): void {var userlist
                : ArrayCollection = arraycollection (Event.result);
                var userlist_flexdata:arraycollection = new ArrayCollection (); for (var i:int = 0; i < userlist.length; i++) {var User:userbean = UserBean (Userli
                    St.getitemat (i)); Userlist_flexdaTa.additem (user);
            } Datagrid.dataprovider = Userlist_flexdata; }]]> </mx:Script> <mx:remoteobject id= "Usermanager" destination= "Usermanager" Showbu
        Sycursor= "true"/> <mx:panel x= "" y= "" width= "399" height= "285" layout= "absolute" title= "user List" > <mx:datagrid x= "0" y= "0" width= "379" height= "243" id= "DataGrid" > <mx:columns> < Mx:datagridcolumn headertext= "User name" datafield= "UserName"/> <mx:datagridcolumn "age" headertext= Ld= "Age"/> <mx:datagridcolumn headertext= "email" datafield= "email"/> </mx:columns&
        Gt </mx:DataGrid> </mx:Panel> </mx:Application>


Run this flex program after starting the Tomcat server, as shown in the following illustration:


Reference Address: http://www.cnblogs.com/RocD-DuPeng/articles/1751040.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.