更新 Spring Webflow 中的对象未显示在 UI 上

发布于 2024-12-19 09:58:29 字数 3408 浏览 3 评论 0原文

我真的很困惑,因为我对整个主题都很陌生。我目前正在使用 spring webflow 开发一个项目。在我的网络界面中,用户必须输入存储在 bean 中的用户名。

表单:

  <fieldset>
       <legend>User Details</legend>
       <h:outputText value="Please enter the Name for your Broker:    " />
       <h:inputText id="brokerName" value="#{brokerBean.brokerName}"/>
       <h:outputText value="Please enter the password for your Broker: " />
       <h:inputSecret id="password" value="#{brokerBean.password}"/>
    </fieldset>
    <p:commandButton value="Register Broker" action="register" ajax="false"/> 

相应的 bean:

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;

    ... getters & setters...

输入用户名后,用户将转到下一页,其中应显示当前的 CashPosition 并每 5 秒更新一次,因为该值可能已更改(通过管理该值) PrimeFaces 民意调查)。到目前为止,这是有效的。

当用户输入他的用户名和一些其他值时,他按下按钮并触发相应流程的转换。

<view-state id="view">
        <transition on="register" to="mainpage">    
            <evaluate expression="brokerBean.checkCredentials(flowRequestContext)" />
            <evaluate expression="connectionBean.connect(brokerBean.brokerName, brokerBean.password, brokerBean)" /> 
        </transition>
    </view-state>

和 ConnectionBean 的方法:

public boolean connect(String username, String password, HumanBrokerBean brokerBean){

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

为了更新 bean 的值,我将 bean 对象转发到其他一些类。

public class ConnectionService {
    public boolean connect (String username, String password, String serverURL,
        boolean bypass, HumanBrokerBean brokerBean){ ....
    }
}

当我尝试打印 bean 对象的变量值时,

System.out.println("Brokername :"+brokerBean.getBrokerName())

我得到了正确的结果。当我现在尝试更新 cashPosition 时,

brokerBean.setCashPosition();

正确的结果不会显示在我的网络界面民意调查部分中。始终返回初始值零。

上面提到,我用 primefaces-poll 实现了轮询功能,它使用了 AJAX。

<h:form>  
    <h:outputText id="txt_cash" value="#{brokerBean.cashPosition}" />  
    <p:poll interval="3" update="txt_cash" />  
</h:form> 

我总是得到的 AJAX 响应...总是返回 0.0,这是每个 Broker 的 cashPosition 的初始值...

<?xml version='1.0' encoding='UTF-8'?>
<partial-response><changes><update id="j_idt16:txt_cash"><![CDATA[<span id="j_idt16:txt_cash">0.0</span>]]></update><update id="javax.faces.ViewState"><![CDATA[e1s2]]></update></changes><extension primefacesCallbackParam="validationFailed">{"validationFailed":false}</extension></partial-response>

对我来说,这意味着我的 BrokerBean 有两个不同的实例,但是我不知道为什么。我想我必须在我的 bean 中使用一些注释,但我不确定是哪一个。

我需要做什么才能使这个场景运行?

I'm really getting confused because I'm pretty new to this whole topic. I'm currently developing a project using spring webflow. In my webinterface a user has to type in a username that is than stored in a bean.

The Form:

  <fieldset>
       <legend>User Details</legend>
       <h:outputText value="Please enter the Name for your Broker:    " />
       <h:inputText id="brokerName" value="#{brokerBean.brokerName}"/>
       <h:outputText value="Please enter the password for your Broker: " />
       <h:inputSecret id="password" value="#{brokerBean.password}"/>
    </fieldset>
    <p:commandButton value="Register Broker" action="register" ajax="false"/> 

The according bean:

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;

    ... getters & setters...

After typing in the username the user is forwarded to the next page where his current cashPosition should be displayed and updated every 5 seconds, because the value might have changed (managed that with PrimeFaces poll). This is working so far.

When the user has typed in his username and some other values he presses a button and a transition of the according flow is fired.

<view-state id="view">
        <transition on="register" to="mainpage">    
            <evaluate expression="brokerBean.checkCredentials(flowRequestContext)" />
            <evaluate expression="connectionBean.connect(brokerBean.brokerName, brokerBean.password, brokerBean)" /> 
        </transition>
    </view-state>

And the method of the ConnectionBean:

public boolean connect(String username, String password, HumanBrokerBean brokerBean){

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

To update the values of the bean, I forwarded the bean object to some other class

public class ConnectionService {
    public boolean connect (String username, String password, String serverURL,
        boolean bypass, HumanBrokerBean brokerBean){ ....
    }
}

When i try to outprint the variable-values of the bean object with

System.out.println("Brokername :"+brokerBean.getBrokerName())

I'm getting the correct results. When I now try to update the cashPosition with

brokerBean.setCashPosition();

The correct result is not displayed in my webinterface poll-section. The initial value of zero is returned always.

As mentioned above, I implemented the polling function with primefaces-poll, which uses AJAX.

<h:form>  
    <h:outputText id="txt_cash" value="#{brokerBean.cashPosition}" />  
    <p:poll interval="3" update="txt_cash" />  
</h:form> 

And the AJAX response I always get... returns always 0.0, which is the initial value of the cashPosition of each Broker...

<?xml version='1.0' encoding='UTF-8'?>
<partial-response><changes><update id="j_idt16:txt_cash"><![CDATA[<span id="j_idt16:txt_cash">0.0</span>]]></update><update id="javax.faces.ViewState"><![CDATA[e1s2]]></update></changes><extension primefacesCallbackParam="validationFailed">{"validationFailed":false}</extension></partial-response>

For me that means, that there are two different instances of my BrokerBean but I'm not sure why. I think I will have to use some annotation in my bean, but I'm not sure which one.

What would I need to do to make this scenario run?

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

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

发布评论

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

评论(1

唠甜嗑 2024-12-26 09:58:29

您的 SWF servlet 是否包含“组件扫描”标记?如果是,这将导致 Bean 被扫描两次:首先在根 Web 应用程序上下文中,然后在 SWF Servlet 上下文中。您的流程操作将引用 SWF-servlet bean 实例,但 JSF EL 表达式将位于主应用程序上下文中。

对于 MVC/SWF,建议仅扫描 servlet 上下文中的 @Controller beans。看
为什么 DispatcherServlet 创建另一个应用程序上下文?

Does your SWF servlet contain "component-scan" tag? If yes, this will cause beans to be scanned twice: first in root web application context, then in SWF servlet context. Your flow actions will refer to SWF-servlet bean instance, but JSF EL expressions will be in main application context.

For MVC/SWF it is recommended to scan only @Controller beans in servlet context. See
Why DispatcherServlet creates another application context?

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