简体   繁体   English

JSF 2.3中的@FacesConverter和@FacesValidator中的EJB和托管bean注入

[英]EJB and managed bean injections in @FacesConverter and @FacesValidator in JSF 2.3

@FacesConverter and @FacesValidator are not eligible for EJB or managed bean injection points up to JSF 2.2. @FacesConverter@FacesValidator不适合EJB或JSF 2.2以下的托管bean注入点。

They are supposed to work with JSF 2.3 (currently available as a milestone only) using an additional managed attribute with @FacesConverter and @FacesValidator as mentioned here . 他们应该用JSF 2.3(目前作为工作的里程碑使用附加专用) managed与属性@FacesConverter@FacesValidator提到这里

In JSF 2.1 very few JSF artifacts were injection targets. 在JSF 2.1中,很少有JSF工件是注入目标。 In JSF 2.2 injection was made possible in a huge amount of additional artefacts but the very ones where injection actually matters most, converters and validators, were mysteriously left in the cold . 在JSF 2.2中,可以通过大量其他工件进行注入,但是实际上注入最重要的那些对象( 转换器和验证器)却被冷落了

In JSF 2.3 this has now finally been taken care of as the following artefacts have been added to the list of injection targets: 在JSF 2.3中,现在已经解决了这一问题,因为以下伪像已添加到注入目标列表中:

  • javax.faces.convert.Converter
  • javax.faces.validator.Validator
  • javax.faces.component.behavior.Behavior

However, in contrast to the artefacts already on this list these new 3 are not automatically injection targets. 但是,与该清单中已经存在的伪像相反,这些新的3个不是自动注入目标。 They will only become so when a new attribute called "managed" on the corresponding annotations @FacesConverter , @FacesValidator and @Behavior is set to true. 只有在相应的@FacesConverter@FacesValidator@Behavior注释上名为“ managed”的新属性设置为true时, @FacesValidator @Behavior变为true。 Furthermore all these 3 annotations have been upgraded to being CDI qualifiers by adding the @Qualified annotation to their definition. 此外,通过将@Qualified批注添加到其定义中,所有这3个批注均已升级为CDI限定符。

The existing attributes of @FacesConverter , @FacesValidator and @Behavior have not been modified, meaning they are all binding , as is the new attribute "managed". @FacesConverter@FacesValidator@Behavior的现有属性尚未修改,这意味着它们都是绑定的 ,新属性“ managed”也是如此。

The attribute managed is however, unavailable in Mojarra 2.3.0-m02. 但是, managed的属性在Mojarra 2.3.0-m02中不可用。

Is it because of a milestone? 是因为一个里程碑? Is it dependent upon a specific Weld/CDI version? 是否取决于特定的Weld / CDI版本? I am currently going with GlassFish Server 4.1. 我目前正在使用GlassFish Server 4.1。 Different artifact versions are mentioned here (The default Weld version supplied by the server version is 2.2.2 final). 这里提到不同的工件版本(服务器版本提供的默认Weld版本为2.2.2 final)。

As you can see in Mojarra 2.3.0-m02's Application#createConverter() implementation , it checks if it's running in JSF 2.3 mode as per faces-config.xml version declaration before trying to grab a CDI-managed one. 如您在Mojarra 2.3.0-m02的Application#createConverter()实现中所看到的,在尝试获取CDI管理的version之前,它会根据faces-config.xml version声明检查它是否在JSF 2.3模式下运行

In other words, in order to get @FacesConverter(managed=true) , @FacesValidator(managed=true) and thus @Inject in those classes to work, you need to make sure that your webapp's faces-config.xml is declared conform JSF 2.3 as below: 换句话说,为了使@FacesConverter(managed=true)@FacesValidator(managed=true)以及因此在这些类中的@Inject起作用,您需要确保将Webapp的faces-config.xml声明为符合JSF 2.3如下:

<?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">

    <!-- Config here. -->

</faces-config>

Noted should be that the IDE may error on the JSF 2.3 XSD file not being publicly available (yet), you can safely ignore that part, it won't be validated during runtime by JSF. 应当注意的是,IDE可能会在尚未公开的JSF 2.3 XSD文件上出错(尚未),您可以放心地忽略该部分,JSF在运行时不会对其进行验证。 You could alternatively stick to 2.2 XSD and ignore any IDE warning/error on an unsupported version . 您也可以选择使用2.2 XSD并忽略不受支持version上的任何IDE警告/错误。


The jdevelopment.nl blog author has been notified about this and he will make sure that in a next update the above is clarified in blog as well. jdevelopment.nl博客的作者已收到有关此问题的通知,他将确保在下次更新中也在博客中阐明以上内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM