交互 UI 和 Java 代码

发布于 2024-12-20 07:15:29 字数 2490 浏览 6 评论 0原文

大家好,

好吧,(对我来说)我的问题很难解释,但我想对你们大多数人来说这似乎是微不足道的。我正在使用 spring webflow 构建一个应用程序。用户必须在 Web 应用程序中输入用户名,然后将其存储在 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 已更改。现在我必须更新 Bean 的变量“cashPosition”,该变量应显示在 UI 中。

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,它在这里不起作用......所以我必须能够从我的 bean 访问这个类,但我我不知道怎么办。

我希望我能够让您清楚我的问题...

非常感谢任何帮助!

Hy Folks,

ok, (for me) my problem is hard to explain but I guess for most of you it will seem pretty trivial. I'm building an application using spring webflow. The users have to type in their username in the webapp, which is then stored in a bean. When they click on the button "login" a method of the bean ( connect() ) is called, which than establishes a jms connection to a another server.

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;
    }

}

After some time a message from the remote server arrives saying that the CashPosition of a particular user has changed. Now I will have to update the variable "cashPosition" of the Bean whcih than should be displayed in the UI.

1) My Problem is that I simply can't access the the values of the bean. How can I manage to access them?

2) After some time the user may want to send a message to the server. For this reason I have a method inside my ConnectionService Class. Now I wanted to create a method in the Bean that should mediate between the UI and the ConnectionService. Here I have the problem, that I can't create a class variable and an according method like

private Connection Service connection;

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

because some elements of the class Connection Service aren't serializable (ActiveMQ). That's why I tried it in this way:

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

But this solution always creates a new instance of the class connection service, which doesn't work here because of ActiveMQ... So I have to be able to access this class from my bean but I'M not sure how.

I hope that I colud make you my problem clear...

Any help is highly apprechiated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

友谊不毕业 2024-12-27 07:15:29

您可以按照您的尝试将连接作为类变量,但使用瞬态关键字标记它以防止其被序列化,如下所示:

private transient Connection Service connection;

请注意,如果bean 被反序列化后,connection 将为 null - 因此您要么需要检查这一点,要么提供自定义的 private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException;执行默认反序列化的方法,然后重新创建连接。 (更多详细信息请访问 http://docs.oracle。 com/javase/6/docs/api/java/io/Serializable.html。)

You could put your connection as a class variable as you tried, but mark it with the transient keyword to prevent it from being serialized, like this:

private transient Connection Service connection;

Just be aware that if the bean is ever deserialized, the connection will be null - so you either need to check for this, or provide a custom private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException; method that performs the default deserialization, then re-creates the connection. (Additional details available at http://docs.oracle.com/javase/6/docs/api/java/io/Serializable.html .)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文