在c:foreach中设置JPA实体的瞬态字段并检索它

时间:2017-10-15 15:20:40

标签: jsf foreach entity actionmethod

我是JSF的新手。我有一个c:foreach元素从bean获取图像列表。每个图像列表元素都是一个Image对象(由我创建),它具有一个瞬态布尔字段。该字段最初是错误的。我还有一个outputText来显示布尔值。现在我希望当单击一个按钮时,布尔字段设置为true,并使用新的布尔值刷新outputText。我试过但布尔值总是假的。这可能吗?看起来我设置布尔字段的图像与获取布尔字段的图像不一样,或者foreach中的图像元素可能没有更新。

JSF代码:

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

带有JPA注释的Java Image类:

@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*/
}

1 个答案:

答案 0 :(得分:0)

BalusC是对的。

您的代码正在运作

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

我在GitHub上创建了一个演示应用:https://github.com/simasch/46756639

请检查出来。