简体   繁体   中英

Save state of ViewScoped bean

I'm building a JSF application using Mojarra 2.1.6 implementation. My application screens are @ViewScoped and each bean is being destroyed from one view to another one. 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:

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

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.

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. I don't know if there is a better way to do this in JSF, maybe maintaining the @ViewScoped beans...

The f:viewParam, f:param manages the setting, conversion and validation of GET parameters. Therefore the url will be something like 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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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