【问题标题】:Is it possible to pass passthrough attributes in a custom composite component?是否可以在自定义复合组件中传递传递属性?
【发布时间】:2018-09-09 06:45:03
【问题描述】:

在 JSF 2.2 中,我有一个自定义复合组件,它包装了一个普通组件(比如说 h:commandLink)。

是否可以在我的复合组件上添加passthrough attributes 并将它们传递回包装的组件?

类似这样的东西(这是一个例子,不要在意它的用处):

my:commandLink的声明:

<cc:interface>
    <cc:attribute name="text" type="java.lang.String" />
</cc:interface>

<cc:implementation>
    <h:commandLink a:magicInsertionOfAllThePassThroughAttributes>
        #{cc.attrs.text}
    </h:commandLink>
</cc:implementation>

使用my:commandLink:

<my:commandLink text="oh no" a:data-something="…" a:data-otherthing="…" />

【问题讨论】:

  • Styleclass 和 title 是 commandLink 上的现有属性。您确定您的意思是 JSF 2.2 意义上的“passThrough”吗?还是您只想在myCommandLink 上拥有动态数量的属性,所有这些属性都自动映射到h:commandLink 的现有属性?
  • 我的例子很糟糕,我用更奇特的属性对其进行了修改。目的确实是将my:commandLink 的所有未知属性映射到包装的h:commandLink
  • 我在谷歌上搜索了一下,还没有找到简单的东西(也不是复杂的东西)。但是您是否有复合材料上允许的属性的“固定”列表,或者您可以定义它们(对代码完成也有好处)。如果不是,我认为您需要添加一个自定义组件类:docs.oracle.com/javaee/7/tutorial/jsf-custom004.htm#BNAVU。如果可以的话,我想我有一个“解决方案”
  • 这里的目的是处理任何 passthrough 属性(例如,很多 data- 用于 jQuery 行为)。即使使用组件 java 类,我也没有在谷歌上找到任何有用的东西。我很想知道你发现了什么:)
  • 是的...但是也许添加检查哪些是通过...但是明确定义组件上的属性(没有数据前缀)也很好,因此您可以完成代码.. . 在您的 ide 和自定义组件中,您可以通过 f:passThroughAttribute 标签添加所有它们,并为它们添加 data- 前缀。 (也有一个f:passTroughAttributes(复数),但我在任何地方都没有找到cc.passThroughAttrs

标签: jsf jsf-2.2 composite-component passthrough-attributes


【解决方案1】:

因此,在@Kukeltje cmets 之后,我设法使用自定义复合组件支持类做一些事情:

@FacesComponent("PassThrough")
public class PassThroughComponent extends Component
{
    @Override
    public void encodeBegin(FacesContext facesContext) throws IOException
    {
        super.encodeBegin(facesContext);
        findComponent("pass").getPassThroughAttributes().putAll(getPassThroughAttributes());
    }
}

在我的组件中使用带有 id='pass' 的 DOM 元素,我可以将属性传递给它。

唉,Eclipse 不理解这个技巧,并触发警告看到所有这些“未知属性 'a:something'”。

我真的很想知道 Primefaces 组件如何设法避免这种情况。

【讨论】:

  • PrimeFaces 组件是真实组件而不是复合组件。这就是为什么我说要明确定义它们,如果你的命名空间对复合属性有好处,eclipse 不应该抱怨
【解决方案2】:

我认为 JSF API 缺少一个帮助器。可能类似于“insertPassthroughAttributes”,类似于 cc:insertChildren。 也许 OmniFaces 可以对此进行原型设计,我们可以将其添加到下一个 JSF 规范中。应该有人创建规范问题。

【讨论】:

  • 这更像是一个评论而不是一个答案;-P。在omnifaces 页面上,他们有一个“投票”,您可以输入JavaEE 9 ;-)
  • 是的,我应该把它添加为评论,对不起:D
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-11-01
  • 2019-12-17
  • 1970-01-01
  • 2020-02-04
  • 1970-01-01
  • 1970-01-01
  • 2011-12-17
相关资源
最近更新 更多