gpt4 book ai didi

java - 如何在 JSF 2 中指定 body id 属性?

转载 作者:行者123 更新时间:2023-11-30 07:26:16 26 4
gpt4 key购买 nike

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

那么,我该如何指定 id JSF 2 中 html 主体的属性?

最佳答案

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

与此同时,假设您使用的是 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>

关于java - 如何在 JSF 2 中指定 body id 属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10451424/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com