There are a couple of steps to enabling CDI for JSF2 portlets that use Portlet Bridge.
First is to add the following dependency to your pom.xml to add the portlet integration library for CDI:
When deploying to GateIn, the portlet integration library for CDI is automatically included for JSF2 portlets that use CDI.
So we need to ensure the dependency is marked as provided in our pom.xml:
Add the following portlet filter definition into your portlet.xml:
Then add to portlet.xml the activation of the above filter for each portlet that requires CDI:
For CDI 1.0 the following scopes are available for use within JSF2 portlets:
There are some possible pitfalls with CDI that are explained below.
Any beans that are defined as @ConversationScoped, but which remain transient, that store data during the ActionRequest will not have that data accessible within the bean during a subsequent RenderRequest. As the Portlet lifecycle uses separate servlet requests to represent ActionRequest and RenderRequest, it results in the conversation context being deactivated at the end of ActionRequest and all transient conversations are destroyed.
The only difference in behavior between a JSF2 application and a JSF2 portlet is that there can be problems if a long running conversation is ended within a RenderRequest.
For example, when a long running conversation is ended during RenderRequest CDI will destroy the conversation at the end of the JSF render response phase, but the id of the conversation will still be present within the Portlet Bridge scope. As the id of the conversation is retained by the Portlet Bridge, if the user refreshes the portlet page and a RenderRequest is sent to the portlet, the previously saved conversation id will be attempted to be activated by CDI but the associated conversation context does not exist.
One work around for the above problem is to not end a long running conversation. Instead of always calling begin() on the injected conversation, only call it when isTransient() is true. This will prevent a failure in converting a transient to long running conversation.
If it's possible for your portlet to only end a long running conversation within an ActionRequest, then you won't experience the above problem.
As the conversation id parameter, cid, is set as a request parameter on the url, each time we convert a conversation from long running to transient, or vice versa, we need to ensure that any urls that our portlet uses are updated. This ensures that the latest value for the conversation id parameter, or lack of one if we converted a conversation to transient, are present on any links the user clicks. Not doing so can result in problems such as:
- Long running conversations being lost because the conversation id parameter was not present on subsequent requests to the portlet
- A CDI container throwing ContextNotActiveException because the conversation id parameter that was present on the link is no longer present as a valid conversation for use.
In most cases its simply a matter of adding more ids to the render attribute of any ajax component that will start or end a long running conversation, so that the ids of all components that contain url links to actions using a conversation or links to pages of the portlet are included in the list to be re rendered.
@RequestScoped will work within a CDI and JSF2 portlet, but due to the portlet lifecycle you need to be aware that data from an ActionRequest will not be retained for use in a RenderRequest.
There are plans to implement a replacement for @RequestScoped specifically for use within portlets, but it is not currently available.