Dynamic graphic image from datalist var

UI Components for JSF
Post Reply
tales
Posts: 2
Joined: 18 Jan 2011, 17:51

18 Jan 2011, 18:04

Is it possible?

Code: Select all

<p:dataList value="#{searchMB.matching}" var="item"
		paginator="true" rows="10" effectSpeed="fast"
		paginatorTemplate="{PreviousPageLink} {CurrentPageReport} {NextPageLink} {RowsPerPageDropdown}"
		rowsPerPageTemplate="10,25,50,100">
	<p:graphicImage value="#{item.photo}"/>
	${item.name}: ${item.gender} ${item.hairColor}
	${item.country} ${item.majorLevel}
</p:dataList>

Code: Select all

public class SearchMB {

	private List<DataListItem> data;

	public List<DataListItem> getMatching() {
		return data;
	}

	public class DataListItem {

		private final CloserProfile profile;
		private final StreamedContent photo;

		private DataListItem(CloserProfile profile) {
			this.profile = profile;
			if (profile.getPhoto() == null) {
				photo = null;
			} else {
				photo = new DefaultStreamedContent(
						new ByteArrayInputStream(profile.getPhoto()), profile.getPhotoType());
			}
		}

		public Object getPhoto() {
			if(photo == null) {
				return "/resources/no_photo.png";
			}
			return photo;
		}
	...
	}
...
}
I tried it this way, but the streamed image is always broken. It's URL is like .../search.xhtml?primefacesDynamicContent=item.photo
PrimeFaces 2.2RC2
JSF 2.0
Mojarra 2.0.2
Glassfish v3

cagatay.civici
Prime
Posts: 18616
Joined: 05 Jan 2009, 00:21
Location: Cybertron
Contact:

18 Jan 2011, 18:23

You need to place an identifier to graphicImage request using f:param and load the corresponding image according to parameter provided. This is the way to handle dynamic graphic images inside data iteration.

tales
Posts: 2
Joined: 18 Jan 2011, 17:51

19 Jan 2011, 15:21

Thanks for the reply, but could you tell me how exactly to do it? I've been trying it for some time now, and reading tutorials and examples. I got this:

Code: Select all

<p:dataTable value="#{searchMB.matching}" var="item"
			paginator="true" rows="10"
			paginatorTemplate="{PreviousPageLink} {CurrentPageReport} {NextPageLink} {RowsPerPageDropdown}"
			rowsPerPageTemplate="10,25,50,100">
		<p:column>
			<f:facet name="header">
				Foto
			</f:facet>
			<p:graphicImage value="#{searchMB.photo}">
				<f:param id="photo" name="photo" value="${item.id}"/>
			</p:graphicImage>
		</p:column>
	...
</p:dataTable>

Code: Select all

public Object getPhoto() {
	String id = (String) FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("photo");
	CloserProfile profile = closerProfileFacade.find(Integer.parseInt(id));
	if (profile.getPhoto() == null) {
		return "/resources/no_photo.png";
	} else {
		return new DefaultStreamedContent(
				new ByteArrayInputStream(profile.getPhoto()), profile.getPhotoType());
	}
}
I have no idea if I can make it work. Should I ask this somewhere else, could you tell me where, or recommend me a good tutorial? Thanks in advance.
PrimeFaces 2.2RC2
JSF 2.0
Mojarra 2.0.2
Glassfish v3

stepgalvao
Posts: 1
Joined: 27 Apr 2011, 04:45

27 Apr 2011, 04:54

In may application this worked:

list.xhtml

Code: Select all

 <h:form  prependId="false">
                <p:dataGrid var="it" value="#{studentController.allEntity}" columns="3"
                            rows="9" paginator="true" effect="true"
                            paginatorPosition="bottom"
                            paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} "
                            rowsPerPageTemplate="9,12,15">

        <p:column>
            <p:panel  style="text-align:center"  header="#{it.name}">
                
                <p:graphicImage  id="photoImage" title="#{it.photo.absolutePath}" cache="false"
                                    width="60" height="70" value="#{studentController.photo}">
                            <f:param name="photoPath" value="#{it.photo.absolutePath}"/>
                </p:graphicImage>
       .....


Session Bean Method

Code: Select all

public DefaultStreamedContent getPhoto() throws FileNotFoundException{
        String param = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("photoPath");
        if(param==null)
            return  new DefaultStreamedContent(new FileInputStream(Util.getDefaultPhoto()), "image/jpeg");
        else
            return new DefaultStreamedContent(new FileInputStream(new File(param)), "image/jpeg");
}
But, for this application, i think that static image is better than dynamic

User avatar
toJuanito
Posts: 11
Joined: 13 Mar 2013, 19:15

21 Mar 2013, 19:24

Just for the record:

Code: Select all

FacesContext.getCurrentInstance().getExternalContext().getRequestMap().get("item");
item is the var attribute for dataTable, dataGrid or dataList.

:)
Apache 6.0, Primefaces 3.3.1, JSF 2.1, Hibernate.

/**
* Sorry about my english.
*/

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 58 guests