本文介绍了在视图之间保留Bean值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找有关使用JSF 2在视图之间保留Bean值的最佳实践.

I'm looking for best practices regarding preserving bean values between views using JSF 2.

请考虑以下情形:
*在视图A中,实例化视图范围的bean ABean并检索数据以在视图中显示.
*在视图A中,您可以单击一个条目以在视图B中查看其详细信息.
*从视图B返回视图A时,将显示先前由ABean检索的数据.

Consider the following scenario:
* In view A, the view scoped bean ABean is instantiated and retrieves data for display in the view.
* In view A you can click on an entry to view details for it in view B.
* When returning from view B to view A, the data previously retrieved by ABean is displayed.

保存从A Bean检索到的数据以便在从视图B返回时能够再次显示的最佳方法是什么?
再次检索数据通常不会发生,因为从视图B返回时会创建一个新的ABean实例,因此通常不会进行检索,因为这是一项耗时的操作,并且会导致不良的使用体验.
不允许将ABean置于会话作用域范围内,因为如果您离开页面然后返回,则将显示缓存"数据,而不是检索新数据(即,您无法确定是否由于导航而加载视图A返回页面,或者如果您要从视图B返回).

What is the best way to preserve the data retrieved by ABean, to be able to display it again when returning from view B?
Retrieving the data again, which would normally happen since a new instance of ABean is created when returning from view B, is not an option since it's a time-consuming operation and leads to a bad use experience.
Having ABean being session scoped is not an option, since if you leave the page and then return, the "cached" data would be displayed instead of retrieving new data (i.e. you cannot determine if you're loading view A as a result of navigating to the page or if you're returning from view B).

我正在寻找的显然是一个对话范围,它很好地解决了这个问题(这是我们以前使用JSF 1和WebFlow时所拥有的).不幸的是,JSF没有该功能,并且由于我们处于Portlet环境中,因此无法使用CDI.

What I'm looking for is obviously a conversation scope, which solves this nicely (and is what we had before, when using JSF 1 and WebFlow). Unfortunately, JSF doesn't have that and since we're in a portlet environment we cannot use CDI.

有什么想法吗?

推荐答案

使用带有条件渲染内容的单个视图是最简单的.

Using a single view with conditionally rendered content is the easiest.

例如

<h:form>
    <h:dataTable id="table" value="#{bean.items}" var="item" rendered="#{empty bean.item}">
        <h:column>
            #{item.foo}
        </h:column>
        <h:column>
            #{item.bar}
        </h:column>
        <h:column>
            <h:commandLink value="edit" action="#{bean.edit(item)}">
                <f:ajax execute="@this" render="@form" />
            </h:commandLink>
        </h:column>
    </h:dataTable>

    <h:panelGrid id="detail" columns="3" rendered="#{not empty bean.item}">
        <h:outputLabel for="foo" />
        <h:inputText id="foo" value="#{bean.item.foo}" />
        <h:message for="foo" />

        <h:outputLabel for="bar" />
        <h:inputText id="bar" value="#{bean.item.bar}" />
        <h:message for="bar" />

        <h:panelGroup />
        <h:commandButton value="save" action="#{bean.save}">
            <f:ajax execute="detail" render="@form" />
        </h:commandButton>
        <h:messages globalOnly="true" layout="table" />
    </h:panelGrid>
</h:form>

具有以下视图范围的Bean

with the following view scoped bean

private List<Item> items;
private Item item;

@EJB 
private ItemService itemService;

@PostConstruct
public void init() {
    items = itemService.list();
}

public void edit(Item item) {
    this.item = item;
}

public void save() {
    itemService.save(item);
    item = null;
}

如果有必要,可以将视图部分分成两个<ui:include>.如有必要,可以将bean分成两个bean(每个部件一个),详细信息中的一个将表1作为托管属性.但这不是imo,只是使它变得更加复杂.

The view parts could if necessary be split out over two <ui:include>s. The bean could if necessary be split out over two beans (one for each part) of which the detail one has the table one as managed property. But that's imo not necessary and only makes it more complicated.

这篇关于在视图之间保留Bean值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 22:52