简体   繁体   中英

How to pass ArrayList/lists/iterables argument to a JSF1.2 custom components

I am trying to show a list of images from my backing bean which has a function

public getImgList(){
    return ArrayList<string>imgPathList;
}

Now this List has to be rendered to a equivalent output of images which are displayed side by side. like

[image][image][image]

Which is not possible if I use <h:datatable> , because it will render all of them one per row, And I cant find any method by which I can render them using custom component as I will have to pass the list of images to the custom components. Can anyone please explain how to pass a list to a custom component in JSF 1.2 Mojarra?

Just specify it as attribute.

<my:component value="#{bean.imagePaths}" />

It'll just be available as exactly the same type in the value property of your custom component class with help of UIComponent#getValueExpression() .


Given the fact that you're already asking this trivial question, I think developing the custom component is going to take a lot of time for you. Also, since a lot of solutions already exist, you're basically reinventing the wheel here. I'd suggest to take a different route, namely just using existing tags/components. There are two options:

  • Use JSTL <c:forEach> . This will work if you don't put it inside another iterating component like <h:dataTable> and you aren't rendering input elements in the loop.

     <c:forEach items="#{bean.imagePaths}" var="imagePath"> <img src="#{imagePath}" /> </c:forEach> 
  • Use a 3rd party component library which offers a fullworthy JSF iterating component which doesn't render any HTML. I'd suggest Tomahawk's <t:dataList> .

     <t:dataList value="#{bean.imagePaths}" var="imagePath"> <img src="#{imagePath}" /> </t:dataList> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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