【问题标题】:JSF2.3 FacesConverter managed can not inject a stateless serviceJSF2.3 FacesConverter 托管无法注入无状态服务
【发布时间】:2021-08-11 02:06:54
【问题描述】:

环境:

  • 野蝇 22
  • Java 11
  • JSF 2.3

我创建了一个 @FacesConverter managed=true 但是当我尝试 @Inject 一个 @Stateless 类时,我得到了 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>
    <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>

【问题讨论】:

  • stackoverflow.com/questions/7531449/… 回答你的问题了吗?
  • 是的,它通过将 FacesConverter 替换为 Named 来工作,但我想知道为什么不使用 FacesConverter。 Wildfly22 支持 JSF 2.3。有什么想法吗?

标签: jsf converters inject jsf-2.3


【解决方案1】:

您不能在 FacesConverter 或 FacesComponent 中使用 @Inject。但是您可以以编程方式获取 CDI bean。例如

CDI.current().select(WorkTypeManager.class).get();

【讨论】:

  • 这不是答案。这是一个解决方法。 2017 年,JSF 2.3 中添加了对 FacesConverter(和 FacesValidator)中 @Inject 的原生支持。
猜你喜欢
  • 1970-01-01
  • 2017-05-23
  • 1970-01-01
  • 1970-01-01
  • 2013-03-14
  • 1970-01-01
  • 2013-05-31
  • 2014-03-29
相关资源
最近更新 更多