0

以下代码不会重新呈现表单:

xhtml:

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:a4j="http://richfaces.org/a4j"
    xmlns:rich="http://richfaces.org/rich"
    template="/WEB-INF/templates/default.xhtml">
    <ui:define name="content">
        <h:form id="form">
            <rich:panel header="My Header">
                <h2>
                    <h:outputLabel value="#{tournamentBean.mode}" />
                </h2>
                <a4j:commandButton value="Toggle"
                    action="#{tournamentBean.toggleMode()}" render="form" />
            </rich:panel>
        </h:form>
    </ui:define>
</ui:composition>

豆:

import java.io.Serializable;
import javax.faces.view.ViewScoped;
import javax.inject.Named;

@SuppressWarnings("serial")
@Named("tournamentBean")
@ViewScoped
public class TournamentBean implements Serializable {

  private String mode = "A";

  public String getMode() {
    return mode;
  }

  public void toggleMode() {
    if (this.mode.equals("A"))
      this.mode = "B";
    else
      this.mode = "A";
  }
}

我使用的是 Wildfly 8.0,因此使用的是 JSF 2.2。每次单击按钮时都会调用方法 toggleMode。在 IE 11 中,它从不重新呈现表单。在 Chrome 中,它可以工作两次,但不会更多次。

我错过了什么?

4

1 回答 1

-1

@Named是 CDI 注释,@ViewScoped来自 JSF。所以你有 CDI 和 JSF 试图管理 bean,所以这当然不会工作,结果 bean 范围可以是单例的,如果它要工作的话。

替换@ViewScoped为例如@javax.enterprise.context.RequestScoped并尝试运行代码。如果您需要使用视图范围,请四处寻找 CDI 实现或conversationscope. 即使 CDI 不直接支持viewscope.

或者迁移到 JSF 和它的@ManagedBeans,但是那些被废弃了。

于 2013-12-18T23:26:53.503 回答