转自http://www.cnblogs.com/kunpengit/archive/2011/12/06/2277657.html
Flex结合java实现一个登录功能
1、 环境的搭建:
s
对message-congig.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="loginserverTag">
<properties>
<source>com.wangkun.Login</source>
</properties>
</destination>
</service>
Web.xml的配置如下:
<display-name>BlazeDS</display-name>
<description>BlazeDS Application</description>
<!-- Http Flex Session attribute andbinding listener support -->
<listener>
<listener-class>flex.messaging.HttpFlexSession</listener-class>
</listener>
<!-- MessageBroker Servlet -->
<servlet>
<servlet-name>MessageBrokerServlet</servlet-name>
<servlet-class>flex.messaging.MessageBrokerServlet</servlet-class>
<init-param>
<param-name>services.configuration.file</param-name>
<param-value>/WEB-INF/flex/services-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<url-pattern>/messagebroker/*</url-pattern>
</servlet-mapping>
这些xml文件基本固定。
1、 创建Bean类User和Login类
public class Login {
public String login(String name, String psw) {
if ("aa".equals(name) && "123".equals(psw)) {
return "ok";
} else {
return "error";
}
}
public String login(User user){
if("sss".equals(user.getName()) && "123".equals(user.getPwd())){
return "good";
}else{
return "sorry";
public User login(){
return new User("wang","aaa",33);
}
启动tomcat,至此myeclipse里的配置结束。
3、
在flah builder里创建一个flex项目。
4、 对index.mxml的编写:
首先要在fx:Decaration中配置destination对应于message-congig.xml中的id
Result是回调函数,
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
<mx:RemoteObject id="loginClientTag"destination="loginserverTag" result="huidiao1(event)" endpoint="/flexApp01/messagebroker/amf"></mx:RemoteObject>
<mx:RemoteObject id="loginClientTag2"destination="loginserverTag" result="huidiao2(event)" endpoint="/flexApp01/messagebroker/amf"></mx:RemoteObject>
</fx:Declarations>
Fx:script中的代码如下:
<fx:Script>
<![CDATA[
import com.wang.asclass.User;
import mx.controls.Alert;
import mx.rpc.events.ResultEvent;
protected function huidiao1(event:ResultEvent):void
{
// TODO Auto-generated method stub
var result:String =event.result as String ;
Alert.show(result,"结果");
}
protected function huidiao2(event:ResultEvent):void
var result:User = event.result as User;
Alert.show(result.name);
trace(result.name);
protected function senddata(event:MouseEvent):void //按确认按钮时
var name:String=this.nametext.text;
var psw :String=this.pswtext.text;
var user :User = new User();
user.name=name;
user.pwd=psw;
loginClientTag.login(user);
protected function canceldata(event:MouseEvent):void //按取消按钮时
this.nametext.text="";
this.pswtext.text="";
protected function showdata(event:MouseEvent):void{ //按查询按钮
loginClientTag2.login();
]]>
</fx:Script>
视图:
<s:Panel x="184" y="71" width="375" height="259">
<s:Label x="34" y="27" text="姓名:" />
<s:TextInput id="nametext" x="78" y="18"/>
<s:Label x="34" y="72" text="密码:"/>
<s:TextInput id="pswtext" x="78" y="62"/>
<s:Button x="51" y="141" label="确定" click="senddata(event)"/>
<s:Button x="162" y="141" label="取消" click="canceldata(event)"/>
<s:Button x="272" y="141" label="查询" click="showdata(event)"/>
</s:Panel>
可创建一个名为User的as类
package com.wang.asclass
{
[Bindalbe]
[RemoteClass(alias="com.wangkun.User")]
public class User
{
public function User()
{
}
public var name : String;
public var pwd : String;
public var age : int;