Flex与Servlet通讯(中);

类别:Java 点击:0 评论:0 推荐:

在上一篇文章中,我使用<mx:model>与servlet通讯,但是网上的例子大部分用<mx:httpservice>。但是我用以下方式的时候,就出现错误了.
<mx:HTTPService id="ServletService1" url="servlet/XMLFacadeServlet"   >
        <mx:request/>
               </mx:HTTPService>

于是我就查看web.xml,改动了好几处,还是出现错误,看看自带的sample,代码还是差不多一样了阿。
试了好久,也没弄好。于是就搜索包含文z字employees.jsp(sample的url是这个),发现这个文件flex-config.xml
,知道就是他了,打开,有这么一段

    <http-service-proxy>
    ..........................
<whitelist>
            <!-- whitelist config for unnamed services -->
            <unnamed>
              <url>http://{localserver}/*</url>
                <url>https://{localserver}/*</url
..............................................
</unnamed>

            <!-- whitelist config for named services -->
            <named>
....................
<service name="SampleEmployeeSrv">
                    <url>{context.root}/explorer/data/employees.jsp</url>
                    <use-custom-authentication>true</use-custom-authentication>
                </service>
................................................
再看在flex目录下的 flex-config.xml文件
<whitelist>
            <!-- whitelist config for unnamed services -->
            <unnamed>
                <url></url>
                <!--
                For security, the whitelist is locked down by default.
                Uncomment the first two urls below to enable access to all URLs,
                or the last two urls to enable access to the local server,
                or add above the individual URLs you wish to access.
                <url>http://*</url>
                <url>https://*</url>
                <url>http://{localserver}/*</url>
                <url>https://{localserver}/*</url>
                -->
            </unnamed>
            <!-- whitelist config for named http services -->
            <!-- <named> -->
                <!-- define an http service which may be referenced by name from mxml -->
                <!-- <service name="service"> -->
                    <!-- enables use of custom fault code on the client for handling authentication failures -->
                    <!-- <use-custom-authentication>false</use-custom-authentication> -->
                    <!-- actual url to use when accessing the named http service -->
                    <!-- <url>http://localhost:8100/flex/servicename</url> -->
                    <!-- user-name and password to use when accessing this http service -->
                    <!-- <run-as user="user" password="pwd"/> -->
                    <!-- Adds the service's url to the unnamed whitelist.  If false, these can never be used unnamed -->
                    <!-- This should be set to false if using web application security with this named service -->
                    <!-- <allow-unnamed-access>true</allow-unnamed-access> -->
                <!-- </service> -->
            <!-- </named> -->
                          </whitelist>
于是,我把
<url>http://*</url>
                <url>https://*</url>
                <url>http://{localserver}/*</url>
                <url>https://{localserver}/*</url>
这段的注释符去掉就可以了。
 还有一种访问servlet的方法就是通过named来的,如在上面<named>...</named>加上

                <service name="ServletService">
                    <url>{context.root}/servlet/XMLFacadeServlet</url>
                    <use-custom-authentication>true</use-custom-authentication>
                </service>
mxml这样使用:
<mx:HTTPService id="ServletService1" serviceName="ServletService">

mx:HTTPService 需要一个send操作才能执行发送GET或POST命令以获得数据,如:
        <mx:Button label="Get Employee List" click="employeeSrv.send();"/>
修改flex-config.xml后要重起tomcat的,如果你修改后flex-config.xml存在问题(如:把named写成name),
访问mxml的时候就会出现 url is not available 这个错误了。

 

本文地址:http://com.8s8s.com/it/it13233.htm