我正在尝试使用BlazeDs将值从java返回到flex前端.成功blazeds正在连接,同时检索它显示的值[对象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 + "nnMessage:n" + e.fault.faultString + "nnDetail: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;
}
}
如果有人知道解决方案,请帮助我。
,对 RPC 服务的调用是异步的。
首先,您将远程函数称为 getUser
。此请求将发送到服务器,getOperation
函数会立即返回。此调用的结果是一个 AsyncToken
对象,它不是调用 getUser
的结果。令牌是异步完成令牌设计模式的一部分。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;
}