Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When adding a faces message in JSF my actions aren't performed?

Tags:

jsf-2

I have the following backing 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);
}
}

... And the following xhtml page:

  <!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>

This is a simple reproducible test case. When you click on the redistribute without message, things work as expected. When you click on the redistribute button it displays the success message but the input fields are not updated. However, the text output field is updated just one time.

I have tried using immediate=true on both buttons and that doesn't affect this. This is a very simple case, I can't understand why it doesn't work.

I have tried this with all recent versions of Mojarra including 2.1.3.

like image 503
jr. Avatar asked Nov 13 '22 19:11

jr.


1 Answers

This is another <ui:repeat> anomaly. I haven't nail down the exact root cause yet so that I can check if this is already reported to the JSF guys and if necessary report it, but I can tell that it works when I replace the <ui:repeat> by a <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>

Maybe a <table> is not semantically correct for you. If this is really undesireable, you might want to check if it works without problems with Tomahawk's <t:dataList>, RichFaces' <rich:dataList>, PrimeFaces' <p:dataList>, etc each which supports rendering the children without additional markup.

Update: I reported it as issue 2157.

like image 119
BalusC Avatar answered Feb 20 '23 22:02

BalusC