0

Environment:

  • Wildfly 22
  • Java 11
  • JSF 2.3

I have created a @FacesConverter managed=true but when I try to @Inject a @Stateless class I get null.

face.xhtml

    ...
    <h:outputText value="#{workBean.work.type.name}" rendered="#{!workBean.editable}"/>
    <p:selectOneMenu id="cbxType" value="#{workBean.work.type}"
                     required="true" requiredMessage="#{msgs.wrk_reqtype}"
                     converter="workTypeConverter"
                     rendered="#{workBean.editable}">
        <f:selectItem itemLabel="#{msgs.select}"  itemValue="#{null}" noSelectionOption="false" />
        <f:selectItems value="#{workBean.types}" var="t"
                       itemValue="#{t}" itemLabel="#{t.name}"/>
    </p:selectOneMenu>
    ...

WorkTypeConverter

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
import javax.faces.convert.FacesConverter;
import javax.inject.Inject;
import java.time.format.DateTimeParseException;

@FacesConverter(value = "workTypeConverter", managed = true)
public class WorkTypeConverter implements Converter {

    @Inject
    WorkTypeManager typeMgr; //null¿?

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        if (value == null || value.isEmpty()) {
            return null;
        }
        try {
            return typeMgr.findById(Integer.parseInt(value));
        } catch(Exception e){
            throw new ConverterException(new FacesMessage(value + " string is not a valid WorkType"));
        }
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        if (value == null) {
            return null;
        }
        if (value instanceof WorkType) {
            return ((WorkType)value).getId().toString();
        } else {
            throw new ConverterException(new FacesMessage(value + " object is not a valid WorkType"));
        }
    }
}

pom.xml dependencies

<dependencies>
    <dependency>
        <groupId>jakarta.platform</groupId>
        <artifactId>jakarta.jakartaee-api</artifactId>
        <version>8.0.0</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.primefaces</groupId>
        <artifactId>primefaces</artifactId>
        <version>10.0.0</version>
    </dependency>
    <dependency>
        <groupId>io.jsonwebtoken</groupId>
        <artifactId>jjwt</artifactId>
        <version>0.9.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>3.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>3.15</version>
        <type>jar</type>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.25</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
</dependencies>
Joe
  • 7,749
  • 19
  • 60
  • 110
  • Does https://stackoverflow.com/questions/7531449/cdi-injection-into-a-facesconverter answer your question? – Tomek May 22 '21 at 11:27
  • Yes, it works by substituting FacesConverter by Named, but I would like to find out why is not working with FacesConverter. Wildfly22 has support to JSF 2.3. Any idea? – Joe May 23 '21 at 19:32

1 Answers1

-1

You cannot use @Inject in FacesConverter or FacesComponent. But you can programmatically get CDI bean. For example

CDI.current().select(WorkTypeManager.class).get();
  • 1
    This is not an answer. This is a work around. Native support for `@Inject` in `FacesConverter` (and `FacesValidator`) has been added to JSF 2.3 in 2017. – BalusC May 22 '21 at 17:36