[jboss-jira] [JBoss JIRA] (WFLY-2611) Unify JSF action listener and CDI integration

Farah Juma (JIRA) issues at jboss.org
Fri Apr 11 18:16:14 EDT 2014


    [ https://issues.jboss.org/browse/WFLY-2611?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12961311#comment-12961311 ] 

Farah Juma commented on WFLY-2611:
----------------------------------

In order for CDI to work when providing a custom ActionListener via f:actionListener, it looks like the "binding" attribute needs to be specified instead of the "type" attribute. When the "type" attribute is specified, a new instance of the given action listener will be created. However, this instance won't be managed by CDI and so injection won't work. In particular, here's what happens when the "type" attribute is used:

>From [com.sun.faces.taglib.jsf_core.ActionListenerTag | https://github.com/jboss/mojarra/blob/2.2.6-jbossorg-3/jsf-ri/src/main/java/com/sun/faces/taglib/jsf_core/ActionListenerTag.java#L176]:

{code}listener = (ActionListener) Util.getListenerInstance(type, null);{code}

Then, from [com.sun.faces.util.Util#getListenerInstance | https://github.com/jboss/mojarra/blob/2.2.6-jbossorg-3/jsf-ri/src/main/java/com/sun/faces/util/Util.java#L219]:

{code}instance = ReflectionUtils.newInstance(((String) type.getValue(faces.getELContext())));{code}

This will end up calling [Class clazz.newInstance()| https://github.com/jboss/mojarra/blob/2.2.6-jbossorg-3/jsf-ri/src/main/java/com/sun/faces/util/ReflectionUtils.java#L165] to instantiate the ActionListener. Since it's not instantiated by CDI, injection won't work.

Adding the @Named annotation to the custom ActionListener implementation and using the "binding" attribute instead allows you to bind to the action listener managed by CDI, e.g.,

{code:xml}<f:actionListener binding="#{myActionListener}"/>{code} 

I updated the reproducer to use the "binding" attribute and verified that the tests in ActionListenerCdiTest now pass (see https://github.com/fjuma/javaee7-samples/commit/fe0bc23d73e40c7768a9cf53d510b3b9c66e2f04).
                
> Unify JSF action listener and CDI integration
> ---------------------------------------------
>
>                 Key: WFLY-2611
>                 URL: https://issues.jboss.org/browse/WFLY-2611
>             Project: WildFly
>          Issue Type: Feature Request
>      Security Level: Public(Everyone can see) 
>          Components: CDI / Weld, JSF
>    Affects Versions: 8.0.0.CR1
>            Reporter: Tomas Remes
>            Assignee: Farah Juma
>
> There are three ways, how can user specify custom JSF action listener:
> 1. Method binding - with actionListener param. This case is ok, meaning CDI injection and interceptor works for given method/bean.
> 2. Providing custom implementation - <f:actionListener type="org.jboss.jsf.listeners.MyActionListener"/>. In this case CDI doesn't work at all for processAction method of given class. 
> 3. Defining in faces-config - in this case CDI injection works, but interceptor is not executed. 
> Reproducer available at https://github.com/tremes/javaee7-samples/tree/cdi-testing/jsf/actionlistener-injection. Test can be executed with "mvn clean test -Pwildfly-managed-arquillian" or "mvn clean test -Pwildfly-remote-arquillian" and IMHO all should pass.
>  

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira


More information about the jboss-jira mailing list