迭代加载图像w @PostConstruct

时间:2016-04-18 21:47:23

标签: jsf primefaces uirepeat graphicimage

我有两个文件[index.xhtml,details.xhtml]

的index.xhtml

...
<c:if test="#{not empty itemBean.listItem}">
    <ui:repeat var="items" value="${itemBean.listItem}">
        <h:link value="#{items.name}" outcome="/views/item/details?faces-redirect=true&amp;id=#{items.idItem}">
            <p:graphicImage value="#{itemBean.image}">
                <f:param name="id" value="#{itemBean.idFromViewProperty}" />
            </p:graphicImage>
        </h:link>
    </ui:repeat>
</c:if>
...

details.xhtml (在页面上有他想要的详细信息,下载地址的参数ID - 向某人发送链接的能力)

<h:form>
    <c:if test="${empty itemDetailsBean.dbImage1}">
    //I have here are different descriptive data about this product
        <div class="img">
            <p:graphicImage styleClass="img" value="#{itemDetailsBean.dbImage1}"/>
        </div>
    //I have here are different descriptive data about this product
    </c:if>
</h:form>

ItemBean

@Named
@ManagedBean
@RequestScoped
public class ItemBean implements Serializable {

    ... //other variable

    private StreamedContent image;

    public StreamedContent getImage() {
        return image;
    }

    @PostConstruct
    public void init() {
        if (FacesContext.getCurrentInstance().getCurrentPhaseId() == PhaseId.RENDER_RESPONSE) {
            image = new DefaultStreamedContent();
        } else {
            image = new DefaultStreamedContent(new ByteArrayInputStream(wardrobeService.getItemById(id).getPhoto1()));
            idFromViewProperty = Long.parseLong(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("id"), 10);
            this.selectedItem = wardrobeService.getItemById(idFromViewProperty);
            if (selectedItem != null && selectedItem.getPhoto1() != null) {
                    byte[] photos = selectedItem.getPhoto1();
                    InputStream dbStream = new ByteArrayInputStream(photos);
                    dbImage1 = new DefaultStreamedContent(dbStream, "image/jpeg");
                }
            }
        }
    }

    @ManagedProperty("#{param.id}")
    private Long id;

    public void setId(Long id) {
        this.id = id;
    }
}

ItemDetailsBean

@Named
@ManagedBean
@RequestScoped
public class ItemDetailsBean implements Serializable {

    @Inject
    ItemService itemService;
    @Inject
    AuthBean authBean;

    private Item selectedItem;
    private StreamedContent dbImage1;


    public StreamedContent getDbImage1() {
        return dbImage1;
    }

    public void setDbImage1(StreamedContent dbImage1) {
        this.dbImage1 = dbImage1;
    }

    @ManagedProperty("#{param.id}")
    private Long id;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @PostConstruct
    public void init() {
        if (FacesContext.getCurrentInstance().getCurrentPhaseId() == PhaseId.RENDER_RESPONSE) {
            dbImage1 = new DefaultStreamedContent();
        } else {
            this.selectedItem = itemService.getItemById(id);
            if (selectedItem != null && selectedItem.getPhoto1() != null) {
                byte[] photos = selectedItem.getPhoto1();
                this.dbImage1 = new DefaultStreamedContent(new ByteArrayInputStream(photos), "image/jpeg");
            }
        }
    }
}

在index.xhtml中,我想在循环中显示图像。当用户点击图片时,我想重定向到包含所选图片详细信息的页面。 必须在参数中传递ID - 允许其将链接发送给朋友。

0 个答案:

没有答案