简体   繁体   English

如何显示替代文字 <p:graphicImage> 在PrimeFaces?

[英]How to display alternate text on <p:graphicImage> in PrimeFaces?

I'm displaying images on <p:graphicImage> which are stored under the webroot\\resources\\fileUpload\\product_image\\thumb directory as follows. 我在<p:graphicImage>上显示图像,这些图像存储在webroot\\resources\\fileUpload\\product_image\\thumb目录下,如下所示。

<p:graphicImage library="fileUpload" 
                name="#{not empty row.prodImage?'product_image/thumb/':'&nbsp;'}#{row.prodImage}" 
                alt="#{row.prodName}"/>

row refers to a JPA entity object stored in a java.util.List<Entity> which is retrieved while iterating over a <p:dataTable> . row指的是存储在java.util.List<Entity>的JPA实体对象,该对象在迭代<p:dataTable>被检索。


When an image name is empty, the name attribute of <p:graphicImage> is evaluated to name=" " . 当图像名称为空时, <p:graphicImage>name属性将被计算为name=" " In which case, it does not work just leaving the page blank without any errors. 在这种情况下,它只是将页面留空而没有任何错误。

If &nbsp; 如果&nbsp; in the conditional expression is removed then, it causes java.lang.StringIndexOutOfBoundsException to be thrown. 然后,在条件表达式中删除它会导致抛出java.lang.StringIndexOutOfBoundsException

So, how to display alternate text on <p:graphicImage> , if the image to display is unavailable? 那么,如果要显示的图像不可用,如何在<p:graphicImage>上显示替代文字?

If &nbsp; 如果&nbsp; in the conditional expression is removed then, it caused java.lang.StringIndexOutOfBoundsException to be thrown 然后,在条件表达式中删除它会导致抛出java.lang.StringIndexOutOfBoundsException

Indeed, reproduced it. 的确,转载它。 That's technically a bug in PrimeFaces' GraphicImageRenderer . 这在技术上是PrimeFaces的GraphicImageRenderer一个错误。 At line 70, it should have done a if(name != null && !name.isEmpty()) instead of alone name != null before trying to get it as a resource. 在第70行,它应该在尝试将其作为资源之前完成if(name != null && !name.isEmpty())而不是单独的name != null

I'm not sure if it's for them worth to fix it as it actually doesn't make sense to specify an empty name and the workaround is relatively easy by conditionally rendering two images, one without the name and another with the name: 我不确定它们是否值得修复它,因为它实际上没有意义指定一个空name并且通过有条件地渲染两个图像(一个没有名称,另一个没有名称)来解决方法相对容易:

<p:graphicImage alt="#{row.prodName}" rendered="#{empty row.prodImage}" />
<p:graphicImage library="fileUpload" name="product_image/thumb/#{row.prodImage}" alt="#{row.prodName}" rendered="#{not empty row.prodImage}" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM