One of the three methods for interaction between flex and java: Remote Object

Source: Internet
Author: User

This demo is also written based on the process of executing the program from the front-end page to the back-end java program.




Note: The blazeds is introduced in this program.




1. component elements on the page
View plaincopy to clipboardprint? <S: HGroup>
<S: TextInput id = "username" width = "120" type = "codeph" text = "/codeph"/>

<S: Button label = "clickMe" click = "clickMe ()"/>
</S: HGroup>
<S: HGroup>
<S: TextInput id = "username" width = "120" type = "codeph" text = "/codeph"/>

<S: Button label = "clickMe" click = "clickMe ()"/>
</S: HGroup>
2. Implement the method clickMe () function in the click attribute

View plaincopy to clipboardprint? <Fx: Script>
<! [CDATA [
Private function clickMe (): void {
Var value: String = this. username. text;

This. testService. sayHello (value );
}
]>
</Fx: Script>
<Fx: Script>
<! [CDATA [
Private function clickMe (): void {
Var value: String = this. username. text;

This. testService. sayHello (value );
}
]>
</Fx: Script> note: the remote object testService and the method sayHello () in the remote object are used in the clickMe () function. No prompt is given when writing this method.

3. Introduce the remote object testService to the page

View plaincopy to clipboardprint? <Fx: Declarations>
<! -- Place non-visual elements (such as services and value objects) Here -->
<S: RemoteObject id = "testService" destination = "testService">
<S: method name = "sayHello" result = "returnResultHandler (event)"/>
</S: RemoteObject>
</Fx: Declarations>
<Fx: Declarations>
<! -- Place non-visual elements (such as services and value objects) Here -->
<S: RemoteObject id = "testService" destination = "testService">
<S: method name = "sayHello" result = "returnResultHandler (event)"/>
</S: RemoteObject>
</Fx: Declarations> explanation: 1) the destination property introduces the object name in the <destination> tag configured in the remoting-config.xml file (the xml file is given in Part 5 below)

2) The id in <s: RemoteObject> indicates a unique identifier name for the object.

3) The name attribute in <s: method> lists the names of methods in remote objects. The result attribute declares a callback function to process the result value, the result value is encapsulated in the event parameter.


4. describes the processing function returnResultHandler (event) for processing returned results)

View plaincopy to clipboardprint? <Fx: Script>
<! [CDATA [
Import mx. controls. Alert;
Import mx. rpc. events. ResultEvent;


Private function returnResultHandler (event: ResultEvent): void {
Var str: String = String (event. result );

Alert. show (str );
}
]>
</Fx: Script>
<Fx: Script>
<! [CDATA [
Import mx. controls. Alert;
Import mx. rpc. events. ResultEvent;


Private function returnResultHandler (event: ResultEvent): void {
Var str: String = String (event. result );

Alert. show (str );
}
]>
</Fx: Script> explanation: 1) event. result returns the result of the remote object, converts it to the String type, and copies the result to str.

2) the dialog box is displayed and the value of str variable Alert. show (str) is output );


5. java objects exposed in the remoting-config.xml file for flex pages

View plaincopy to clipboardprint? <Service id = "remoting-service"
Class = "flex. messaging. services. RemotingService">

<Destination id = "testService">
<Properties>
<Source> com. test. service. TestService </source>
</Properties>
</Destination>

</Service>
<Service id = "remoting-service"
Class = "flex. messaging. services. RemotingService">

<Destination id = "testService">
<Properties>
<Source> com. test. service. TestService </source>
</Properties>
</Destination>

</Service> Note: There is no prompt during the file writing process. Please pay more attention when you enter it! Preferably copy

6. The file for the remote object com. test. service. TestService in the configuration file should be as follows:


View plaincopy to clipboardprint? Public class TestService {

Public String sayHello (String name ){

System. out. println ("method = sayHello ");
Return "hello:" + name;
}
}
Public class TestService {

Public String sayHello (String name ){

System. out. println ("method = sayHello ");
Return "hello:" + name;
}
}



The above is a small example of flex + java interaction, which is completed through the Remote Object method!

Author: "liuwang8888"
 

Author: "liuwang8888"
 

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.