2011-12-06 64 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之间的连接是怎样的? –