ssh+flex

1新建工程
2 加入jar包
添加hibernate5包
添加struts2包
添加spring4包
3 加入blazeds工程中代码
添加\blazeds工程中lib目录下的jar包添加到该工程中
\blazeds工程中的flex文件夹添加到工程的WEB-INFO目录下,有以下几个文件
message-config.xml
proxy-config.xml
remoting-config.xml
service-config.xml
version.properties


4 配置文件
spring配置文件 applicationContext.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:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context" 


xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" 


xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">


<!-- 自动扫描 -->
<context:component-scan base-package="cn.yue.flexDemo" />
<!--================================ 数据源 ================================== -->
<!-- PropertyPlaceholderConfigurer是一个Bean后处理器,它会读取属性文件信息,并将这


些信息设置成Spring配置文件的元数据。 -->
<bean



class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:dbconn.properties</value>
<!--如果有多个属性文件,依次在下面列出来 -->
<!--value>name.properties</value -->
</list>
</property>
</bean>


<!-- 定义数据源Bean,使用C3P0数据源实现 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="${jdbc.driverClassName}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!-- <property name="initialSize" value="${jdbc.initialSize}" /> <property 
name="maxActive" value="${jdbc.maxActive" /> <property 


name="maxIdle" value="${jdbc.maxIdle" 
/> <property name="minIdle" value="${jdbc.minIdle" /> -->
</bean>


<!-- 集成jpa -->
<!-- 实体管理工厂 -->
<bean id="entityManagerFactory"



class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="persistenceXmlLocation" value="classpath:META-


INF/persistence.xml" />
<property name="loadTimeWeaver">
<bean



class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
</property>
</bean>
<!-- 事务管理器 -->
<bean id="transactionManager" 


class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>






<!-- 配置事务增强处理 使用注解方式 -->
<tx:annotation-driven transaction-manager="transactionManager" />


</beans>




struts配置文件 struts.xml
修改web.xml文件,添加如下内容:
<!-- ssh集成flex -->


<!-- 设置spring配置文件路径 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>


<!-- 设置spring的热布署 <context-param> <param-name>contextClass</param-name> 
<param-


value>org.apache.struts2.spring.ClassReloadingXMLWebApplicationContext</param-value> 
</context-param> -->


<!-- 设置spring监听,在应用启动时启动spring容器 -->
<listener>
<listener-


class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>


<!-- 自定义struts2的核心拦截器 


org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter -->
<filter>
<filter-name>struts2</filter-name>
<filter-


class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<!-- 让Struts2的核心Filter拦截所有请求 -->
<filter-mapping>
<filter-name>struts2</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>
    
   <!-- 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>
<servlet-name>MessageBrokerServlet</servlet-name>
<url-pattern>/messagebroker/*</url-pattern>
    </servlet-mapping>


5 添加springFactory类和springFactoryInatance类,示例代码如下:


/**
 * Copyright (C) 2015
 * 
 * FileName:SpringFactoryInstance.java
 *
 * Author:<a href="mailto:zhenhuayue@sina.com">Retacn</a>
 *
 * CreateTime: Sep 1, 2015
 */
package cn.yue.flexDemo.factory;


import org.springframework.beans.BeansException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;


import flex.messaging.FactoryInstance;
import flex.messaging.FlexFactory;
import flex.messaging.config.ConfigMap;
import flex.messaging.services.ServiceException;


/**
 * flex的spring工厂
 * 
 * @version
 * 
 * @Description:
 * 
 * @author <a href="mailto:zhenhuayue@sina.com">Retacn</a>
 * 
 * @since Sep 1, 2015
 * 
 */
public class SpringFactoryInstance extends FactoryInstance {


/**
* 构造器

* @param factory
* @param id
* @param properties
*/
public SpringFactoryInstance(FlexFactory factory, String id, ConfigMap properties) 


{
super(factory, id, properties);
}


public Object lookup() {
ApplicationContext appContext = 


WebApplicationContextUtils.getWebApplicationContext


(flex.messaging.FlexContext.getServletConfig().getServletContext());
String beanName = getSource();
try {
return appContext.getBean(beanName);
} catch (NoSuchBeanDefinitionException nexc) {
ServiceException e = new ServiceException();
String msg = "Spring service named '" + beanName + "' does not 


exist.";
e.setMessage(msg);
e.setRootCause(nexc);
e.setDetails(msg);
e.setCode("Server.Processing");
throw e;
} catch (BeansException bexc) {
ServiceException e = new ServiceException();
String msg = "Unable to create Spring service named '" + beanName 


+ "' ";
e.setMessage(msg);
e.setRootCause(bexc);
e.setDetails(msg);
e.setCode("Server.Processing");
throw e;
}
}


public String toString() {
return "SpringFactory instance for id=" + getId() + " source=" + 


getSource() + " scope=" + getScope();
}


}








/**
 * Copyright (C) 2015
 * 
 * FileName:SpringFactory.java
 *
 * Author:<a href="mailto:zhenhuayue@sina.com">Retacn</a>
 *
 * CreateTime: Sep 1, 2015
 */
package cn.yue.flexDemo.factory;


import flex.messaging.FactoryInstance;
import flex.messaging.FlexFactory;
import flex.messaging.config.ConfigMap;


/**
 * springFactory
 * 
 * @version
 * 
 * @Description:
 * 
 * @author <a href="mailto:zhenhuayue@sina.com">Retacn</a>
 * 
 * @since Sep 1, 2015
 * 
 */
public class SpringFactory implements FlexFactory {


/**
* 初始化方法
*/
@Override
public void initialize(String id, ConfigMap configMap) {
}


/**
* 实例化方法
*/
@Override
public FactoryInstance createFactoryInstance(String id, ConfigMap properties) {
SpringFactoryInstance instance = new SpringFactoryInstance(this, id, 


properties);
instance.setSource(properties.getPropertyAsString(SOURCE, instance.getId


()));
System.out.println("Spring====" + instance.toString());
return instance;
}


@Override
public Object lookup(FactoryInstance instance) {
SpringFactoryInstance factoryInstance = (SpringFactoryInstance) instance;
return factoryInstance.lookup();
}
}




6 在service-config.xml中注册springFactory

<!-- 注册spring工厂类 -->
<factories>
<factory id="spring" class="cn.yue.flexDemo.factory.SpringFactory" />
</factories>


7 添加实体bean,示例代码如下:
package cn.yue.flexDemo.model;


import java.io.Serializable;


import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;


/**
 * 用户
 * 
 * @version
 * 
 * @Description:
 * 
 * @author <a href="mailto:zhenhuayue@sina.com">Retacn</a>
 * 
 * @since Aug 31, 2015
 * 
 */
@Entity
public class User implements Serializable {
private static final long serialVersionUID = -4431562144786459898L;
/** 标识 **/
private Integer id;
/** 用户名 **/
private String name;
/** 密码 **/
private String password;


/**
* 构造器

* @param name
*            用户名
* @param password
*            密码
*/
public User(String name, String password) {
this.name = name;
this.password = password;
}


@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Integer getId() {
return id;
}


public void setId(Integer id) {
this.id = id;
}


@Column(length = 20, nullable = false)
public String getName() {
return name;
}


public void setName(String name) {
this.name = name;
}


@Column(length = 20, nullable = false)
public String getPassword() {
return password;
}


public void setPassword(String password) {
this.password = password;
}


@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}


@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
User other = (User) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}


@Override
public String toString() {
return "User [id=" + id + ", name=" + name + "]";
}


}










8 添加业务方法
/**
 * Copyright (C) 2015
 * 
 * FileName:IUserService.java
 *
 * Author:<a href="mailto:zhenhuayue@sina.com">Retacn</a>
 *
 * CreateTime: Sep 1, 2015
 */
package cn.yue.flexDemo.service;


import cn.yue.flexDemo.model.User;


/**
 * 用户业务接口
 * 
 * @version
 * 
 * @Description:
 * 
 * @author <a href="mailto:zhenhuayue@sina.com">Retacn</a>
 * 
 * @since Sep 1, 2015
 * 
 */
public interface IUserService {


/**
* 添加用户

* @param user
*/
public void addUser(User user);


/**
* 删除用户

* @param id
*/
public void delUser(Integer id);


}




/**
 * Copyright (C) 2015
 * 
 * FileName:UserServiceBean.java
 *
 * Author:<a href="mailto:zhenhuayue@sina.com">Retacn</a>
 *
 * CreateTime: Sep 1, 2015
 */
package cn.yue.flexDemo.service.impl;


import cn.yue.flexDemo.model.User;
import cn.yue.flexDemo.service.IUserService;


/**
 * 用户业务实现
 * 
 * @version
 * 
 * @Description:
 * 
 * @author <a href="mailto:zhenhuayue@sina.com">Retacn</a>
 * 
 * @since Sep 1, 2015
 * 
 */
public class UserServiceBean implements IUserService {


/**
* 添加用户
*/
@Override
public void addUser(User user) {


}


/**
* 删除用户
*/
@Override
public void delUser(Integer id) {


}


}




9  测试业务方法
/**
 * Copyright (C) 2015
 * 
 * FileName:DbTest.java
 *
 * Author:<a href="mailto:zhenhuayue@sina.com">Retacn</a>
 *
 * CreateTime: Sep 1, 2015
 */
package cn.yue.flexDemo.test;


import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


import cn.yue.flexDemo.model.User;
import cn.yue.flexDemo.service.IUserService;


/**
 * 数据库测试方法
 * 
 * @version
 * 
 * @Description:
 * 
 * @author <a href="mailto:zhenhuayue@sina.com">Retacn</a>
 * 
 * @since Sep 1, 2015
 * 
 */
public class DbTest {
private static ApplicationContext ctx;
/** 用户业务类 **/
private static IUserService userService;


/**
* 测试前置方法
*/
@BeforeClass
public static void setUpBeforeClass() {
// 创建Spring容器
try {
ctx = new ClassPathXmlApplicationContext


("applicationContext.xml");
userService = (IUserService) ctx.getBean("userServiceBean");
} catch (BeansException e) {
e.printStackTrace();
}
}


/**
* 添加前台用户
*/
@Test
public void saveTest() {
for (int i = 0; i < 10; i++) {
User user = new User("retacn" + i, "123456" + i);


userService.addUser(user);
}
}
}




10 emoting-config.xml中配置flex客户端公开的bean


<!-- flex前台客户端所用来务bean -->
<destination id="userService">
<properties>
<factory>spring</factory>
<source>userServiceBean</source>
</properties>
</destination>


11 客户端程序


<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
  xmlns:s="library://ns.adobe.com/flex/spark" 
  


creationComplete="windowedapplication1_creationCompleteHandler(event)"
  xmlns:mx="library://ns.adobe.com/flex/mx">
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here-->
<mx:RemoteObject id="srv" destination="userService" 


endpoint="http://localhost:7001/flexDemo/messagebroker/amf" result="findAllFaultHandler


(event)">
<mx:method name="getAll"  />
</mx:RemoteObject>

</fx:Declarations>
<fx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.events.FlexEvent;
import mx.rpc.events.ResultEvent;
//应用对象
private function findAllFaultHandler(event:ResultEvent):void{

Alert.show(""+event.result);

}

protected function windowedapplication1_creationCompleteHandler


(event:FlexEvent):void
{
srv.getAll();
}

]]>
</fx:Script> 
<!---->


<mx:HDividedBox width="100%" height="100%">
<mx:Panel width="100%" height="100%" title="Inventory Management">
<mx:AdvancedDataGrid id="list" width="100%" height="100%">
<mx:columns>
<mx:AdvancedDataGridColumn dataField="name" 


headerText="Name"/>
<mx:AdvancedDataGridColumn dataField="password" 


headerText="password"/>
</mx:columns>
</mx:AdvancedDataGrid>
</mx:Panel>
</mx:HDividedBox>
</s:WindowedApplication>

posted @ 2015-09-06 12:55  retacn_yue  阅读(134)  评论(0编辑  收藏  举报