如何在PrimeFaces dataTable列中使用换行符(漂亮打印)显示XML内容?

如何在PrimeFaces dataTable列中使用换行符(漂亮打印)显示XML内容?

我的XML内容已经采用漂亮的打印格式,我可以在从FacesConverter执行System.out.println时使用换行符看到它,该字符串会在表列上呈现之前将字节转换为字符串。

这是FacesConverter的代码:

@FacesConverter(value="xmlStream") public class ByteConverter implements Converter { @Override public Object getAsObject(FacesContext context, UIComponent component, String value) { byte[] buffer = null; try{ buffer = value.getBytes("UTF-8"); }catch(UnsupportedEncodingException e){ buffer = value.getBytes(); } return buffer; } @Override public String getAsString(FacesContext context, UIComponent component, Object value) { String text = null; if (value instanceof byte[]){ try{ text = new String((byte[])value, "UTF-8"); }catch(UnsupportedEncodingException e){ text = new String((byte[])value); } } System.out.println("text="); return text; } } 

这是我在上面做System.out.println时在控制台中看到的内容:

  eth1  1 42  foo bar2    2 4668  foo bar2    

但是当数据显示在PF dataTable列上时,它显示为没有换行符的单行字符串 – 有没有办法在控制台上显示它? 其他人如何在PF表中显示XML内容?

嗯,看起来这就是诀窍:

 .whiteSpaceClass{ white-space: pre-wrap; } 

但是现在我不得不处理跨越一半页面的行,因为XML太大了。 我想这句古老的说法’小心你要求的’适用于这里…… 🙂

尽管如此,只显示前几行并能够基于单击事件展开/折叠其余部分,甚至在双击单元格时将其显示在框中会很好 – 问题是,我不知道如何要做到这一点,或PF是否支持… 🙂

您可以使用可扩展行来显示整个XML。 请参阅showcase中使用p:rowExpansion的示例。