2011-12-06 62 views
0

海蘭夥計們,交互UI和Java代碼

OK,(對我來說)我的問題是很難解釋的,但我想對於大多數的你會顯得很瑣碎。我正在使用spring webflow構建一個應用程序。用戶必須在webapp中鍵入他們的用戶名,然後存儲在一個bean中。當他們點擊按鈕「登錄」時,會調用一個bean的方法(connect()),該方法會建立到另一個服務器的jms連接。

public class HumanBrokerBean implements Serializable { 

    /** The Constant serialVersionUID. */ 
    private static final long serialVersionUID = 1L; 

    /** The broker name. */ 
    private String brokerName; 

    /** The password. */ 
    private String password; 

    private double cashPosition = 0; 



    /** 
    * Gets the password. 
    * 
    * @return the password 
    */ 
    public String getPassword() { 
     return password; 
    } 

    /** 
    * Sets the password. 
    * 
    * @param password 
    *   the new password 
    */ 
    public void setPassword(String password) { 
     this.password = password; 
    } 

    /** 
    * Gets the broker name. 
    * 
    * @return the broker name 
    */ 
    public String getBrokerName() { 
     return brokerName; 
    } 

    /** 
    * Sets the broker name. 
    * 
    * @param brokerName 
    *   the new broker name 
    */ 
    public void setBrokerName(String brokerName) { 
     this.brokerName = brokerName; 
    } 

    /** 
    * @return the cashPosition 
    */ 
    public double getCashPosition() { 
     return cashPosition; 
    } 


     public boolean connect(){ 

     ConnectionService connection = new ConnectionService(); 
     //if there have been problems while establishing the connection 
     if(!connection.connect(username, password, serverConnection, byPass)){ 
      return false; 
     } 
     //if connection was established 
     return true; 
    } 

} 

經過一段時間後,來自遠程服務器的消息到達,說明特定用戶的CashPosition已更改。現在我將不得不更新比應該顯示在UI中的變量「cashPosition」。

1)我的問題是我根本無法訪問bean的值。我如何設法訪問它們?

2)經過一段時間用戶可能想要發送消息到服務器。爲此,我在ConnectionService類中有一個方法。現在我想在Bean中創建一個應該在UI和ConnectionService之間進行調解的方法。在這裏,我有問題,我不能創建類變量和類似

private Connection Service connection; 

public void sendMessage(String message){ 
    connection.send(message); 
} 

一個根據方法因爲類連接服務的某些元素是不可序列(ActiveMQ的)。這就是爲什麼我這樣嘗試過:

public void sendMessage(String message){ 
    ConnectionService connection = new ConenctionService(); 
    connection.send(message); 
} 

但是這種解決方案總是創建類連接服務,這並不在這裏工作,因爲的ActiveMQ ...的新實例,所以,我必須能夠訪問這個班從我的豆,但我不知道如何。

我希望我colud讓你我的問題清楚......

任何幫助高度apprechiated!

回答

0

你可以把你的connection爲類變量,你試過,但與transient關鍵字標記,以防止它被序列化,像這樣:

private transient Connection Service connection; 

要知道,如果bean是有史以來反序列化,connection將爲空 - 因此您需要檢查此操作,或者提供執行默認反序列化的自定義private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException;方法,然後重新創建連接。 (其他詳細信息請見http://docs.oracle.com/javase/6/docs/api/java/io/Serializable.html。)

+0

mhm,謝謝你的回答,但我不確定這是否會解決我的問題。儘管我已經閱讀了很多關於(春季)的豆類,但我對它很陌生。我的主要問題確實是:我如何訪問/操作已經以編程方式創建的bean的內容?換句話說:「普通」java代碼和bean之間的連接是怎樣的? –