【问题标题】:Declaring component handler via annotation通过注解声明组件处理程序
【发布时间】:2014-07-31 09:10:00
【问题描述】:

我的自定义组件目前是在自定义taglib.xml文件中声明的,其中一些需要处理程序,例如:

<tag>
    <tag-name>mycomponent</tag-name>
    <component>
        <component-type>com.so.MyComponent</component-type>
        <renderer-type>com.so.MyComponentRenderer</renderer-type>
        <handler-class>com.so.MyComponentHandler</handler-class>
    </component>
</tag>

现在我想从新的基于注释的 JSF 2.2 声明中受益,写作:

@FacesComponent(value = MyComponent.COMPONENT_TYPE, tagName = "mycomponent", createTag = true)
public class MyComponent {

...但我不知道如何将组件处理程序附加到此类。处理程序类是否有任何注释?

提前非常感谢!

【问题讨论】:

    标签: jsf custom-component jsf-2.2 taglib


    【解决方案1】:

    我在尝试升级到 JSF 2.2 时也遇到了这个问题。我发现 web-facelettaglibary_2_2.xsd 有一个错误,与 Mojarra 2.2.* 实现不匹配。见https://java.net/jira/browse/JAVASERVERFACES-3762

    因此,如果您需要为组件定义处理程序类,您仍然需要在标记库文件中定义处理程序类元素,正如您在问题中指定的那样。此外,您必须将 xsd 架构降级为 web-facelettaglibrary_2_0.xsd。

    请注意,通过 JSF 2.2 扩展 @FacesComponent 并没有消除使用 taglib 文件的需要。组件处理程序类就是一个例子。根据需要标记标签属性是另一回事。

    最佳实践仍然要求所有组件在 taglib 文件中都有条目,以便正确记录它们以获得上下文帮助。由于 taglib 条目需要 tag-name 和 component-type,你不应该使用 @FacesComponent 来为每个组件定义这些。那会更容易出错。

    因此,在您的示例中,您最好的选择是坚持使用:

    @FacesComponent(MyComponent.COMPONENT_TYPE)
    public class MyComponent {
    

    【讨论】:

    • 感谢详细解释!
    猜你喜欢
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 2012-08-24
    • 1970-01-01
    • 2021-11-05
    • 1970-01-01
    • 2019-10-03
    • 2016-04-26
    相关资源
    最近更新 更多