简体   繁体   English

保存ViewScoped bean的状态

[英]Save state of ViewScoped bean

I'm building a JSF application using Mojarra 2.1.6 implementation. 我正在使用Mojarra 2.1.6实现构建JSF应用程序。 My application screens are @ViewScoped and each bean is being destroyed from one view to another one. 我的应用程序屏幕为@ViewScoped ,每个bean从一个视图被销毁到另一个视图。 Also I want to have a kind of navigation bar or breadcrumb, so the user can go back from page to page using it. 我也想拥有一种导航栏或面包屑,以便用户可以使用它来翻页。 To achieve that I'm using Primefaces p:button , in this way: 为了实现这一点,我以这种方式使用了Primefaces p:button

<h:panelGroup
      rendered="#{!navigationManager._DisableNavigationButtons}">
<p:toolbar>
    <p:toolbarGroup>
        <ui:repeat value="#{navigationManagerSystem._Navegables}"
                                        var="item">
            <p:button value="#{item._Title}" outcome="#{item._IncludePath}">
                <f:param name="params" value="#{item._NavigationParams}" />
            </p:button>
        </ui:repeat>
        <p:button disabled="true"
            value="#{navigationManagerSystem._Navegable._Title}" />
    </p:toolbarGroup>
</p:toolbar>

As you can see in this code, it's basically a toolbar with buttons, this buttons are saving old url's, so when user clicks one of them he can go back. 如您在此代码中看到的,它基本上是一个带有按钮的工具栏,该按钮用于保存旧的url,因此当用户单击其中一个时,他可以返回。

However, my problem is that I have some pages which are receiving view parameters and I want to do something generic, so the best idea would be to save parameters in a java.util.List ( navigationManagerSystem is a @SessionScoped bean) in order to recover them when user clicks a button to go back, to achieve the old bean state. 但是,我的问题是我有一些页面正在接收视图参数,并且我想做一些通用的事情,所以最好的主意是将参数保存在java.util.ListnavigationManagerSystem@SessionScoped bean)中,以便当用户单击按钮返回时恢复它们,以实现旧bean状态。

But the example above is not working, even the destination page is written in that way 但是上面的示例不起作用,即使目标页面也以这种方式编写

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:p="http://primefaces.org/ui"
template="/templates/general_template.xhtml">

<ui:define name="metadata">
    <f:metadata>
        <f:viewParam id="user" name="user"
            value="#{navegableUserData._ParamUser}" required="true"
            requiredMessage="User is required" />
        <f:viewParam id="params" name="params"
            value="#{navegableUserData._NavigationParams}" />
        <f:event type="preRenderView"
            listener="#{navegableUserData.initialize}" />
    </f:metadata>
    <h:message for="user" />
</ui:define>

<ui:define name="general_content">
    <p:outputPanel autoUpdate="false" id="Datos_Loged" name="Datos_Loged"
        layout="block">
        <h:form id="SystemUserForm">
            <ui:include
                src="/system/manage_user/content/edit_user/system_user_data/system_user.xhtml" />
        </h:form>
    </p:outputPanel>
</ui:define>

_NavigationParams setter is not being invoked and I have a conversion exception. _NavigationParams setter没有被调用,并且我有一个转换异常。 I don't know if there is a better way to do this in JSF, maybe maintaining the @ViewScoped beans... 我不知道在JSF中是否有更好的方法可以做到这一点,也许可以维护@ViewScoped bean。

The f:viewParam, f:param manages the setting, conversion and validation of GET parameters. f:viewParam,f:param管理GET参数的设置,转换和验证。 Therefore the url will be something like newPage.jsf?param=10. 因此,该URL将类似于newPage.jsf?param = 10。 If you try to put aa list of classes in the get parametter there will be an error because the get parameter must be a string. 如果尝试将类列表放入get参数中,则将出现错误,因为get参数必须是字符串。

I think the solution can be not to pass the parameters directly through the view. 我认为解决方案可以是不直接通过视图传递参数。 Just host them into navigationManager bean which is @SessionScoped and controls the queue of buttons. 只需将它们托管到@SessionScoped navigationManager bean中,并控制按钮队列。 Anyway, I need to know which button is clicked in order to recover the params from my @ViewScoped bean, an index value through the view would be enough, wouldn't it? 无论如何,我需要知道单击了哪个按钮才能从@ViewScoped bean中恢复参数,通过视图的索引值就足够了,不是吗?

That way I can also remove buttons that are after the one clicked in order to maintain the breadcrumb updated. 这样,我也可以删除单击后的按钮,以保持面包屑更新。

Anyway, other ideas are welcome. 无论如何,欢迎其他想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM