如何渲染<h:outputLink>的自定义属性?

Ser*_*soy 2 jsf facelets pinterest

我正在尝试使用如下所示的片段来实现pinterest的pinit按钮:

<h:outputLink value="http://pinterest.com/pin/create/button/" class="pin-it-button" count-layout="horizontal">
   <f:param name="url" value="#{beanOne.someMethod}/sometext{prettyContext.requestURL.toURL()}"/>
   <f:param name="media" value="#{beanOne.someOtherMethod}/sometext/somemoretext/#{beanTwo.someMethodTwo}-some-text.jpg"/>
   <f:param name="description" value="#{beanTwo.someOtherMethodTwo}"/>
   <img border="0" src="//assets.pinterest.com/images/PinExt.png" title="Pin It" />
</h:outputLink>
Run Code Online (Sandbox Code Playgroud)

这是陷阱:

  • 整个标记是从两个不同bean的四种不同方法的组合以及一些静态文本创建的
  • url参数显然需要urlencoded,因此我在h:outputLink中使用f:param以便它们获得urlencoded
  • 生成的a标签需要具有非标准count-layout="horizontal"属性

现在我的问题是:

  • 如何将count-layout属性注入h:outputLink或生成的锚标记
  • 否则,如果我不能,那么另一种非侵入性(我不想改变bean方法)的方法来完成所需的pinit按钮标记?

所需的标记可以在http://pinterest.com/about/goodies/上找到"网站的pin it按钮"部分.

Bal*_*usC 5

使用普通<a>元素以及委托的自定义EL函数URLEncoder#encode().

<c:set var="url" value="#{beanOne.someMethod}/sometext#{prettyContext.requestURL.toURL()}"/>
<c:set var="media" value="#{beanOne.someOtherMethod}/sometext/somemoretext/#{beanTwo.someMethodTwo}-some-text.jpg"/>
<c:set var="description" value="#{beanTwo.someOtherMethodTwo}"/>

<a href="http://pinterest.com/pin/create/button/?url=#{utils:encodeURL(url)}&amp;media=#{utils:encodeURL(media)}&amp;description=#{utils:encodeURL(description)}" class="pin-it-button" count-layout="horizontal">
   <img border="0" src="//assets.pinterest.com/images/PinExt.png" title="Pin It" />
</a>
Run Code Online (Sandbox Code Playgroud)

(请注意,该class属性无效<h:outputLink>,您应该使用styleClass)

或者创建一个自定义渲染器,<h:outputLink>为其添加count-layout属性支持.假设您正在使用Mojarra,最简单的方法是扩展它OutputLinkRenderer:

public class ExtendedLinkRenderer extends OutputLinkRenderer {

    @Override
    protected void writeCommonLinkAttributes(ResponseWriter writer, UIComponent component) throws IOException {
        super.writeCommonLinkAttributes(writer, component);
        writer.writeAttribute("count-layout", component.getAttributes().get("count-layout"), null);
    }

}
Run Code Online (Sandbox Code Playgroud)

要使其运行,请按以下方式注册faces-config.xml:

<render-kit>
    <renderer>
        <component-family>javax.faces.Output</component-family>
        <renderer-type>javax.faces.Link</renderer-type>
        <renderer-class>com.example.ExtendedLinkRenderer</renderer-class>
    </renderer>
</render-kit>
Run Code Online (Sandbox Code Playgroud)