在JSF中添加faces消息时,我的动作不会执行?

时间:2011-07-30 02:40:51

标签: jsf-2

我有以下支持bean:

@ViewScoped
@ManagedBean
public class WeighFamilyBacking2 implements Serializable {

private static final long serialVersionUID = 1L;
private String[] children = new String[] { "Child1", "Child2", "Child3" };
private HashMap<String, Integer> newWeights;

public WeighFamilyBacking2() {
    newWeights = new HashMap<String, Integer>();
    for (String s : getChildren())
        newWeights.put(s, new Integer(0));
}

public void distributeWeightsWithoutMessage(ActionEvent event) {
    for (String s : newWeights.keySet()) {
        newWeights.put(s, newWeights.get(s) + 1);
    }
}

public void distributeWeights(ActionEvent event) {
    for (String s : newWeights.keySet()) {
        newWeights.put(s, newWeights.get(s) + 1);
    }

    FacesContext.getCurrentInstance().addMessage(null,
            new FacesMessage("Succesful", "Weights redistributed."));
}

public HashMap<String, Integer> getNewWeights() {
    return newWeights;
}

public List<String> getChildren() {
    return Arrays.asList(children);
}
}

...以及以下xhtml页面:

  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html">
<h:body>
    <h:form>
        <ui:repeat var="child" value="#{weighFamilyBacking2.children}">
            <h:outputText value="#{child}" />
            <h:outputText value="#{weighFamilyBacking2.newWeights[child]}" /> -
            <h:outputText value="#{weighFamilyBacking2.newWeights[child]}" /> - 
            <h:inputText id="oz" value="#{weighFamilyBacking2.newWeights[child]}" />
            <h:inputText id="lbs"
                value="#{weighFamilyBacking2.newWeights[child]}" />
            <br />
        </ui:repeat>

        <h:commandButton
            actionListener="#{weighFamilyBacking2.distributeWeights}"
            value="Redistribute" />


        <h:commandButton
            actionListener="#{weighFamilyBacking2.distributeWeightsWithoutMessage}"
            value="Redistribute Without Message" />
    </h:form>
</h:body>
</html>

这是一个简单的可重复测试用例。当您单击没有消息的重新分发时,事情按预期工作。单击重新分发按钮时,它将显示成功消息,但不会更新输入字段。但是,文本输出字段只更新一次。

我尝试在两个按钮上使用immediate = true,但这并不影响这一点。这是一个非常简单的案例,我无法理解为什么它不起作用。

我已经尝试了所有最新版本的Mojarra,包括2.1.3。

1 个答案:

答案 0 :(得分:1)

这是另一个<ui:repeat>异常。我还没有找到确切的根本原因,以便我可以检查这是否已经报告给JSF guys并且如果有必要报告它,但我可以告诉它,当我替换<ui:repeat>时它可以工作通过<h:dataTable>

<h:dataTable var="child" value="#{weighFamilyBacking2.children}">
    <h:column>
        <h:outputText value="#{child}" />
        <h:outputText value="#{weighFamilyBacking2.newWeights[child]}" /> -
        <h:outputText value="#{weighFamilyBacking2.newWeights[child]}" /> - 
        <h:inputText id="oz" value="#{weighFamilyBacking2.newWeights[child]}" />
        <h:inputText id="lbs"
            value="#{weighFamilyBacking2.newWeights[child]}" />
    </h:column>
</h:dataTable>

也许<table>在语义上对你不正确。如果这真的是不可取的,你可能想要检查它是否有效但没有问题Tomahawk的<t:dataList>,RichFaces'<rich:dataList>,PrimeFaces'<p:dataList>等每个都支持渲染孩子没有额外的标记

更新:我将其报告为issue 2157