【问题标题】:Weld + JSF 2.0 @ConversationScoped doesn't keep stateWeld + JSF 2.0 @ConversationScoped 不保持状态
【发布时间】:2012-02-24 15:26:14
【问题描述】:

我正在尝试在 JBoss AS 7 和 JSF 2.0 应用程序中使用 CDI 的 Weld 实现。

事实上,当我开始对话时,我的 @ConversationSconed @Named bean 似乎并没有保持他的状态。

为了看到这一点,我只是使用了一个计数器,每次单击命令按钮时我都会使用 Primefaces 和 ajax 递增。

beans.xml 存在于类路径中(META-INF、WEB-INF ...),我只想用@SessionScoped bean 或@ManagedBean @ViewScoped 精确说明,它工作得很好!

但我更喜欢使用@ConversationScoped 并使用@Named bean,而不是使用@ManagedBean。

也许我需要为 JBoss AS 7 或 web.xml 做额外的配置,我不知道...

这是我的@ConversationScoped bean:

@Named
@ConversationScoped
public class ConversationTest implements Serializable {
    private int counter;

    @Inject
    private Conversation conversation;

    public void startConversation() {
        System.out.println(counter);

        counter++;

        if(conversation.isTransient())
            conversation.begin();
    }

    public void stopConversation() {
        if (!conversation.isTransient())
            conversation.end();
    }

    public int getCounter() {
        return counter;
    }

    public void setCounter(int counter) {
        this.counter = counter;
    }
}

这是我的 xhtml 页面的内容:

    <h:form prependId="false">
        <h:panelGroup id="tests">
            <h:outputText value="#{conversationTest.counter}" /> <br/>
            <h:outputText value="Test : #{conversationTest.testHello}" /> <br/><br/>
        </h:panelGroup>

        <p:commandButton
                value="Start !"
                actionListener="#{conversationTest.startConversation}"
                update="tests" />
        <br/>

        <p:commandButton
                value="Stop !"
                actionListener="#{conversationTest.stopConversation}"
                update="tests" />
    </h:form>

我做错了什么?我是不是忘记了什么?

非常感谢您的回答!

【问题讨论】:

  • 我不确定您所说的“不保持状态”是什么意思?您期望的结果是什么?正在发生什么?
  • 当我点击按钮时,我希望数字 conversationTest.counter 增加,但事实并非如此。

标签: jsf jboss cdi conversation-scope


【解决方案1】:

您是否尝试过使用标准的 h:commandButton 而不是 PrimeFaces 种类?如果 PrimeFaces 使用 AJAX(我记得是这样),您可能需要将对话 ID 作为参数发送。

【讨论】:

  • 感谢您的回答!我将尝试使用 h:commandButton 并让您知道结果;)
猜你喜欢
  • 2011-05-22
  • 1970-01-01
  • 1970-01-01
  • 2011-06-04
  • 1970-01-01
  • 2017-12-01
  • 2013-08-15
  • 2013-06-10
  • 2013-12-18
相关资源
最近更新 更多