本人第一次发表文章,如有不妥,请指正。
进入正题,spring+flex网上方法很多,但用注解的文章很少,现用注解的方式配置一个简单的实例。
所用的开发环境eclipse3.5+flex4+spring3+blazeds(4.0.1.17657)
ps:Blazeds版本最低为4.0.1.17657+
因为用Blazeds所以SPRING就不必专门去下载了。先创建个基本FLEX+java的项目,相信大家一定多会。不会的去网上找下,一大推。主要是在让我们选择 blazeds war文件时,我们选择blazeds-spring.war(版本4.0.1.17657+)这样就自动把所有的JAR包多放到lib包下了包括SPRING的。
首先配置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/infrastructure-config.xml,
/WEB-INF/spring/security-config.xml,
/WEB-INF/spring/app-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>
<!-- begin rds
<servlet>
<servlet-name>RDSDispatchServlet</servlet-name>
<display-name>RDSDispatchServlet</display-name>
<servlet-class>flex.rds.server.servlet.FrontEndServlet</servlet-class>
<init-param>
<param-name>useAppserverSecurity</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>messageBrokerId</param-name>
<param-value>_messageBroker</param-value>
</init-param>
<load-on-startup>10</load-on-startup>
</servlet>
<servlet-mapping id="RDS_DISPATCH_MAPPING">
<servlet-name>RDSDispatchServlet</servlet-name>
<url-pattern>/CFIDE/main/ide.cfm</url-pattern>
</servlet-mapping>
end rds -->
<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>
<!-- for WebSphere deployment, please uncomment -->
<!--
<resource-ref>
<description>Flex Messaging WorkManager</description>
<res-ref-name>wm/MessagingWorkManager</res-ref-name>
<res-type>com.ibm.websphere.asynchbeans.WorkManager</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
-->
</web-app>
接着创建FLEX APP命名随便,我现在命名为index.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">
<s:layout>
<s:BasicLayout/>
</s:layout>
<fx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.rpc.events.ResultEvent;
protected function button1_clickHandler(event:MouseEvent):void
{
service.firstApp("Flex");
}
protected function remote_resultHandler(event:ResultEvent):void
{
Alert.show(String(event.result));
}
]]>
</fx:Script>
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
<s:RemoteObject id="service" destination="myService" result="remote_resultHandler(event)" endpoint="messagebroker/amf"/>
</fx:Declarations>
<s:Button x="72" y="101" label="按钮" click="button1_clickHandler(event)"/>
</s:Application>
接着是JAVA端程序
MyService.java
package com.test.service;
import org.springframework.flex.remoting.RemotingDestination;
import org.springframework.flex.remoting.RemotingInclude;
import org.springframework.stereotype.Service;
@Service("myService")
@RemotingDestination(channels = { "my-amf", "my-secure-amf" })
public class MyService {
@RemotingInclude
public String firstApp(String info){
System.out.println(info);//从FLEX端获取的信息
return "Hello World "+info;//返回给FLEX
}
}
然后我们在WebContent/WEB-INF/spring下,修改app-config.xml SPRING配置文件。该文件是在选择BLAZEDS-SPRING.WAR后自动生成的。
修改后的 app-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:security="http://www.springframework.org/schema/security"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
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://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.test.service" />
<context:annotation-config />
</beans>
好了,启动TOMCAT,然后输入地址,我的是
http://localhost:9090/oa/index.html
应该前后台多有东西输出了吧!祝大家好运!