flex blazeDs spring整合



        1.新建一个工程Flex_Spring。Application server type选择j2ee.选择下一步:Target runtime:选择tomcat,flex War file:选择你下载的blaze包,blazeds.war包。然后finish就OK啦,这样就创建了一个flex的j2ee工程。

        2.在web.xml文件中添加如下代码:
<context-param> 
                 <param-name>contextConfigLocation</param-name> 
                 <param-value>/WEB-INF/applicationContext.xml</param-value> 
  </context-param>
<servlet>
   <servlet-name>SpringContextServlet</servlet-name>
   <servlet-class>
    org.springframework.web.context.ContextLoaderServlet
   </servlet-class>
   <load-on-startup>1</load-on-startup>
</servlet>
<servlet>
   <servlet-name>SpringLog4jConfigServlet</servlet-name>
   <servlet-class>
    org.springframework.web.util.Log4jConfigServlet
   </servlet-class>
</servlet>
<servlet>
   <servlet-name>web</servlet-name>
   <servlet-class>
    org.springframework.web.servlet.DispatcherServlet
   </servlet-class>
</servlet>

3.在src下新建一个包:com.hp.common.并将SrpingFactory类(官方网站提供的)放到该包中。并在services-config.xml中声明该类
<factories>
  <factory id="spring" class="com.hp.common.SpringFactory" />
 </factories>

4.添加Spring的配置文件applicationCOntext.xml.可以到spring的例子文件中之间拷贝一个,然后将没有用的删除就可以了
<bean id="helloWorld" class="com.springflex.service">

5。在src新建一个类HelloService.java:
package com.springflex.service;

public class HelloService {
 public String sayHello(String username){
  return "Hello"+username;
 }

}

然后再remoting-config.xml文件中将此类声明到前台:
<destination id="helloWorld">  
        <properties> 
            <factory>spring</factory>
            <source>helloWorld</source>  
        </properties>  
    </destination> 

注意:这里的spring是在services-config.xml中声明的那个SpringFactory,

  6.后端配置基本结束,前端编写页面显示的内容:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
  <mx:Script>
   <![CDATA[
    import mx.rpc.events.FaultEvent;   
       import mx.controls.Alert;   
       import mx.rpc.events.ResultEvent; 
       
       [Bindable]  
       private var user:String;
       
       private function ClickHandler():void{
        remoteObject.getHello(textInput.text);
       }
         
       private function resultHandler(event:ResultEvent):void {
        user=event.result as String;    
       }   
         
       private function faultHandler(event:FaultEvent):void {   
           Alert.show(event.fault.toString(), "失败");   
       }   
   ]]>
  </mx:Script>
     
   <!-- destination 属性值要和 remoting-config.xml 中 destination 的 id 一致-->  
   <mx:RemoteObject id="remoteObject"  
                     destination="helloWorld"  
                    result="resultHandler(event)"  
                    fault="faultHandler(event)"/>  
     
   <mx:Button label="发送消息" click="ClickHandler()" x="256.5" y="197"/>  
   <mx:Text x="391" y="199" width="129" id="username" text="{user}"/>
   <mx:TextInput x="52" y="197" id="textInput"/>

</mx:Application>

你可能感兴趣的:(spring,xml,Web,Flex,HP)