升级到JSF 2.3后,具有属性/注入的自定义转换器不再起作用

时间:2019-08-19 07:21:00

标签: cdi jsf-2.3

我正在尝试升级/使用与JSF 2.2(在Wildfly 13上)一起使用的自定义转换器,以便在JSF 2.3(在Wildfly 17.0.1上运行的Mojarra 2.3.9.SP02)上工作

通过在标记库中定义的自己的标记来使用转换器。 只要不使用标签属性,一切都很好。只是没有在转换器中设置属性。设置员永远不会被召集。 但是,如果我从转换器中删除managed = true,则会设置属性,但是注入将不再起作用。

转换器的用法如下:

<h:inputText id="text" value="#{welcome.text}">
    <cdijsf:converterWithAttr id="myConverter" attr="myAttrValue" />
</h:inputText>

标签库:

<facelet-taglib version="2.3"
        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-facelettaglibrary_2_3.xsd">
    <namespace>http://cdijsf.transdata.net/jsf</namespace>
    <tag>
        <tag-name>converterWithAttr</tag-name>
        <converter>
            <converter-id>cdijsf.ConverterWithAttr</converter-id>
        </converter>
        <attribute>
            <name>attr</name>
            <type>java.lang.String</type>
        </attribute>
    </tag>
</facelet-taglib>

这是转换器代码:

@Dependent
@FacesConverter(value = "cdijsf.ConverterWithAttr", managed = true)
public class ConverterWithAttr implements Converter<String> {

    @Inject
    private BeanManager beanManager;

    private String attr;

    public ConverterWithAttr() {
    }

    @PostConstruct
    private void init() {
      // If 'managed = true' beanManager is injected at this point.
      // If 'managed = false' beanManager is null at this point
    }

    @Override
    public String getAsObject(FacesContext context, UIComponent component, String value) {
        return value;
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, String value) {
        return value;
    }

    public String getAttr() {
        return attr;
    }

    public void setAttr(String attr) {
        // If 'managed = true' setAttr is never called
        // If 'managed = false' setAttr is called
        this.attr = attr;
    }

}

faces-config.xml:

<faces-config version="2.3"
        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">
</faces-config>

我还像这样明确声明了JSF 2.3:

@FacesConfig(version = Version.JSF_2_3)
@ApplicationScoped
public class JsfConfiguration {

}

beans.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans 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/beans_2_0.xsd"
    version="2.0"
    bean-discovery-mode="annotated">
</beans>

0 个答案:

没有答案