p:graphicImage is not working inside h:dataTable/p:dataTable

UI Components for JSF
ccastro333
Posts: 65
Joined: 27 Feb 2011, 18:04

09 Jul 2011, 18:37

I keep on trying to solve this... Please, take a look to my code, because it is pretty similiar to others' working in this topic. Maybe there's a problem because a container? The image is to be displayed within a tab
----------------------------------------------------------------------------
primefaces-3.4
mojarra-2.1.2-FCS
Tomcat 7

User avatar
undermensch
Posts: 140
Joined: 08 Jul 2010, 14:37
Location: Florida, United States

10 Jul 2011, 02:57

What is the scope of your backing bean?
PrimeFaces-11.0.6 / Wildfly 24

ccastro333
Posts: 65
Joined: 27 Feb 2011, 18:04

10 Jul 2011, 18:45

undermensch,
it's view scoped, but I have also tried sessionscoped, without success.
----------------------------------------------------------------------------
primefaces-3.4
mojarra-2.1.2-FCS
Tomcat 7

User avatar
undermensch
Posts: 140
Joined: 08 Jul 2010, 14:37
Location: Florida, United States

11 Jul 2011, 14:07

I have gotten this to work, both in a datatable and in a dynamically updated panel with two modifications, 1 to the graphicimage renderer, the other to the PrimeResourceHandler. I don't intend to use these changes long term, but until I have more time, I'm using them. Here they are:

GraphicImageRenderer

Code: Select all

...
protected String getImageSrc(FacesContext context, GraphicImage image) {
		String src = null;
		Object value = image.getValue();

		if(value == null) {
            src = "";
        }
        else {
            if(value instanceof StreamedContent) {
                StreamedContent streamedContent = (StreamedContent) value;
                Resource resource = context.getApplication().getResourceHandler().createResource("dynamiccontent", "primefaces", streamedContent.getContentType());
                String resourcePath = resource.getRequestPath();
                String rid = createUniqueContentId(context);
                StringBuilder builder = new StringBuilder(resourcePath);

                builder.append("&").append(PrimeResourceHandler.DYNAMIC_CONTENT_PARAM).append("=").append(rid);

                for(UIComponent kid : image.getChildren()) {
                    if(kid instanceof UIParameter) {
                        UIParameter param = (UIParameter) kid;

                        builder.append("&").append(param.getName()).append("=").append(param.getValue());
                    }
                }

                src = builder.toString();                
                context.getExternalContext().getSessionMap().put(rid, value);
                //context.getExternalContext().getSessionMap().put(rid, image.getValueExpression("value").getExpressionString());
            }
            else { ...
PrimeResourceHandler

Code: Select all

...
public void handleResourceRequest(FacesContext context) throws IOException {
        Map<String, String> params = context.getExternalContext().getRequestParameterMap();
        String library = params.get("ln");
        String dynamicContentId = params.get(DYNAMIC_CONTENT_PARAM);
        //logger.log(Level.INFO, "library set to: " + library);
        if (dynamicContentId != null && library != null && library.equals("primefaces")) {

            Map<String, Object> session = context.getExternalContext().getSessionMap();

            try {

                Object dynamicContentEL = (Object) session.get(dynamicContentId);
                //String dynamicContentEL = (String) session.get(dynamicContentId);               

                //ELContext eLContext = context.getELContext();
                //ValueExpression ve = context.getApplication().getExpressionFactory().createValueExpression(context.getELContext(), dynamicContentEL, StreamedContent.class);

                //StreamedContent content = (StreamedContent) ve.getValue(eLContext);
                StreamedContent content = (StreamedContent) dynamicContentEL;
                HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();

                response.setContentType(content.getContentType());

                byte[] buffer = new byte[2048];
...
Good Luck!
PrimeFaces-11.0.6 / Wildfly 24

ccastro333
Posts: 65
Joined: 27 Feb 2011, 18:04

11 Jul 2011, 18:17

Uf, I will try... but this is rather tricky for me (a newbie with PF, and also JSF). May I then consider there's a bug with graphicImage as a Streamedcontent?
----------------------------------------------------------------------------
primefaces-3.4
mojarra-2.1.2-FCS
Tomcat 7

Laabidi Raissi
Posts: 8
Joined: 12 Aug 2011, 16:15

12 Aug 2011, 16:54

Hello everyone,
Are there any news for the ccastro333's case ?
I am trying to send a param to backing bean from graphicImage, but it always returns null:

<p:dataTable value="#{myBean.items}" var="item">
<p:graphicImage value="#{myBean.streamedContent}">
<f:param name="param_name" value="item.itemId"/>
</p:graphicImage>
</p:dataTable>
---------
when trying to retrieve it through:
String image_id = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("param_name");

It always evaluates to null.
Any help?
Eclipse Kepler, WAS 8.5.5, Java EE 6, PrimeFaces 4.0, Mojarra 2.2.5 (JSF 2.2)

ccastro333
Posts: 65
Joined: 27 Feb 2011, 18:04

12 Aug 2011, 18:55

Hi,

I used a workaround. I didn't use param, but attribute. The only problem with that is you have to harcode the name of the component in getStreamedImage method. I think the natural way of doing this is with param, but...

xhtml (within a dataGrid)

Code: Select all

<p:graphicImage  id="itemImgComp"  value="#{itemsBean.streamedImage}" rendered="#{not empty item.defaultImage}">
	<f:attribute name="item_id" value="#{item.id}"/>
</p:graphicImage>
Bean

Code: Select all

public StreamedContent getStreamedImage() {
		
		byte[] emptyImage = new byte[0];
		
		StreamedContent streamedImage = null;

		UIComponent component = FacesContext.getCurrentInstance().getViewRoot().findComponent("itemsForm:itemList:itemImgComp");
		Map attributes = component.getAttributes();
		Integer item_id = (Integer)attributes.get("item_id");
		
		if(item_id != null && itemImages != null){
			if(itemImages.get(item_id.toString()) != null){
				streamedImage = new DefaultStreamedContent(
						new ByteArrayInputStream(((ItemImageView)itemImages.get(item_id.toString())).getImage()), "image/jpg");
			}
		}else{
			streamedImage = new DefaultStreamedContent(new ByteArrayInputStream(emptyImage), "image/jpg");
		}
		
		return streamedImage;
	}
IitemImages is a List containing ItemImageView, a wrapper for the actual image.

And, for the second part of my case, I managed to get the images rendered only by applying the "tunning" suggested a couple posts above. Another workarround that I don't like, and I suspect I am getting collateral error while PF has to find client Ids in certain situations. But images render.
----------------------------------------------------------------------------
primefaces-3.4
mojarra-2.1.2-FCS
Tomcat 7

bhakav
Posts: 22
Joined: 17 Oct 2010, 22:50

13 Aug 2011, 20:14

Thanks castro.. I am getting the attribute value now .. the above post you mentioned .. I found the files in the jar but did not find any method . Do I need to create these two methods or modify the existing ones ?

ccastro333
Posts: 65
Joined: 27 Feb 2011, 18:04

13 Aug 2011, 21:02

Hi,
the methods are as undermesch said, and they are in

org\primefaces\application\PrimeResourceHandler.java
and
org\primefaces\component\graphicimage\GraphicImageRenderer.java
at least, in 3.0.M2

you can simply copy and paste the methods as they are in the post
----------------------------------------------------------------------------
primefaces-3.4
mojarra-2.1.2-FCS
Tomcat 7

bhakav
Posts: 22
Joined: 17 Oct 2010, 22:50

14 Aug 2011, 08:17

Thanks Castro .. I was using 3.01M .I got the files made all the changes does not work .. another unusual thing I saw that method<p:p:graphicImage called first 2 then 3 times for an array of two values .
Last edited by bhakav on 20 Aug 2011, 21:18, edited 1 time in total.

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 20 guests