Search code examples
jsfjakarta-eejsf-2scope

JSF CustomeScope reset for specified view


How can I control what customscope is created during request. Right now I have solution that when I click the commandLink:

<h:commandLink action="orders">
  <f:actionListener type="com.mk.web.jsf.scope.ResetScopeListener" />
  <span data-shortcut="F6" class="start_link">Orders</span>
</h:commandLink>

The custom scope is destroyed and a new one is created. But the problem is that the custom scope only resets if I enter the customers action using this commandLink from above. When I enter to customers action by directly entering the url in browser the custom scope is not reset. How can I make that is customers action is loading even by commandLink or by url then always this custom scope should be reset. Can somebody give me some advice how to achieve this?

Thanks...


Solution

  • Typically you use a custom scope when you want your beans to live longer than the original request but shorter than the session. Based on your scenario though, it seems to me that your custom scope is behaving slightly like a request scope. Anyway, maybe I'm not understanding your scenario fully but one thing that comes to mind is that you can use system events in case a user decides to go to the next page when they type the address directly. So suppose the user tries to go to nextPage.xhtml, it could be defined like this

    <f:event type="preRenderView" listener="#{bean.performPossibleCustomScopeCleanUp}" />
     <h:head>
     </head>
    

    The (pseudo) method above will fire before the view root (aka nextPage.xhtml) is rendered. Inside the method you can check to see if your custom scope is still "alive" and you will handle it as needed. Keep in mind though that this event will be fired every time the page is requested. For instance, if you click the h:commandLink performPossibleCustomScopeCleanUp will still be invoked. There are ways around that

    What can <f:metadata>, <f:viewParam> and <f:viewAction> be used for?

    Just scroll down to the section Performing business action on GET parameters of the answer

    An even better approach would be to implement a custom NavigationHandler through JSF's ConfigurableNavigationHandler API so you can keep track of the URL and perform actions as needed. I didn't dig too dig so you might actually find a better code sample. Hope this helps.