Java and flex Study Notes (4) -- remoteobject Method for communication between flex and Java

Source: Internet
Author: User

After learning the flash event mechanism, we began to learn three communication methods in flex and Java. FLEX can communicate with Java in three ways:

 

● Flex accesses Java common classes using the remoteobject method, which is also the most commonly used method.


● Flex uses httpservice to access Java Server classes (such as servlets.


● Flex Interacts With WebService in WebService mode.

 

Today, I will first learn how to use flex to access common Java classes. Before learning, we need to consider such a problem: Because we use logon as an example in all three examples, we should try to separate the login interface for reuse, this uses the module in flex. How can we transmit the values in the module to the parent window? We also think of the custom events learned in the previous section. Now that you have figured it out, you can start learning today.

 

Switch myeclipse to the myeclipse view and create a common Java class that interacts with Flex. The Code is as follows:


package com.yqsn.test; public class RemoteObjectDemo {    public boolean login(String username,String passworld ){             if(username.equals("admin")&&passworld.equals("123")){           return true;       }else{           return false;       }          }}

 

Add the following code to the WEB-INF file under the webroot/remoting-config.xml/flex directory:


<destination id="remoteObjectDemo">       <properties>           <source>com.yqsn.test.RemoteObjectDemo</source>       </properties>    </destination>

 

 

Switch myeclipse to the Flash view. First, define an event class loginevent. As to serve future value transfer. The Code is as follows:


package com.flex.ases{    import flash.events.Event;       public class LoginEvent extends Event    {         public static const LOGIN_EVENT:String="LOGIN_EVENT";             private  var _loginMess:Object;             public function LoginEvent(type:String,loginMess:Object=null, bubbles:Boolean=false,cancelable:Boolean=false)       {           this._loginMess=loginMess;           super(type, bubbles, cancelable);       }             public function get loginMess():Object       {           return _loginMess;       }        public function set loginMess(value:Object):void       {           _loginMess = value;       }     }}

In this class, we define an event type.Login_eventDefines an object-type variable for storing values.


Create a Vo-class loginmess. As for the login information to serve the user information storage in the future. The Code is as follows:


package com.flex.ases{    public class LoginMess    {             private  var _username:String;             private var _passworld:String;             public function LoginMess()       {                 }        public function get passworld():String       {           return _passworld;       }        public function set passworld(value:String):void       {           _passworld = value;       }        public function get username():String       {           return _username;       }        public function set username(value:String):void       {           _username = value;       }     }}

 

Create a new mxmlmodule file loginmodule. mxml for login. The Code is as follows:

 

<? Xmlversion = "1.0" encoding = "UTF-8"?> <S: module xmlns: FX = "http://ns.adobe.com/mxml/2009" xmlns: S = "Library: // ns.adobe.com/flex/spark" xmlns: MX = "Library: // ns.adobe.com/flex/mx "width =" 256 "Height =" 213 "> <FX: SCRIPT> <! [CDATA [import COM. flex. ases. loginevent; import MX. controls. alert; import MX. events. closeevent; import MX. managers. popupmanager; protected function login_clickhandler (Event: mouseevent): void {// todoauto-generated method stub var loginmess: Object = new object; loginmess. username = username. text; loginmess. passworld = passworld. text; If (username. TEXT = "" | passworld. TEXT = "") {alert. show ("the user name or password cannot be blank! "); Return;} This. dispatchevent (newloginevent (loginevent. login_event, loginmess); username. TEXT = ""; passworld. TEXT = ""; popupmanager. removepopup (this);} protected function logintitlewindow_closehandler (Event: closeevent): void {// todo auto-generatedmethod stub username. TEXT = ""; passworld. TEXT = ""; popupmanager. removepopup (this);}]> </FX: SCRIPT> <FX: declarations> <! -- Place non-visualelements (e.g ., services, value objects) Here --> </FX: declarations> <s: titlewindow x = "1" Y = "1" width = "256" Height = "213" Title = "login" id = "logintitlewindow" Close = "logintitlewindow_closehandler (Event) "> <s: Form width =" 100% "Height =" 183 "> <s: formitem left = "60" Height = "39" width = "224" label = "username" required = "true"> <s: textinput id = "username"/> </S: formitem> <s: formitem required = "true" width = "224" label = "password"> <s: textinput id = "passworld" displayaspassword = "true"/> </S: formitem> <s: formitem width = "227"> <s: button id = "login" label = "login" Click = "login_clickhandler (event)"/> </S: formitem> </S: Form> </s: titlewindow> </S: module>

 

This page will be used repeatedly later, which is one of the advantages of the module File. On this page, we will not process the interaction with Java, because since it is a public page, we should place the interaction part of Java in the corresponding Referenced File.


Create remoteobjectdemo. mxml on the home page. 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 "width =" 100% "Height =" 100% "> <FX: SCRIPT> <! [CDATA [import COM. flex. ases. loginevent; import COM. flex. ases. loginmess; import COM. flex. component. logintitlewindow; import COM. flex. module. loginmodule; import MX. collections. arraycollection; import MX. controls. alert; import MX. managers. popupmanager; import MX. RPC. events. faultevent; import MX. RPC. events. resultevent; [Bindable] private var loginmess: loginmess = new loginmess (); Private var loginmodule: Loginmodule = new loginmodule (); protected function login_clickhandler (Event: mouseevent): void {popupmanager. addpopup (loginmodule, this, true); popupmanager. centerpopup (loginmodule); loginmodule. addeventlistener (loginevent. login_event, getloginmess);} public function getloginmess (Event: loginevent): void {var Username: String = event. loginmess ['username']; var passworld: String = event. loginmess ['passworld']; Loginmess. username = username; remoteobj. login (username, passworld);} protected function remoteobj_resulthandler (Event: resultevent): void {// todoauto-generated method stub var STR: Boolean = event. result as Boolean; If (STR) {alert. show (loginmess. username + ", welcome back... "," prompt "); AAA. TEXT = loginmess. username + ", welcome back... "; BBB. TEXT = ""; login. label = "";} else {alert. show ("Logon Failed. the user name or password you entered does not exist! "," Prompt ") ;}} protected function remoteobj_faulthandler (Event: faultevent): void {// todoauto-generated method stub alert. show (event. fault. message, "error") ;}]]> </FX: SCRIPT> <FX: declarations> <! -- Place non-visualelements (e.g ., services, value objects) Here --> <s: remoteobject id = "remoteobj" Destination = "remoteobjectdemo" result = "remoteobj_resulthandler (event)" fault = "remoteobj_faulthandler (Event) "/> </FX: declarations> <s: label x = "219" Y = "150" width = "182" Height = "27" fontsize = "18" id = "AAA" text = "You have not logged on yet, "verticalalign =" Middle "/> <mx: linkbutton x =" 409 "Y =" 150 "width =" 57 "Height =" 27 "Lab El = "login" id = "login" fontsize = "18" Click = "login_clickhandler (event)"/> <s: label x = "478" Y = "150" width = "37" Height = "27" id = "BBB" fontsize = "18" text =! "Verticalalign =" Middle "/> </S: Application>

 

Now, the page and class are processed. Open the server and deploy the project, and run remoteobjectdemo. mxml on the felx page, as shown below:




After you click "Log on", the module page is displayed, as shown below:




When the username and password we entered are correct, the system prompts you to log on correctly:





If the input is incorrect, the system prompts that the input is incorrect:




We can see that the username and password we entered interact with the login method in Java.


Now, we will learn so much about httpservice.


Original article, reprint please indicate the source: http://www.dianfusoft.com/showDetail.action? Article ID = 130405020736, more original content, visit: http://www.dianfusoft.com/




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.