Flex + Java + Blazeds, flexjavablazeds

Source: Internet
Author: User

Flex + Java + Blazeds, flexjavablazeds

1. Environment: jdk1.6 and Flex4.6

2. Tool: MyEclipse10

3. SERVER: Tomcat7

4. Connection Method: Blazeds

5. Project type: Flex Project

6. Steps

(1) create a new Flex project named FlexToJava

 

After entering the project name, click "Next ".

 

Select BlazeDS and select "use WTP ...", Click Next. By default, the project is created.

The new project directory is as follows:

 

 

(2) Add the new project to Tomcat

 

Start Tomcat, run the FlexToJava. mxml page, and start properly.

If a startup error occurs

 

 

Find the context. xml file in the conf directory of Tomcat and add <Loader delegate = "true"/>.

 

(3) switch to the Java end and create a new Java class FlexToJava. java

 

(4) locate the WEB-INF under the WebRoot \ remoting-config.xml \ flex directory and add a piece of code for configuring the Java class. The id here is the value of the destination attribute in the RemoteObject on the Flex page.

<destination id="myFlexToJava"><properties><source>com.java.FlexToJava</source></properties></destination>


(5) Find the front-end Flex page-FlexToJava. mxml and write the code to call the backend Java method.

The Code is as follows:

<? 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. resultEvent; protected function button?clickhandler (event: MouseEvent): void=this.ro.getname(this.txt. text); this. ro. addEventListener (ResultEvent. RESULT, getName);} protected function getName (event: ResultEvent): void {Alert. show (event. result. toString ();} // There are two methods to process the returned value. One is to write data directly in RemoteObject, and the other is addEventListenerprivate function error (e: FaultEvent ): void {errorMsg. text = e. message. toString () ;}]]> </fx: Script> <fx: Declarations> <s: RemoteObject id = "ro" destination = "myFlexToJava" fault = "error (event) "> </s: RemoteObject> </fx: Declarations> <s: Label x =" 65 "y =" 80 "text =" name "/> <s: textInput id = "txt" x = "97" y = "73"/> <s: button x = "250" y = "74" label = "get" click = "button#clickhandler (event)"/> </s: Application>


Run the Flex file and obtain the following interface:

 

However, an error is reported.

 

[RPC Fault faultString = "Send failed"

FaultCode = "Client. Error. MessageSend"

FaultDetail = "Channel. Connect. Failed error NetConnection. Call. Failed: HTTP: Status 404:

Url: 'http: // localhost: 8080/WebRoot/messagebroker/amf' "]

Observe this error and find that the url is incorrect. Our address should be http: // localhost: 8080/FlexToJava, rather than http: // localhost: 8080/WebRoot, so you need to modify a place.

Go to the "properties" menu of the project,

 

Select the Flex server and find that "content folder" is "WebRoot". Remember to change the editor parameter in the Flex compiler

"-Services" D:/Workspaces/FlexToJava/WebRoot/WEB-INF/flex/services-config.xml "-locale en_US", otherwise an error will be reported, this is because after the Flex server is modified, the WebRoot will be automatically changed to FlexToJava and will be changed back.

 

(6) Redeploy the project, start Tomcat, run FlexToJava. mxml, enter the name, and click the button to bring up the dialog box. Successful!

Pay attention to the URL here. The default out URL will have one fewer out directory, and you need to configure it again in the running configuration. Alternatively, enter http: // localhost: 8080/FlexToJava/out in the URL project of the Output Folder under the Flex build path menu in the project properties menu, and the out directory will appear at runtime.

 

 

Postscript

I tried several times to check whether the result was successful or not. I tried to configure the content folder on the Flex server or other methods. This is a clear method, it is probably better. You can build it at a time. Use it together first.

This is how to create a Flex project from the Flex end and then embed it into Java. You can also create a Java Web project from the Java end and then embed the Flex project. I have tried several times and it has not been successful, I just learned something else.

When creating a Java Web project from the Java end, one step is to decompress Blazeds. war package, replace the WEB-INF of the new project with the four profiles under the flex folder under the WEB-INF, several must lib packages and web. xml file.

Among them, the flex folder under the four configuration files in the remoting-config.xml file, we configured a piece of code, this configuration is to guide flex how to access the java object, but also guide the Blazeds configuration class ing function. In addition, the Class address must be configured here, not the interface. Services-config.xml file, register the data channel, the following code defines a channel, Flex communicates with the server through this channel.

<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"/></channel-definition>

The above url is similar to the above error "http: // localhost: 8080/WebRoot/messagebroker/amf, we can see that the position of WebRoot in the error should be context. root (context root directory), I don't know whether it is a problem or something. Our error is solved after the value of "content folder" is modified, however, this should be the value of the "context root directory.

The knowledge about Blazeds and the configuration files must be further learned.

 

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.