简体   繁体   English

p:graphicImage和Fancybox集成

[英]p:graphicImage and Fancybox Integration

I'm using JSF 2.2 with PrimeFaces 5.3 under the GlassFish 4.1 我正在使用GlassFish 4.1下的PrimeFaces 5.3和JSF 2.2

Inside the application I use the approach to show the images from the database. 在应用程序内部,我使用这种方法来显示数据库中的图像。 So that means I don't have an URL. 所以这意味着我没有URL。 There're tons of example from this point of view, but I will paste here in order to be more useful. 从这个角度来看,有很多示例,但是我将在此处粘贴以便更加有用。

Here the facelets 这里的小面

<p:graphicImage value="#{applicationScopedBean.imagesFromDb}" class="img">
    <f:param name="imageId" value="#{actualAd.favouriteImageId}" />
    <f:param name="cvlTimeStamp" value="#{now}" />
</p:graphicImage>

And here the Backing Bean 还有这里的后备豆

public StreamedContent getImagesFromDb() {
        FacesContext context = FacesContext.getCurrentInstance();

        if (context.getCurrentPhaseId() == PhaseId.RENDER_RESPONSE) {
            // So, we're rendering the HTML. Return a stub StreamedContent so that it will generate right URL.
            return new DefaultStreamedContent();
        } else {
//             So, browser is requesting the image. Return a real StreamedContent with the image bytes.
            String imageId = context.getExternalContext().getRequestParameterMap().get("imageId");
            return new DefaultStreamedContent(new ByteArrayInputStream(imageManager.getById(Long.valueOf(imageId)).getContent()));
        }
    }

Here an example of a the generated HTML code 这是生成的HTML代码的示例

<img src="/myWebApp/faces/javax.faces.resource/dynamiccontent.properties?ln=primefaces&amp;v=5.3&amp;pfdrid=GgwXKejrBbRvnC%2Fxp98FzlaymhDf7Gb%2BEVoD%2BlqKVRmYUBBZeMmjKw%3D%3D&amp;pfdrt=sc&amp;imageId=23&amp;myTimeStamp=Sun+May+15+19%3A19%3A08+CEST+2016&amp;pfdrid_c=true">

By design, in order to use the gallery that comes from fancybox we need a code similar to the following 通过设计,为了使用来自fancybox的图片库,我们需要类似于以下代码

<a class="fancybox" rel="group" href="resources/bootstrap/css/images/single/1.jpg">
   <img id="img_01" alt="" class="raised" src="resources/bootstrap/css/images/single/1.jpg" style="width: 100%" />

But using graphicImages with streams, I don't have the link needed in the href value. 但是,将graphicsImages与流一起使用时,我在href值中没有所需的链接。 There's a chance to retrieve the generated image url? 有机会检索生成的图像网址吗? Basically I need to retrieve the generated string used to fill the src attribute of the img tag. 基本上,我需要检索用于填充img标签的src属性的生成的字符串。

Is it possible to solve the problem? 有可能解决问题吗? Thank you! 谢谢!

The solution is to mark every p:graphicImage and his h:outputLink with a custom id. 解决方案是用自定义ID标记每个p:graphicImage及其h:outputLink。

<c:forEach items="#{myController.images}" var="img" begin="0" class="hidden-xs" varStatus="loop">
  <h:outputLink id="aInsideLightbox#{loop.index+1}" class="fancybox imgCVLGalleryDetail hidden-xs" rel="group">
    <p:graphicImage id="imgInsideLightbox#{loop.index+1}" value="#{applicationScopedBean.imagesFromDb}" class="img">
      <f:param name="imageId" value="#{img.imageWrapper.id}" />
    </p:graphicImage>
  </h:outputLink>                                               
</c:forEach>

and then in the front-end side when the page is ready 然后在页面准备好时在前端

<h:outputScript library="js" name="external.js"/>
<script>
  $(document).ready(function () {
    setUrlInTheFancyboxLinkAndImage();
    setTypeImageInFancybox();
  });
</script>

and in an external .js file the following functions. 并在外部.js文件中具有以下功能。

function setUrlInTheFancyboxLinkAndImage() {
    for (i = 0; i < 20; i++) {
        $imgToImprove = document.getElementById("imgInsideLightbox" + i);
        if ($imgToImprove !== null) {
            $aToImprove = document.getElementById("aInsideLightbox" + i);
            if ($aToImprove !== null) {
                $aToImprove.setAttribute("href", $imgToImprove.getAttribute("src"));
            }
        }
    }
}

function setTypeImageInFancybox() {
    $(".fancybox").fancybox({
        type: 'image',
        openEffect: 'none',
        closeEffect: 'none'
    }); }

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

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