0

I have two files [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 (On the page with the details he wants to go by downloading the parameter id of the address - the ability to send someone a link)

<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");
            }
        }
    }
}

In index.xhtml I'd like to display images in a loop. When a user clicks on a picture, I would like to redirect to a page with details of the selected image. Id must be passed in parameter - will allow it to send the link to a friend.

user3128303
  • 747
  • 1
  • 11
  • 26
  • @BalusC, I got there id null – user3128303 Apr 19 '16 at 19:03
  • Your code is indeed not correct. It's not the same as shown in the answer. – BalusC Apr 19 '16 at 19:12
  • @BalusC, Topic updated above. – user3128303 Apr 19 '16 at 19:32
  • Your code is not the same a shown in the answer. You aren't passing the image ID as f:param. – BalusC Apr 19 '16 at 19:33
  • Id needs to get from the url as I wrote in the subject. (eg. localhost:8080/myApp?id=4) – user3128303 Apr 19 '16 at 19:37
  • I mean the point b with my questions. – user3128303 Apr 19 '16 at 19:40
  • Your question is now confusing. Please do not duplicate code, but update existing code. The stack trace also doesn't seem to match updated code. – BalusC Apr 19 '16 at 19:43
  • Ok, give me a moment – user3128303 Apr 19 '16 at 19:45
  • I hope you now better describe my problem – user3128303 Apr 19 '16 at 20:20
  • You are still not passing the image ID via `` to `` in `details.xhtml`. Add a `` to it. Here's a better explanation of how `` really works: http://stackoverflow.com/a/12452144 (this is also linked in bottom of the duplicate) – BalusC Apr 19 '16 at 20:22
  • In file ItemBean in line with image = new DefaultStreamedContent(new ByteArrayInputStream(wardrobeService.getItemById(id).getPhoto1()));.. id is null ... – user3128303 Apr 19 '16 at 20:47
  • Can I send you a project that you check what I'm doing wrong? – user3128303 Apr 19 '16 at 20:48
  • Your comment is confusing. The problem is already in index.xhtml? I was already not sure as you're using the same ID for every image (so basically every iteration would show the same image with ID `idFromViewProperty`). In the line pointed out in your comment, the `id` can't be null as the `Long.parseLong()` never returns `null` and would throw NPE. I recommend to put the project aside and create a scratchpad project and write code exactly following the answer in the duplicate, play around with it in order to grasp the basic concepts and then apply the lessons learnt to the orignial project. – BalusC Apr 19 '16 at 20:52

0 Answers0