我需要在Java支持bean返回图像后自动调用JS
大小调整功能。这是负责图像加载的xhtml
部分。它是dataGrid
的一部分。
<p:panel id="panel1" >
<p:graphicImage id="image1" value="#{imageService.image}" width="100%" height="100%">
<f:param name="id" value="#{kprod.id}" />
</p:graphicImage>
</p:panel>
这是bean方法:
public StreamedContent getImage() throws IOException {
FacesContext context = FacesContext.getCurrentInstance();
if (context.getCurrentPhaseId() == PhaseId.RENDER_RESPONSE) {
return new DefaultStreamedContent();
}
else {
String id = context.getExternalContext().getRequestParameterMap().get("id");
return findImage(Long.valueOf(id));
}
}
完成渲染阶段后,转到返回实际图像的else分支。
我有正在使用的JS
功能resizeImage()
。我通过将属性onclick="resizeImage()"
添加到<p:graphicImage>
进行了测试,然后单击图像,并正确调整了尺寸。我现在需要的是自动调用此函数。<p:graphicImage>
没有我可以使用的任何属性。我也尝试过<p:ajax oncomplete="resizeImage()" />
涉及到<p:graphicImage>
节点,但是每次我使用ajax
时都会返回此错误:
无法确定事件属性:null
我还有一个子问题。最终HTML
中的实际图像ID为contentForm:kprods:7:image1
,其中编号根据顺序可变。有没有一种方法可以将元素的JS
id传递为参数?
非常感谢。
我找到了解决办法。基本上,我所需要的只是进入onload函数。但是primefaces元素<p:graphicImage>
没有这样的属性。
因此我将我的传递属性添加到我的xhtml新命名空间中:
xmlns:x="http://xmlns.jcp.org/jsf/passthrough"
。
并将x:onload
属性添加到graphicImage元素:
<p:graphicImage id="image1" value="#{imageService.image}" x:onload="resizeImage(this.id)" width="100%" height="100%">