Flex+J2EE实例(cairngorm+blazeDS+hibernate+spring) part4
----add cairngorm
1.添加在libs 下 添加Cairngorm.swc ,此时,具备了cairngorm框架能力
2.运用cairngorm框架
2.1 在flex_src 下创建如下文件夹 和 文件
AdminVO.as
package vo.AdminVO
{
[Bindable]
public class AdminVO
{
public var username:String;
public var password:String;
public var age:String;
public var email:String;
}
}
###################
AdminModel.as
package model.AdminModel
{
import mx.collections.ArrayCollection;
import vo.AdminVO.AdminVO;
[Bindable]
public class AdminModel
{
public var adminDatas:ArrayCollection = new ArrayCollection();
public var adminVO:AdminVO = new AdminVO();
public var value :String = '' ;
}
}
#######################
AppModelLocator.as
package model
{
import com.adobe.cairngorm.model.ModelLocator;
import model.AdminModel.AdminModel;
[Bindable]
public class AppModelLocator implements ModelLocator
{
private static var __instance:AppModelLocator = null;
public static function getInstance():AppModelLocator
{
if(__instance == null)
{
__instance=new AppModelLocator();
}
return __instance;
}
//提供单例 model
public var adminModel:AdminModel = new AdminModel();
}
}
###################
AdminEvent.as
package event.AdminEvent
{
import com.adobe.cairngorm.control.CairngormEvent;
public class AdminEvent extends CairngormEvent
{
//事件名称
public static const ADMIN_LOGIN_EVENT:String="adminLoginEvent"; //企业能耗曲线图
//事件类型
public var event_type:String="";
public function AdminEvent(type:String)
{
this.event_type=type;
super(type);
}
}
}
#################################
Services.mxml
<?xml version="1.0" encoding="utf-8"?>
<cairngorm:ServiceLocator
xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
xmlns:cairngorm="http://www.adobe.com/2006/cairngorm">
<!--~~~~~~~~~~~~~~~~~~~~~~Declarations~~~~~~~~~~~~~~~~~~~~~~-->
<fx:Declarations>
<s:RemoteObject id="adminRO" destination="administratorService" showBusyCursor="true" endpoint="../messagebroker/amf"
/>
</fx:Declarations>
</cairngorm:ServiceLocator>
#################################
AdminDL.as
package business.delegates
{
public class AdminDL
{
import com.adobe.cairngorm.business.ServiceLocator;
import mx.rpc.AsyncToken;
import mx.rpc.IResponder;
import mx.rpc.remoting.RemoteObject;
public var serviceLocator:ServiceLocator = ServiceLocator.getInstance();
public var _remoteobject:RemoteObject;
public var _responder:IResponder;
public function AdminDL(responder:IResponder)
{
_remoteobject = serviceLocator.getRemoteObject("adminRO");
_responder = responder;
}
public function getDate(username:String):void
{
var token:AsyncToken = _remoteobject.firstApp(username);
token.addResponder(_responder);
}
}
}
############################
AdminCommand.as
package commands.AdminCommand
{
import business.delegates.AdminDL;
import com.adobe.cairngorm.commands.ICommand;
import com.adobe.cairngorm.control.CairngormEvent;
import event.AdminEvent.AdminEvent;
import model.AppModelLocator;
import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.rpc.Responder;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import vo.AdminVO.AdminVO;
public class AdminCommand implements ICommand
{
private var _model:AppModelLocator = AppModelLocator.getInstance();
private var adminEvent:AdminEvent ; //or public
public function execute(event:CairngormEvent):void
{
adminEvent = event as AdminEvent;
if(adminEvent.type == AdminEvent.ADMIN_LOGIN_EVENT){
var responder:Responder = new Responder(result,fault);
var dl:AdminDL = new AdminDL(responder);
dl.getDate(_model.adminModel.adminVO.username);
}
}
private function result(e:ResultEvent):void{
if(e.result !=null)
{
/**********返回列表*************/
_model.adminModel.adminDatas.removeAll();
var tempList:ArrayCollection = e.result as ArrayCollection;
var arr:Array = tempList.toArray();
for(var a:String in arr){
var data:AdminVO = new AdminVO();
data.username = arr[a][0];
data.password = arr[a][1];
data.age = arr[a][2];
data.email = arr[a][3]
_model.adminModel.adminDatas.addItem(data);
}
}else{
Alert.show("查找失败!");
}
}
//如果请求发送不成功则执行
private function fault(e:FaultEvent):void{
Alert.show("请求发送失败!");
}
}
}
###############################
AppControl.as
package control
{
import com.adobe.cairngorm.control.FrontController;
import event.AdminEvent.AdminEvent;
import commands.AdminCommand.AdminCommand;
public class AppControl extends FrontController
{
public function AppControl()
{
super();
//注册一个事件,将其与一个命令绑定
this.addCommand(AdminEvent.ADMIN_LOGIN_EVENT,AdminCommand);
}
}
}
2.2将FlexJ2ee.mxml置于views文件夹下
修改代码如下
<?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" xmlns:business="business.*" xmlns:control="control.*">
<fx:Declarations>
<business:Services/>
<control:AppControl/>
</fx:Declarations>
<fx:Script>
<![CDATA[
import event.AdminEvent.AdminEvent;
import model.AppModelLocator;
import mx.controls.Alert;
import mx.rpc.events.ResultEvent;
[Bindable]
public var _model:AppModelLocator=AppModelLocator.getInstance();
protected function button1_clickHandler(event:MouseEvent):void
{
_model.adminModel.adminVO.username = nameInput.text;
var e:AdminEvent = new AdminEvent(AdminEvent.ADMIN_LOGIN_EVENT);
e.dispatch();
}
]]>
</fx:Script>
<s:TextArea id="backTxt" x="109" y="122" height="99"/>
<!--<s:Button label="send" click="RemotingSayHello(event)" x="110" y="243"/>-->
<s:TextInput id="nameInput" x="109" y="73"/>
<s:Label x="47" y="75" text="Rieciver:"/>
<s:Label x="47" y="36" text="这是我的第一个FLEXJ2EE项目
欢迎大家访问我的BLOG
http://blog.csdn.net/kkkloveyou"/>
<s:Button x="109" y="249" label="按钮" click="button1_clickHandler(event)"/>
<mx:DataGrid x="320" y="79" dataProvider="{_model.adminModel.adminDatas}">
<mx:columns>
<mx:DataGridColumn headerText="username" dataField="username"/>
<mx:DataGridColumn headerText="password" dataField="password"/>
<mx:DataGridColumn headerText="age" dataField="age"/>
<mx:DataGridColumn headerText="email" dataField="email"/>
</mx:columns>
</mx:DataGrid>
</s:Application>
2.3修改FlexJ2ee.mxml的启动路径
右键FlexJ2ee.mxml
3.配置项目
3.1 将remoting-config.xml中的
<destination id="hello">
<properties>
<source>
com.Hello
</source>
</properties>
</destination>
删除
3.2 在WEB-INF下 添加 flex-servlet.xml 对flex 消息进行管理
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:flex="http://www.springframework.org/schema/flex"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/flex
http://www.springframework.org/schema/flex/spring-flex-1.0.xsd">
<flex:message-broker>
<flex:message-service
default-channels="my-streaming-amf,my-longpolling-amf,my-polling-amf" />
<flex:secured />
</flex:message-broker>
</beans>
3.2 在WEB-INF下 添加spring文件夹,并在底下创建3个配置文件
app-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"
default-autowire="byName">
<!-- <context:annotation-config/>-->
<aop:aspectj-autoproxy proxy-target-class="true"/>
<context:component-scan base-package="com.pkms"/>
<!-- 事物配置 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory"
ref="sessionFactory">
</property>
</bean>
<tx:advice id="txAdvice"
transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*"
propagation="REQUIRED"/>
<tx:method name="update*"
propagation="REQUIRED"/>
<tx:method name="del*"
propagation="REQUIRED"/>
<tx:method name="save*"
propagation="REQUIRED"/>
<tx:method name="query*"
propagation="REQUIRED"/>
<tx:method name="get*"
read-only="true"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:advisor pointcut="execution(public * com.app.dao.*DAO.*.*(..))"
advice-ref="txAdvice"/>
<aop:advisor pointcut="execution(public * com.core.hibernate.*Dao.*(..))"
advice-ref="txAdvice"/>
</aop:config>
</beans>
#######################
infrastructure-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="com.mysql.jdbc.Driver">
</property>
<property name="url" value="jdbc:mysql://localhost:3306/pkms"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
</props>
</property>
</bean></beans>
#######################
security-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.xsd">
<http entry-point-ref="entryPoint">
<anonymous enabled="false"/>
</http>
<beans:bean id="entryPoint" class="org.springframework.security.web.authentication.Http403ForbiddenEntryPoint"/>
<authentication-manager>
<authentication-provider>
<user-service>
<user name="john" password="john" authorities="ROLE_USER" />
<user name="admin" password="admin" authorities="ROLE_USER, ROLE_ADMIN" />
<user name="guest" password="guest" authorities="ROLE_GUEST" />
</user-service>
</authentication-provider>
</authentication-manager>
</beans:beans>
3.3 修改 web.xml为:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<display-name>BlazeDS Spring Integration Application</display-name>
<description>BlazeDS Spring Integration Application</description>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/*-config.xml
</param-value>
</context-param>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Http Flex Session attribute and binding listener support -->
<listener>
<listener-class>flex.messaging.HttpFlexSession</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Spring Dispatcher Servlet -->
<servlet>
<servlet-name>flex</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>flex</servlet-name>
<url-pattern>/messagebroker/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
</welcome-file-list>
</web-app>
4.将项目部署至tomcat, 右键运行FlexJ2ee.mxml
在Reciver中输入 数据库中数据 lww 或者 xyp,结果如下
下载地址