FLex3基于WebService跟java通信(完整篇)

///////首先创建一个flex3的工程,代码如下/////////
<?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.ResultEvent;
import mx.controls.Alert;
[Bindable]
public var xml:XML=new XML();
public function testResult(e:ResultEvent):void
{
Alert.show("流程2");
xml=XML(e.result);
if(xml.toString().length>0)
{
Alert.show("流程一");
this.showValue.text=this.xml.toString();
}
}
public function hello():void
{
this.myWebService.example(this.aa.text);
Alert.show("流程三");
}
]]>
</mx:Script>
<mx:WebService id="myWebService"
wsdl="http://localhost:8080/flexWebService/services/test?wsdl"
useProxy="false"
fault="Alert.show(event.fault.toString())"
result="testResult(event)">
<mx:operation name="example"/>
</mx:WebService>
<mx:Button x="208" y="187" label="Button" width="85" click="hello();"/>
<mx:Label x="208" y="150" text="Label" id="showValue"/>
<mx:TextInput x="173" y="107" id="aa" />
</mx:Application>
/////////创建一个java的WebService工程(flexWebService)创建webservice类Test类将其
请求url设置为test,使用默认生成的example方法测试,部署到tomcat服务器,启动服务器,输入http://localhost:8080/flexWebService/services看是否发现test的wsdl。

/////////////////java代码////////////////////////////
package com.bus;
public interface Itest {
public String example(String message);
}
package com.bus;
public class TestImpl implements Itest {
public String example(String message) {
return "前台传输的信息为"+message;
}
}
////////////xml文件/////////
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <servlet>
    <servlet-name>XFireServlet</servlet-name>
    <servlet-class>org.codehaus.xfire.transport.http.XFireConfigurableServlet</servlet-class>
    <load-on-startup>0</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>XFireServlet</servlet-name>
    <url-pattern>/services/*</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>
///////////////////services.xml/////////////////
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xfire.codehaus.org/config/1.0">

<service>
<name>test</name>
<serviceClass>com.bus.Itest</serviceClass>
<implementationClass>com.bus.TestImpl</implementationClass>
<style>wrapped</style>
<use>literal</use>
<scope>application</scope>
</service>
</beans>

你可能感兴趣的:(java,tomcat,xml,webservice,Flex)