本文介绍了在 JSF2 h:inputText 组件中使用 bootstrap 相关标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有在 JSF2 组件中使用 bootstrap 相关的标签?例如,我对使用 bootstrap typeahead 功能感兴趣,它需要像

Is there anyway to use bootstrap related tags in JSF2 components? For example I'm interested in using the bootstrap typeahead feature which requires something like

<h:inputText id="typeahead" type="text" data-provide="typeahead"></h:inputText>

但由于 h:inputText 不存在 data-provide 它被剥离了,所以 typeahead 功能显然不起作用.

but since data-provide doesn't exist for h:inputText it gets stripped out and so the typeahead feature would obviously not work.

推荐答案

取决于您使用的 JSF 版本.

Depends on JSF version you're using.

JSF 2.0/2.1 中,无法指定其他属性.JSF HTML 呈现器将只呈现预定义的属性.您需要创建一个自定义渲染器来实现所需的工作.为了最小化样板代码,您不得不扩展特定于实现的渲染器.不清楚您使用的是哪一个,所以这里只是一个针对 Mojarra 的示例:

In JSF 2.0/2.1, it's not possible to specify additional attributes. The JSF HTML renderers will only render predefined attributes. You'd need to create a custom renderer to achieve the desired job. To minimize boilerplate code, you're forced to extend the implementation-specific renderer. It's unclear which one you're using, so here's just a Mojarra targeted example:

import com.sun.faces.renderkit.html_basic.TextRenderer;

public class MyTextRenderer extends TextRenderer {

    @Override
    protected void getEndTextToRender(FacesContext context, UIComponent component, String currentValue) throws IOException {
        Object dataProvide = component.getAttributes().get("data-provide");

        if (dataProvide != null) {
            context.getResponseWriter().writeAttribute("data-provide", dataProvide, null);
        }

        super.getEndTextToRender(context, component, currentValue);
    }

}

faces-config.xml中按如下方式注册以使其运行:

Register it as follows in faces-config.xml to get it to run:

<render-kit>
    <renderer>
        <component-family>javax.faces.Input</component-family>
        <renderer-type>javax.faces.Text</renderer-type>
        <renderer-class>com.example.MyTextRenderer</renderer-class>
    </renderer>
</render-kit>

JSF 2.2 中,可以通过新的 passthrough 命名空间或 标记来实现.另请参阅 JSF 2.2 中有哪些新内容?- HTML5 直通属性.


In JSF 2.2, it's possible by the new passthrough namespace or the <f:passThroughAttribute> tag. See also What's new in JSF 2.2? - HTML5 Pass-through attributes.

因此:

<html ... xmlns:a="http://xmlns.jcp.org/jsf/passthrough">
...
<h:inputText id="typeahead" a:data-provide="typeahead" />

(注意type 属性已经默认为text)

(note that the type attribute defaults to text already)

或者:

<h:inputText id="typeahead">
    <f:passThroughAttribute name="data-provide" value="typeahead" />
</h:inputText>

另见:

  • JSF 不呈现自定义 HTML 标记属性
  • 这篇关于在 JSF2 h:inputText 组件中使用 bootstrap 相关标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 06:45