而不是arrayCollection

时间:2011-05-11 06:49:51

标签: java flex flex4 blazeds

我正在尝试使用BlazeDs将java中的值返回到Flex前端。成功时blazeds正在连接,同时检索它显示的值[object Asynvtoken]没有从java方法获取值。

我的灵活代码是:

<fx:Script>
        <![CDATA[
            import mx.collections.ArrayCollection;
            import mx.controls.Alert;
            import mx.rpc.AsyncToken;
            import mx.rpc.events.FaultEvent;
            import mx.rpc.events.ResultEvent;

            public function get_user():void
            {
                var token:AsyncToken = ro.getOperation('getUser').send();
                user_grid.dataProvider = token.toString();
                Alert.show(user_grid.dataProvider.toString());
            }
            private function fault(e:FaultEvent):void
            {
                Alert.show("code:\n" + e.fault.faultCode  + "\n\nMessage:\n" + e.fault.faultString + "\n\nDetail:\n" + e.fault.faultDetail);
            }
            private function result(e:ResultEvent):void
            {
                userbtn.visible = false;
                lnkbtn.visible = true;
            }
        ]]>
    </fx:Script>

    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
        <s:RemoteObject id="ro"
                        source="com.freelancer.GetUser"
                        fault="fault(event)"
                        destination="BlazeDsService">
            <s:method name="getUser"
                      result="result(event)"/>
        </s:RemoteObject>
    </fx:Declarations>

        <mx:DataGrid id="user_grid" x="-1" y="-1" width="705" height="356">
            <mx:columns>
                <mx:DataGridColumn headerText="Id" dataField="id"/>
                <mx:DataGridColumn headerText="Name" dataField="name"/>
                <mx:DataGridColumn headerText="Password" dataField="pwd"/>
            </mx:columns>
        </mx:DataGrid>      

    <s:Button x="322" y="429" id="userbtn" label="Get User" width="98" 
              height="23" cornerRadius="10" click="get_user()"/>
    <mx:LinkButton x="331" y="429" id="lnkbtn" visible="false" label="Connected Please Wait Loading . . ."/>

我的java代码是:

public class GetUser
{
    public static void main(String[] argv) 
    {
        System.out.println("-------- PostgreSQL " +
                "JDBC Connection Testing ----------");
        getUser();
    }
    public static List<User> getUser()
    {
         List<User> ls=new ArrayList<User>();   
         String host = "test";
            String port = "1234";
            String dbName = "test";
        Connection connection = null;
        try 
        {
            connection = DriverManager.getConnection(
                    "jdbc:postgresql://" + host + ":" + port + "/" + dbName,"user", "user");

            System.out.println("Database is connected");    

         String strSQL = "select * from test";
         Statement st = connection.createStatement();
         ResultSet rs=st.executeQuery(strSQL);
         System.out.println("hi,query executed");
         while(rs.next())
         {
             User user = new User();
             user.setId(rs.getInt("id"));
             user.setName(rs.getString("name"));
             user.setPwd(rs.getString("pwd"));
             ls.add(user);
         }
        }
        catch(Exception e)
        {
            System.out.println();
        }
        finally
        {
            try
            {
                connection.close();
            }
            catch (Exception ignored) 
            {

            }
        }
         return ls;
    }
}

如果有人知道解决方案,请帮助我。

1 个答案:

答案 0 :(得分:1)

请记住,对RPC服务的调用是异步的。

首先,您调用远程功能getUser。此请求将发送到服务器,getOperation函数立即返回。此调用的结果是AsyncToken对象,它不是getUser调用的结果。令牌是Asynchronous Completion Token design pattern的一部分。 RPC函数返回后,客户端将在结果处理程序中获取响应,该处理程序是您声明的result函数。结果是ResultEvent参数的一部分。

所以以下内容应该有效:

    public function get_user():void
    {
        var token:AsyncToken = ro.getOperation('getUser').send();
    }

    private function result(e:ResultEvent):void
    {
        user_grid.dataProvider = e.result;     
        userbtn.visible = false;
        lnkbtn.visible = true;
    }