0

I'm new to JSF. I have a c:foreach element that gets an images list from a bean. Each images list element is an Image object (created by me) that has a transient boolean field. This field initially is false. I also have an outputText to show the boolean value. Now I want that when a button is clicked the boolean field is set to true and the outputText is refreshed with the new boolean value. I tried but the boolean value is always false. Is this possible to do? It seems like the image where I set the boolean field is not the same where I get the boolean field, or maybe the image element in the foreach is not updated.

JSF code:

<c:forEach items="#{publicGalleryImageShowController.images}" var="img">
            <h:form>
                <h:commandButton
                    action="#{img.setShowComments(true)}"
                    value="Show/Hide">
                    <f:ajax render="co-#{img.id}" />
                </h:commandButton>
            </h:form>

            <h:panelGroup id="co-#{img.id}">
                <h:outputText value="#{img.showComments}" />
            </h:panelGroup>
</c:forEach>

Java Image class with JPA annotation:

@Entity
public class Image {
    private boolean showComments;
    /*other fields*/

    @Transient
    public boolean isShowComments() {
        return showComments;
    }
    public void setShowComments(boolean showComments) {
        this.showComments = showComments;
    }   

    /*other getters and setters*/
}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Euleo
  • 1
  • 3
  • An advice for future JSF questions: please don't use [java] tag as long as the problem is not demonstrable using a plain vanilla Java application with main() method. [java] users are not per definition capable of understanding JSF questions nor are they per definition interested in seeing JSF questions in their list. – BalusC Oct 16 '17 at 08:01

1 Answers1

0

BalusC is right.

Your code is working

action="#{img.setShowComments(true)}"

I created a demo app on GitHub: https://github.com/simasch/46756639

Please check it out.

Simon Martinelli
  • 34,053
  • 5
  • 48
  • 82