Flex用RemoteObject方式與Java通訊是最常用的方式,這是一種最直觀的方式。當然Flex也可以用HttpService與伺服器類如servlet通訊,這也是本次學習的重點。
這次學習是在上節的基礎上進行的。本節學慣用到的LoginEvent.as,LoginModule.mxml檔案代碼如上節所示。
好了,建立一個servlet類LoginServlet.java,代碼如下所示:
package com.yqsn.servlet; import java.io.IOException;import java.io.PrintWriter; import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse; public class LoginServlet extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generatedmethod stub req.setCharacterEncoding("utf-8"); resp.setCharacterEncoding("utf-8"); PrintWriter out=resp.getWriter(); String username=req.getParameter("username"); String passworld=req.getParameter("passworld"); //System.out.println(username+":"+passworld); if(username.equals("admin")&& passworld.equals("123")){ out.print(true); }else{ out.print(false); } }}
這個servlet在web.xml中的配置如下所示:
<servlet> <servlet-name>LoginServlet</servlet-name> <servlet-class>com.yqsn.servlet.LoginServlet</servlet-class> </servlet><servlet-mapping> <servlet-name>LoginServlet</servlet-name> <url-pattern>/LoginServlet</url-pattern> </servlet-mapping>
將MyEclipse切換到flash視圖,建立一個application檔案HttpServiceDemo.mxml,代碼如下所示:
<?xmlversion="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 com.flex.ases.LoginEvent; import com.flex.ases.LoginMess; import com.flex.module.LoginModule; import mx.controls.Alert; import mx.managers.PopUpManager; import mx.rpc.events.FaultEvent; import mx.rpc.events.ResultEvent; private var loginModule:LoginModule=new LoginModule(); [Bindable] private var username:String; [Bindable] private varpassworld:String; protected function login_clickHandler(event:MouseEvent):void { // TODOAuto-generated method stub PopUpManager.addPopUp(loginModule,this,true); PopUpManager.centerPopUp(this.loginModule); loginModule.addEventListener(LoginEvent.LOGIN_EVENT,loginHander); } public function loginHander(event:LoginEvent):void{ //varobj:Object=event.loginMess as Object; username=event.loginMess['username']; passworld=event.loginMess['passworld']; httpServiceSend.send(); } protected function httpServiceSend_faultHandler(event:FaultEvent):void { // TODOAuto-generated method stub Alert.show(event.fault.message as String,"提示"); } protected function httpServiceSend_resultHandler(event:ResultEvent):void { // TODOAuto-generated method stub var result:Boolean=event.result as Boolean; if(result){ Alert.show(username+",歡迎您回來!","提示"); aaa.text=username+",歡迎您回來!"; login.label=""; bbb.text=""; }else{ Alert.show("對不起,使用者名稱或密碼不存在!","提示"); } //Alert.show("成功了!"); } ]]> </fx:Script> <fx:Declarations> <!-- Place non-visualelements (e.g., services, value objects) here --> <s:HTTPService id="httpServiceSend" url="http://localhost:8000/JavaAndFlexDemo/LoginServlet" useProxy="false"fault="httpServiceSend_faultHandler(event)"result="httpServiceSend_resultHandler(event)" > <s:request > <username>{username}</username> <passworld>{passworld}</passworld> </s:request> </s:HTTPService> </fx:Declarations> <s:Label x="200" y="150" width="182" height="27" fontSize="18" id="aaa" text="您還沒有登陸,現在就" verticalAlign="middle"/> <mx:LinkButton x="393" y="150" width="57" height="27" label="登陸" id="login" fontSize="18"click="login_clickHandler(event)"/> <s:Label x="459" y="150" width="37" height="27" id="bbb" fontSize="18" text="吧!" verticalAlign="middle"/></s:Application>
從代碼中我們可以看出,我們先定義一個HttpServlet發送請求httpServiceSend,然後通過下面這種方式存值:
<s:request > <username>{username}</username> <passworld>{passworld}</passworld> </s:request>
這種方式很簡單,我們在後台通過request. getParameter(“參數名”)取值就可以了,當然我們也可以在loginHander(event:LoginEvent)函數中通過下面方式存值並發送請求:
public functionloginHander(event:LoginEvent):void{ //varobj:Object=event.loginMess as Object; username=event.loginMess['username']; passworld=event.loginMess['passworld']; var obj:Object=new Object; obj.username=username; obj.passworld=passworld; httpServiceSend.send(obj); }
運行結果是一樣的,你可以試試。
好了,程式算是完成了,現在開始驗收結果。
開啟伺服器並部署項目,運行felx頁面RemoteObjectDemo.mxml,如下所示:
當我們點擊“登陸”按鈕後,彈出module頁面,如下所示:
當我們輸入的使用者名稱和密碼都正確時則提示你登陸正確:
輸入錯誤則提示你輸入不正確:
可以看出,我們輸入的使用者名稱與密碼已經用httpservice方式發送到後台並且成功接受了並將結果返回給前台了。
好了,就學習這麼多,下節將學習WebService方式。
原創文章,轉載請註明出處:http://www.dianfusoft.com/showDetail.action?articleId=130410011145
,更多原創文章,請訪問:http://www.dianfusoft.com/