【问题标题】:Inside c:foreach set transient field of a JPA entity and retrieve it在 c:foreach 内部设置 JPA 实体的瞬态字段并检索它
【发布时间】:2018-03-27 04:01:07
【问题描述】:

我是 JSF 的新手。我有一个从 bean 获取图像列表的 c:foreach 元素。每个图像列表元素都是一个具有瞬态布尔字段的图像对象(由我创建)。该字段最初为假。我还有一个 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*/
}

【问题讨论】:

  • 对未来 JSF 问题的建议:只要使用带有 main() 方法的普通 Java 应用程序无法证明问题,请不要使用 [java] 标记。 [java] 用户不是按照定义能够理解 JSF 问题,也不是按照定义对在他们的列表中查看 JSF 问题感兴趣。

标签: jsf foreach entity actionmethod


【解决方案1】:

BalusC 是对的。

您的代码正在运行

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

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

请检查一下。

【讨论】:

  • 请先尝试自己,然后再作为答案。
  • 很好的评论,Euleos 问题的解决方案是什么?
猜你喜欢
  • 2016-09-17
  • 2013-04-28
  • 2014-01-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-28
  • 2013-09-03
  • 1970-01-01
相关资源
最近更新 更多