Spring+flex(Blazeds)註解方式創建項目

Spring+flex(Blazeds)註解方式創建項目

轉自:http://szydnjw.javaeye.com/blog/852134

本人第一次發表文章,如有不妥,請指正。
   進入正題,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
Java代碼 複製代碼
  1. <?xml version="1.0" encoding="UTF-8"?>   
  2. <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">   
  3. <web-app>   
  4.   
  5.     <display-name>BlazeDS Spring Integration Application</display-name>   
  6.     <description>BlazeDS Spring Integration Application</description>   
  7.   
  8.     <context-param>   
  9.         <param-name>contextConfigLocation</param-name>   
  10.         <param-value>   
  11.             /WEB-INF/spring/infrastructure-config.xml,    
  12.             /WEB-INF/spring/security-config.xml,   
  13.             /WEB-INF/spring/app-config.xml   
  14.         </param-value>   
  15.     </context-param>   
  16.        
  17.     <filter>   
  18.         <filter-name>springSecurityFilterChain</filter-name>   
  19.         <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>   
  20.     </filter>   
  21.   
  22.     <filter-mapping>   
  23.       <filter-name>springSecurityFilterChain</filter-name>   
  24.       <url-pattern>/*</url-pattern>   
  25.     </filter-mapping>   
  26.   
  27.     <!-- Http Flex Session attribute and binding listener support -->   
  28.     <listener>   
  29.         <listener-class>flex.messaging.HttpFlexSession</listener-class>   
  30.     </listener>   
  31.   
  32.     <listener>   
  33.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>   
  34.     </listener>   
  35.   
  36.     <!-- Spring Dispatcher Servlet -->      
  37.     <servlet>   
  38.         <servlet-name>flex</servlet-name>   
  39.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>   
  40.         <load-on-startup>1</load-on-startup>   
  41.     </servlet>   
  42.   
  43. <!-- begin rds   
  44.     <servlet>   
  45.         <servlet-name>RDSDispatchServlet</servlet-name>   
  46.         <display-name>RDSDispatchServlet</display-name>   
  47.         <servlet-class>flex.rds.server.servlet.FrontEndServlet</servlet-class>   
  48.         <init-param>   
  49.             <param-name>useAppserverSecurity</param-name>   
  50.             <param-value>true</param-value>   
  51.         </init-param>   
  52.         <init-param>   
  53.             <param-name>messageBrokerId</param-name>   
  54.             <param-value>_messageBroker</param-value>   
  55.         </init-param>   
  56.         <load-on-startup>10</load-on-startup>   
  57.     </servlet>   
  58.   
  59.     <servlet-mapping id="RDS_DISPATCH_MAPPING">   
  60.         <servlet-name>RDSDispatchServlet</servlet-name>   
  61.         <url-pattern>/CFIDE/main/ide.cfm</url-pattern>   
  62.     </servlet-mapping>   
  63. end rds -->   
  64.   
  65.     <servlet-mapping>   
  66.         <servlet-name>flex</servlet-name>   
  67.         <url-pattern>/messagebroker/*</url-pattern>   
  68.      </servlet-mapping>   
  69.   
  70.     <welcome-file-list>   
  71.         <welcome-file>index.html</welcome-file>   
  72.         <welcome-file>index.htm</welcome-file>   
  73.     </welcome-file-list>   
  74.   
  75.     <!-- for WebSphere deployment, please uncomment -->   
  76.     <!--   
  77.     <resource-ref>   
  78.         <description>Flex Messaging WorkManager</description>   
  79.         <res-ref-name>wm/MessagingWorkManager</res-ref-name>   
  80.         <res-type>com.ibm.websphere.asynchbeans.WorkManager</res-type>   
  81.         <res-auth>Container</res-auth>   
  82.         <res-sharing-scope>Shareable</res-sharing-scope>   
  83.     </resource-ref>   
  84.     -->   
  85.   
  86. </web-app>  


    接着創建FLEX APP命名隨便,我現在命名爲index.mxml,代碼如下
Java代碼 複製代碼
  1. <?xml version="1.0" encoding="utf-8"?>   
  2. <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"    
  3.                xmlns:s="library://ns.adobe.com/flex/spark"    
  4.                xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">   
  5.     <s:layout>   
  6.         <s:BasicLayout/>   
  7.     </s:layout>   
  8.   
  9.   
  10.   
  11.   
  12.     <fx:Script>   
  13.         <![CDATA[   
  14.             import mx.controls.Alert;   
  15.             import mx.rpc.events.ResultEvent;   
  16.                
  17.             protected function button1_clickHandler(event:MouseEvent):void  
  18.             {   
  19.                 service.firstApp("Flex");   
  20.             }   
  21.             protected function remote_resultHandler(event:ResultEvent):void  
  22.             {   
  23.                 Alert.show(String(event.result));   
  24.             }   
  25.         ]]>   
  26.     </fx:Script>   
  27.   
  28.     <fx:Declarations>   
  29.         <!-- 將非可視元素(例如服務、值對象)放在此處 -->   
  30.         <s:RemoteObject id="service" destination="myService" result="remote_resultHandler(event)" endpoint="messagebroker/amf"/>   
  31.     </fx:Declarations>   
  32.     <s:Button x="72" y="101" label="按鈕" click="button1_clickHandler(event)"/>   
  33. </s:Application>  

   接着是JAVA端程序
   MyService.java
Java代碼 複製代碼
  1. package com.test.service;   
  2.   
  3. import org.springframework.flex.remoting.RemotingDestination;   
  4. import org.springframework.flex.remoting.RemotingInclude;   
  5. import org.springframework.stereotype.Service;   
  6.   
  7.   
  8. @Service("myService")   
  9. @RemotingDestination(channels = { "my-amf""my-secure-amf" })   
  10. public class MyService {   
  11.        
  12.     @RemotingInclude  
  13.     public String firstApp(String info){   
  14.            
  15.         System.out.println(info);//從FLEX端獲取的信息   
  16.            
  17.         return "Hello World "+info;//返回給FLEX   
  18.            
  19.     }   
  20.   
  21. }  

   然後我們在WebContent/WEB-INF/spring下,修改app-config.xml SPRING配置文件。該文件是在選擇BLAZEDS-SPRING.WAR後自動生成的。
   修改後的 app-config.xml
  
Java代碼 複製代碼
  1. <?xml version="1.0" encoding="UTF-8"?>   
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:security="http://www.springframework.org/schema/security"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"  
  5.     xsi:schemaLocation="   
  6.         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   
  7.         http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.xsd   
  8.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">   
  9.   
  10.     <context:component-scan base-package="com.test.service" />   
  11.     <context:annotation-config />   
  12. </beans>  

    好了,啓動TOMCAT,然後輸入地址,我的是
  http://localhost:9090/oa/index.html
  應該前後臺多有東西輸出了吧!祝大家好運!
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章