flex4 整合java ee 练习——HelloWorld

一、修改 web.xml文件,添加如下代码

<display-name>BlazeDS</display-name>
    <description>BlazeDS Application</description>

    <!-- 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>
        <display-name>MessageBrokerServlet</display-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>
    
<!-- 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>        
        <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>MessageBrokerServlet</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-INF下面,如图:附件

 

三、新建java类:HelloWorld.java

package hello;
public class HelloWorld {

    public HelloWorld(){}
   
    public String getHelloWorld(String str){
        System.out.println("Hello World!  "+str);
        return "Hello  "+str;
    }
}
 

 

 

四、修改remote-config.xml文件,添加内容如下:

<destination id="helloWorld">
        <properties>
            <source>hello.HelloWorld</source>
        </properties>
</destination>

 

五、新建MXML应用程序 TestHelloWorld.xml

<?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:Declarations>
		<!-- 将非可视元素(例如服务、值对象)放在此处 -->
		<mx:RemoteObject id="remoteObject" destination="helloWorld" 
						 result="resultHandler(event)" fault="faultHandler(event)">
		</mx:RemoteObject>
	</fx:Declarations>
	<fx:Script>
		<![CDATA[
			import mx.controls.Alert;
			import mx.rpc.events.FaultEvent;
			import mx.rpc.events.ResultEvent;
			private function resultHandler(event:ResultEvent):void{
				Alert.show(event.result.toString(),"提示");
			}
			
			private function faultHandler(event:FaultEvent):void{
				Alert.show(event.fault.toString(),"提示");
			}
			
			private function sendStr():void{
				remoteObject.getHelloWorld(str.text);
				//Alert.show(str.text);
			}
		]]>
	</fx:Script>
	<s:Label id="Label" text="请输入字符串" x="195" y="197"/>
	<s:TextInput id="str"  x="279" y="188"/>
	<s:Button label="发送" click="sendStr()" x="435" y="189"/>
</s:Application>

你可能感兴趣的:(flex4)