3

I have following FacesConverter:

@FacesConverter(forClass = Onderwerp.class, managed = true)
public class OnderwerpConverter implements Converter<Onderwerp> {

@Inject
private Web web;

@Override
public Onderwerp getAsObject(FacesContext context, UIComponent component, String value) {

    log.trace("Converting to object from string: " + value);

    return web.getAllActiveOnderwerpen().stream().filter(o -> o.getId().equals(Long.parseLong(value))).findFirst().get();
}


@Override
public String getAsString(FacesContext context, UIComponent component, Onderwerp onderwerp) {

    log.trace("Converting to string from object: " + onderwerp);

    return onderwerp.getId().toString();
}

}

The referenced CDI bean is:

@Named
@ApplicationScoped
public class Web { ... }

Faces-config.xml is:

<?xml version="1.0" encoding="UTF-8"?>
<faces-config xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_3.xsd" version="2.3">

Now whatever kind of bean I inject via @Inject it is always null. It seems the injection feature in 2.3 is not working (yet), or I am missing something :)

I'm using Mojarra 2.3.0 on EAP 7.0. Also tested without success using 2.3.3 on EAP 7.0 and 7.1.

My current workaround is replacing the code where I need the injected CDI bean like this:

return CDI.current().select(Web.class).get().getAllActiveOnderwerpen().stream().filter(o -> o.getId().equals(Long.parseLong(value))).findFirst().get();

This works fine, but is kinda ugly of course :)

Anyone has experienced this behavior?

onderbewustzijn
  • 935
  • 7
  • 32
  • Is the converter recognized as a bean so that it is injected into? Try adding some bean defining annotation to it, such as `@Dependent` or `@Named` or whatever else fits your setup. – Siliarus May 16 '17 at 08:53
  • 2
    In JSF 2.3 you can add managed=true and it automatically should recognize CDI beans... But it doesn't. I can add some bean defining annotation and it works, but that is the old workaround pre 2.3, so no point then. – onderbewustzijn May 16 '17 at 11:01
  • Meaning the workaround doesn't help anymore? BTW do you have any other CDI beans in your application so you can verify that CDI in general is working? Maybe also double check the presence of `beans.xml` (and try using bean discovery mode `all`). – Siliarus May 17 '17 at 07:05
  • @Siliarus All workarounds work ;) I have plenty of CDI beans which are injected into eachother, mixed with EJB beans. No issues there. I don't have beans. xml, because I choose to use only annotated CDI beans. I could add it and put discovery mode all, but the whole point is I want the native CDI support in `@FacesConverter` introduced in JSF 2.3. I need somebody who also has 2.3 installed in his application server who can confirm (or deny) this behavior. Thanks already for helping. – onderbewustzijn May 17 '17 at 09:21
  • 1
    created an issue at mojarra 2.3 https://github.com/javaserverfaces/mojarra/issues/4308 – onderbewustzijn Nov 29 '17 at 14:38
  • I confirm this and I have the same problem with WildFly 16.0 JSF 2.3 @FabiYo – alibttb Apr 21 '19 at 17:32

1 Answers1

0

I had the same issue and I foud the solution.

You must create a config bean indicating JSF version:

import javax.faces.annotation.FacesConfig;
import javax.faces.annotation.FacesConfig.Version;


@FacesConfig(
        // Activates CDI build-in beans
        version=Version.JSF_2_3 
        )
public class ConfigBean {

}
  • Not working, see https://github.com/javaserverfaces/mojarra/issues/4308 – onderbewustzijn Feb 05 '19 at 12:22
  • @FabiYo: did you try? Since for some it seems to do things – Kukeltje Feb 05 '19 at 12:29
  • I'll try to summarize all we've to do in order to make it work. 1 - Set managed = true in "@FacesConverter" annotation 2 - Verify faces 2.3 version in faces-config.xml 3 - Create "@FacesConfig" annotated config bean with property "version=Version.JSF_2_3" Regarding @FabiYo example, I think the problem can be related to "@ApplicationScoped" annotation in Config bean. –  Feb 05 '19 at 17:18
  • 1
    @Kukeltje, sure, that is why I logged the issue. For some it is working, because they use another application server or a servlet container – onderbewustzijn Feb 07 '19 at 06:54
  • @JesúsIbánFernándezRodríguez please provide full server config, versions of JSF etc... – onderbewustzijn Feb 07 '19 at 06:56
  • But then **always** mention your environment specs – Kukeltje Feb 07 '19 at 06:57
  • @FabiYo WildFly Full 15.0.1.Final Mojarra 2.3.5.SP2 JSF 2.3 –  Feb 12 '19 at 09:19