如何在JSF 2中设置body id属性? [英] How to spefic the body id attribute in JSF 2?
问题描述
我需要设置html body
-tag的 id
属性(用于selenium测试)。但是JSF 2 正文
标记(< h:body>
)没有 id
属性。
I need to set the id
attribute of the html body
-tag (for selenium test). But that JSF 2 body
tag (<h:body>
) has no id
attribute.
那么,如何为html主体指定 id
属性JSF 2?
So, how can I specify the id
attribute for the html body in JSF 2?
推荐答案
< h:body>
确实不支持它(诚然令我惊讶的是,它在HTML中完全有效。我已经向JSF人员报告了 issue 2409 。
同时,假设您正在使用Mojarra,您可以通过扩展Mojarra的 BodyRenderer
来解决这个问题,如下所示:
In the meanwhile, assuming that you're using Mojarra, you could solve this by extending Mojarra's BodyRenderer
as follows:
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
注释魔法赢了'重写标准渲染器。)
To get it to run, register it as follows in faces-config.xml
(no, the @FacesRenderer
annotation magic won't work as to overriding the standard renderers).
<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>
这篇关于如何在JSF 2中设置body id属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!