手把手教你使用flex eclipse整合spring

最先下载FlashBuilder_4_7_LS10_win64.exe试了几个eclipse安装插件都没成功,包括myeclipse8.5、spring sts2.9.2、eclipse3.5、j2eeeclipse版本4.2.0,后来搞了一个FlashBuilder_4_LS10.exe安装完找不到插件安装文件原来这个是单独版,必须插件版才行,最后下载FlashBuilder_4_Plugin_LS10.exe终于配置成功了,myeclipse8.5不行,spring sts可以了。

spring sts部署应用跟myeclipse不一样,比较类似eclipse。

用sts整合flex和java有几个步骤:

1:新建动态web工程flexweb,创建web.xml

2: blazeds-turnkey-4.0.0.14931.zip解压, 复制blazed两个文件夹flex和lib到WEB-INF下,里面是blaze的jar包和flex配置文件,然后修改web.xml加入blaze支持

<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>

3:项目右键,添加/更改项目类型>添加flex类型项目,第一步,应用程序类型选择J2EE,下方选择BlazeDS,第二部根文件夹填入项目在workspase的路径加一个WebContent,如E:\workspaces\sts\flexweb\WebContent,根URL填http://localhost:8080/flexweb,上下文根目录/flexweb,输出文件夹使用默认E:\workspaces\sts\flexweb\WebContent\flexweb-debug,点击finish
4:转换完成后,目录有些变化,右键项目>properties>flex构建路径,主源文件夹改为flex_src,然后把自动生成的src目录下的flexweb.mxml移动到flex_src下,环境搭建就算完成了

HelloWorld

flexweb.mxml:

<?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">  

 <fx:Script>
  <![CDATA[
   import mx.controls.Alert;
   import mx.rpc.events.ResultEvent;  

   protected function myFlex_resultHandler(event:ResultEvent):void{
    var name:String=event.result as String;
    Alert.show(name);
   }
   protected function button1_clickHandler(event:MouseEvent):void
   {
    myFlex.sayHello(txtName.text);
   }
  ]]>
 </fx:Script>  

 <fx:Declarations>
  <!-- 将非可视元素(例如服务、值对象)放在此处 -->
  <s:RemoteObject id="myFlex" destination="mytest" result="myFlex_resultHandler(event)"/>
 </fx:Declarations>
 <s:Button x="209" y="135" label="按钮" click="button1_clickHandler(event)"/>
 <s:TextInput x="166" y="81" id="txtName"/>
 <s:Label x="10" y="81" text="请输入内容:" fontSize="15" fontWeight="bold" fontFamily="中易黑体"/>
</s:Application>

其中

<s:RemoteObject id="myFlex" destination="mytest" result="myFlex_resultHandler(event)"/>

指定了一个调用Java的类Hello,mytest对应到remoting-config.xml

在WEB-INFO/flex目录下remoting-config.xml加入

<destination id="mytest">
    <properties>
      <source>com.hongbo.Hello</source>
    </properties>
  </destination>

result对应的是java方法调用的回调函数
建一个普通java类

package com.hongbo;

public class Hello {

 public String sayHello(String name){
  System.out.println("------------------------------------");
  return "Hello First Demo " + name;
 }
}

这样就OK了

访问路径是http://localhost:8080/flexweb/flexweb-debug/flexweb.html

第一次会报404,problems提示无法创建html包装器,右键点击重新创建模板

添加Spring支持

1:web.xml加入

<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/classes/applicationContext.xml </param-value>
 </context-param>
 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>

2:src下创建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: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-2.0.xsd
 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">

 <bean id="hello" class="com.hongbo.Hello">
  <property name="testSpring">
   <ref bean="testSpring"/>
  </property>
 </bean>
 <bean id="testSpring" class="com.hongbo.test.impl.TestSpringImpl"/>
</beans>

3:WEB-INF/flex/service-config.xml加入

<factories>
   <factory id="spring" class="com.hongbo.SpringFactory" />
 </factories>

添加java类

package com.hongbo;

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;
public class SpringFactory implements FlexFactory {
 private static final String SOURCE = "source";

 public void initialize(String id, ConfigMap configMap) {
 }

 public FactoryInstance createFactoryInstance(String id, ConfigMap properties) {
  SpringFactoryInstance instance = new SpringFactoryInstance(this, id,
    properties);
  instance.setSource(properties.getPropertyAsString(SOURCE, instance
    .getId()));
  return instance;
 } // end method createFactoryInstance()  

 public Object lookup(FactoryInstance inst) {
  SpringFactoryInstance factoryInstance = (SpringFactoryInstance) inst;
  return factoryInstance.lookup();
 }

 static class SpringFactoryInstance extends FactoryInstance {
  SpringFactoryInstance(SpringFactory factory, String id,
    ConfigMap properties) {
   super(factory, id, properties);
  }

  public String toString() {
   return "SpringFactory instance for id=" + getId() + " source="
     + getSource() + " scope=" + getScope();
  }

  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;
   }
  }

 }

}

4:修改remoting-config.xml

<destination id="mytest">
    <properties>
     <factory>spring</factory>
      <source>hello</source>
    </properties>
  </destination>

5:修改相应的Java类

package com.hongbo;

import com.hongbo.test.TestSpring;

public class Hello {

 private TestSpring testSpring;

 public void setTestSpring(TestSpring testSpring) {
  this.testSpring = testSpring;
 }

 public String sayHello(String name){
  return testSpring.testSpring(name);
 }
}
package com.hongbo.test;

public interface TestSpring {

 String testSpring(String name);
}
package com.hongbo.test.impl;

import com.hongbo.test.TestSpring;

public class TestSpringImpl implements TestSpring{

 public String testSpring(String name){
  System.out.println("test spring-------------------------------------"+name);
  return "test spring "+name;
 }
}

最后,flex打印语句trace不会打印到控制台,要先卸载flashplayer再安装一个debuger版的flashplayer,下载flashplayer_uninstall.zip,卸载,下载flashplayer10r12_36_winax_debug.exe,安装,卸载安装后好像谷歌浏览器没影响,然后eclipse修改默认浏览器为IE,window>preferences>General>Web browser,选择Internet Explorer,最后还有,启动tomcat后,必须在mxml上面右键debug运行,打开的IE才会打印trace,直接访问网址是不行的。
如有遗漏请指出

(0)

相关推荐

  • spring aop两种配置方式

    第一种:注解配置AOP 注解配置AOP(使用 AspectJ 类库实现的),大致分为三步: 1. 使用注解@Aspect来定义一个切面,在切面中定义切入点(@Pointcut),通知类型(@Before, @AfterReturning,@After,@AfterThrowing,@Around). 2. 开发需要被拦截的类. 3. 将切面配置到xml中,当然,我们也可以使用自动扫描Bean的方式.这样的话,那就交由Spring AoP容器管理. 另外需要引用 aspectJ 的 jar 包:

  • SpringMVC+MyBatis声明式事务管理

    采用的基本搭建环境:SpringMVC.MyBatis.MySQL.tomcat Spring事务管理分解了传统的全局事务管理和本地事务管理的劣势,使得在任何环境中都可以使用统一的事务管理模型,你可以写一次代码,然后在不同的环境从你的代码里面配置不同的事务管理策略,Spring提供两种事务管理策略:一种是声明式事务管理策略,另一种是编程式事务管理策略,这里主要介绍声明式事务管理策略 由于采用的是SpringMVC. MyBatis,故统一采用了标注来声明Service.Controller 由于

  • Spring MVC 框架搭建配置方法及详解

    现在主流的Web MVC框架除了Struts这个主力 外,其次就是Spring MVC了,因此这也是作为一名程序员需要掌握的主流框架,框架选择多了,应对多变的需求和业务时,可实行的方案自然就多了.不过要想灵活运用Spring MVC来应对大多数的Web开发,就必须要掌握它的配置及原理. 一.Spring MVC环境搭建:(Spring 2.5.6 + Hibernate 3.2.0) 1. jar包引入 Spring 2.5.6:spring.jar.spring-webmvc.jar.comm

  • 解决springmvc+mybatis+mysql中文乱码问题

    近日使用ajax请求springmvc后台查询mysql数据库,页面显示中文出现乱码 最初在mybatis配置如下 <select id="queryContentById" resultType = "java.lang.String" parameterType="String" > select text from News where id=#{o} </select> 其中表News的text字段为blob类型

  • java Spring整合Freemarker的详细步骤

    我的開發環境框架:springmvc開發工具:springsource-tool-suite-2.9.0版本:1.6.0_29tomcat版本:apache-tomcat-7.0.26前言:FreeMarker是一个用Java语言编写的模板引擎,它基于模板来生成文本输出.FreeMarker与Web容器无关,即在Web运行时,它并不知道Servlet或HTTP.它不仅可以用作表现层的实现技术,而且还可以用于生成XML,JSP或Java 等.簡而言之,Freemarker就是在Jave Web開發

  • 模拟Spring的简单实现

    项目结构如下: 如何思考?面向抽象编程? 先来看看一个序列图 从以上看出了什么?初学的,能看得出是什么才怪,那不管它了.看看具体实现吧 首先要建立一个实体类:User,放在model包下 package com.wwj.model; /** * 实体类 * @author wwj * Spring */ public class User { private String username; private String password; public String getUsername()

  • SpringMVC文件上传 多文件上传实例

    必须明确告诉DispatcherServlet如何处理MultipartRequest.SpringMVC中提供了文件上传使用方式如下配置xxx-servlet.xml,添加如下代码: 复制代码 代码如下: <bean id="multipartResolver"  class="org.springframework.web.multipart.commons.CommonsMultipartResolver">          <!-- 设置

  • Spring中的事务管理实例详解

    本文实例讲述了Spring中的事务管理.分享给大家供大家参考.具体分析如下: 事务简介: 事务管理是企业级应用程序开发中必不可少的技术,用来确保数据的完整性和一致性 事务就是一系列的动作,它们被当作一个单独的工作单元.这些动作要么全部完成,要么全部不起作用 事务的四个关键属性(ACID) ① 原子性(atomicity):事务室一个原子操作,有一系列动作组成.事务的原子性确保动作要么全部完成,要么完全不起作用 ② 一致性(consistency):一旦所有事务动作完成,事务就被提交.数据和资源就

  • spring快速入门实例教程

    本文实例讲述了spring的基本配置与控制反转.分享给大家供大家参考.具体如下: 这里我们采用了maven构建java项目,没有采用maven的同样适用. 1. 创建maven项目,我创建的项目名称为springdemo. 2. 添加依赖包,我是通过maven添加的包,maven相关配置如下: 复制代码 代码如下: <dependency>     <groupId>org.springframework</groupId>     <artifactId>

  • 读取spring配置文件的方法(spring读取资源文件)

    1.spring配置文件 复制代码 代码如下: <bean id="configproperties"          class="org.springframework.beans.factory.config.PropertiesFactoryBean">          <property name="location" value="classpath:jdbc.properties"/>

  • Spring MVC中上传文件实例

    SpringMVC(注解)上传文件需要注意的几个地方: 1.form的enctype="multipart/form-data",这个是上传文件必须的 2.applicationContext.xml配置: 复制代码 代码如下: <!-- SpringMVC上传文件时,需要配置MultipartResolver处理器 --> <bean id="multipartResolver" class="org.springframework.w

  • Spring常用注解汇总

    本文汇总了Spring的常用注解,以方便大家查询和使用,具体如下: 使用注解之前要开启自动扫描功能 其中base-package为需要扫描的包(含子包). <context:component-scan base-package="cn.test"/> @Configuration把一个类作为一个IoC容器,它的某个方法头上如果注册了@Bean,就会作为这个Spring容器中的Bean. @Scope注解 作用域 @Lazy(true) 表示延迟初始化 @Service用于

随机推荐