我有一个UI组件目的。我想获取该组件在运行时生成的 HTML 代码,以便我可以对其进行分析。
有办法实现这一点吗?
我正在尝试使用 JsfUnit 创建自动化测试。我可以从测试方法中获取 UICompoment 对象。但是,我找不到检查该组件生成的 Html 的方法。
只需执行 JSF 在幕后所做的相同操作:调用UIComponent#encodeAll()。要捕获输出,请将响应编写器设置为本地缓冲区:FacesContext#setResponseWriter().
例如。 (假设您处于调用应用程序阶段;处于渲染响应阶段时,需要以不同的方式完成此操作):
FacesContext context = FacesContext.getCurrentInstance();
ResponseWriter originalWriter = context.getResponseWriter();
StringWriter writer = new StringWriter();
try {
context.setResponseWriter(context.getRenderKit().createResponseWriter(writer, "text/html", "UTF-8"));
component.encodeAll(context);
} finally {
if (originalWriter != null) {
context.setResponseWriter(originalWriter);
}
}
String output = writer.toString();
// ...
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)