【问题标题】:How to spefic the body id attribute in JSF 2?如何在 JSF 2 中指定 body id 属性?
【发布时间】:2012-05-04 15:05:05
【问题描述】:

我需要设置html body-tag 的id 属性(用于硒测试)。但是那个 JSF 2 body 标记 (<h:body>) 没有 id 属性。

那么,如何在 JSF 2 中为 html 正文指定 id 属性?

【问题讨论】:

    标签: java jsf jsf-2


    【解决方案1】:

    <h:body> 确实不支持它(当然也令我惊讶;它在 HTML 中完全有效)。我已将其以issue 2409 的形式向 JSF 人员报告。

    同时,假设您使用的是 Mojarra,您可以通过扩展 Mojarra 的BodyRenderer 来解决此问题,如下所示:

    package com.example;
    
    import java.io.IOException;
    
    import javax.faces.component.UIComponent;
    import javax.faces.context.FacesContext;
    
    import com.sun.faces.renderkit.html_basic.BodyRenderer;
    
    public class BodyWithIdRenderer extends BodyRenderer {
    
        @Override
        public void encodeBegin(FacesContext context, UIComponent component) throws IOException {
            super.encodeBegin(context, component);
    
            if (component.getId() != null) {
                context.getResponseWriter().writeAttribute("id", component.getClientId(context), "id");
            }
        }
    
    }
    

    要让它运行,请在faces-config.xml 中按如下方式注册它(不,@FacesRenderer 注释魔法无法覆盖标准渲染器)。

    <render-kit>
        <renderer>
            <component-family>javax.faces.Output</component-family>
            <renderer-type>javax.faces.Body</renderer-type>
            <renderer-class>com.example.BodyWithIdRenderer</renderer-class>
        </renderer>
    </render-kit>
    

    【讨论】:

    • 嗨,它的工作似乎很有吸引力,但是当我在 Eclipse 之外尝试 mvn install 时,我收到以下错误:包 com.sun.faces.renderkit.html_basic 不存在。有什么想法吗?
    • @Alex:抱歉,Maven 超出了我的范围,这与所提出的具体问题无关。按 按钮提出新问题。
    • @Alex:哦,请注意,根据链接的票证,问题 2409 自 Mojarra 2.1.8 以来已修复。您不妨升级它以使用&lt;h:body id&gt; :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-17
    • 2020-01-04
    • 1970-01-01
    相关资源
    最近更新 更多