Blazeds flex communicates with Java

Source: Internet
Author: User

Myeclipse_8.5 + flex_4 + blazeds Configuration

 

Software required:

Blazeds-turnkey-4.0.0.14931.zip,

Http://opensource.adobe.com/wiki/display/blazeds/Downloads

 

Blazds configuration: Download a blazds package, decompress it, copy the blazeds. War, ds-console.war, samples. War to the webapps folder under tomcat,

Run http: // localhost: 8080/samples to check whether the configuration is successful.

 

Building a web project is not that simple, because flex only serves as the presentation layer, and flex only receives data returned by Java. How can it communicate with the Java server? In this case, we need to use an open-source project, blazeds, developed by Adobe.

Blazeds is a server-based Java Remote Control (remoting) and Web messaging (messaging) Technology released with the lgpl (lesser GNU Public License) Public License. It enables back-end Java applications and Adobe Flex running on browsers
Applications communicate with each other. On the Java application server, it exists as a servlet, so it can be used in any standard Java network application. Yes: blazeds

2. Open myeclise8.0 and create a javaweb project. Select javaee 5.02

3. Right-click webroot and choose import-> General à file system. It is found that the flex folder is added under the WEB-INF directory, which is mainly the four configuration files for communication. 3

Right-click webroot and choose import-> General à file system.

Click Browse, select the tomoat Server installation address, and then select the blazeds folder in webapps (that is, the address where you configured the blazeds)

Click OK and select it

Click Finish and select yse to all.

Imported

4. Deploy the flexweb project to Tomcat. This is important because to integrate flex, you must start the service. The physical path of the deployed server is D: \ soft \ apache \ Tomcat \ webapps \ flexweb.

6. convert to a flex project. First, right-click your jspweb project and choose Add/change project type. Then, select Add flex project type.

7. Select the server as J2EE, and then select blazeds5

8. Configure your server path in the next step. As mentioned above, my physical path is D: \ soft \ apache \ Tomcat \ webapps \ flexweb. So fill in the following information: 6

The root folder is the physical service path, and the root URL is your access path. Here, the Tomcat port is 8080, So enter it. Check your port and add your project name, enter the project name in the context root directory. Finally, I changed the following directory to flex for the output path. Remember to verify that the configuration is valid only when it passes. 6

9. Resolving the creation error 7

Find problems and see an error. Right-click and choose "recreate HTML template. The error has been solved, but the new problem comes again. SRC is the place where Java source code is stored in the source folder of Java, but it is occupied by Flex now.

10. Change the flex project source file to flex_src, right-click the project property, find the main source folder of the Flex Key Path, and change SRC to flex_src9.

11. Move flexweb. XML in the SRC folder to the flex_src folder. The final directory path is 10.

The following describes how to implement communication between flex and Java.

Now we have basically finished it. Now we need to configure the relevant files. First, we need to open the flexweb project and switch to the myeclipse view. If a project error occurs in red XX, it doesn't matter because we didn't start Tomcat. If the server is started, we still need to re-compile the flex project. But here we will not do it until we finish it.

1. Create a Java file in the src directory

package rain.hr.loginValidation;public class Login {/** * @param args */public String userLogin(String name){System.out.println("someone login");return "hello"+name;}public static void main(String[] args) {// TODO Auto-generated method stub}}

 

Ii. Configure webroot/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="loginTest">    <properties>        <source>rain.hr.loginValidation.Login</source>    </properties> </destination></service>

The configuration file is added here.

<destination id="loginTest">    <properties>        <source>rain.hr.loginValidation.Login</source>    </properties> </destination>

 

This is a remote object method for configuring flex to access the Java method. After the configuration, it can communicate with Java. Let's try it.

 

Iii. configure remote objects in flex

My configuration 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: declarations> <s: remoteobject id = "login" Destination = "logintest" result = "login_resulthandler (event)"/> <! -- Place non-visual elements (such as services and value objects) Here --> </FX: declarations> <FX: SCRIPT> <! [CDATA [import MX. controls. alert; import MX. RPC. events. faultevent; import MX. RPC. events. resultevent; protected function login_resulthandler (Event: resultevent): void {var name: String = event. result as string; alert. show (name);} protected function btnsend_clickhandler (Event: mouseevent): void {login. userlogin (txtname. text) ;}]]> </FX: SCRIPT> <s: panel x = "87" Y = "20" width = "250" Height = "200" Title = "www.accptecg.com flex4"> <s: textinput x = "16" Y = "37" id = "txtname"/> <s: button x = "168" Y = "38" label = "send" id = "btnsend" Click = "btnsend_clickhandler (event)"/> </s: panel> </S: Application>

 

The following configuration is a remote object that calls the Java end, Destination = "logintest" is the ID name in the remoting-config.xml file we Just configured, result = "login_resulthandler" is the method in result if the server is successful.

<FX: declarations>
<S: remoteobject id = "login" Destination = "logintest" result = "login_resulthandler (event)"/>

</FX: declarations>

Then, click the send button to send the helloworld method of the remote object and execute click = "btnsend_clickhandler (event )"

Login. userlogin (txtname. Text );

Run the remoteobject id = "login" object corresponding to the userlogin method in the Java class rain. hr. loginvalidation. login.

4. re-verify the flex Server


Now you can access it. Show the small files

 

Solution: Open the blazeds directory, my directory is as follows: \ Tomcat 5.5.1 \ webapps \ blazeds \ WEB-INF
Find the Web. xml file, open the file, and find
<! -- Begin RDS>
<Servlet>
<Servlet-Name> rdsdispatchservlet </servlet-Name>
<Display-Name> rdsdispatchservlet </display-Name>
<Servlet-class> Flex. RDS. server. servlet. frontendservlet </servlet-class>
<Init-param>
<Param-Name> useappserversecurity </param-Name>
<Param-value> true </param-value>
</Init-param>
<Load-on-startup> 10 </load-on-startup>
</Servlet>
<Servlet-mapping id = "rds_dispatch_mapping">
<Servlet-Name> rdsdispatchservlet </servlet-Name>
<URL-pattern>/cfide/main/ide. cfm </url-pattern>
</Servlet-mapping>
<End RDS -->
Content, set <! -- Begin RDS> and <End RDS --> are registered and deleted,

Change "true" to "false" for security verification.

Save and restart the replicate Server
Registration: After modifying the configuration file, you must restart the replicate server.

I don't know why I sometimes changed the Web. xml under the Web. XML project under the directory. If the Web. XML project does not change, both of them will be changed.

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.