[英]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/':' '}#{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
如果
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
如果
in the conditional expression is removed then, it causedjava.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.