I am trying to display images from database and some entities can have many images some only one , so to make it easy I want to let user change pictures with button.
Here is code:
<ui:composition template="layout.xhtml">
<ui:define name="content">
<h:form id="form">
#{flatJsfService.loadData(0,10)}
<ui:repeat var="flat" value="#{flatJsfService.getListOfFlats()}" varStatus="status" >
<div class="container-fluid" >
<div class="row">
<div class="col-sm-4">
<p:imageSwitch effect="wipe" widgetVar="switcher" id="manuelSwitcher"
slideshowAuto="false">
<ui:repeat value="#{fileJsfService.getImages(flat.id)}" var="idd">
<p:graphicImage value="#{fileJsfService.image}" style="width: 100%;height: auto;">
<f:param name="fileId" id="fileId" value="#{idd}" />
</p:graphicImage>
</ui:repeat>
</p:imageSwitch>
<p:commandButton type="button" onclick="PF('switcher').previous();"
icon="pi pi-caret-left" id="prev" style="margin-bottom: 5px"/>
<p:commandButton type="button" onclick="PF('switcher').next();"
icon="pi pi-caret-right" id="next" style="margin-bottom: 5px"/>
</div>
<div class="col-sm-8 ">
some info
</div>
</div>
</div>
</ui:repeat>
</h:form>
</ui:define>
</ui:composition>
I used two ui:repeat , first for get all flats and second all images of flat.
When I display images they are displaying just under previous image, why <p:imageSwitch> doesn't work?
*primefaces version: 6.2.
jsf 2.2.13.
xmlns:p="http://primefaces.org/ui".*
FileJsfService two methods I Am using :
public StreamedContent getImage() throws IOException {
FacesContext context = FacesContext.getCurrentInstance();
if(context.getCurrentPhaseId()==PhaseId.RENDER_RESPONSE){
return new DefaultStreamedContent();
}else{
String flatid = context.getExternalContext().getRequestParameterMap().get("fileId");
File file = fileRepository.findById(Long.valueOf(flatid)).get();
byte[] data=file.getData();
// return new DefaultStreamedContent(new ByteArrayInputStream(data));
return DefaultStreamedContent.builder().contentType(file.getType()).name(file.getFileName())
.stream((SerializableSupplier<InputStream>) new ByteArrayInputStream(data)).build();
}
// So, browser is requesting the image. Return a real StreamedContent with the image
}
public List<String> getImages(long id){
Flat flat = flatRepository.findById(id).get();
List<File> files = flat.getFiles();
List<String> ids = new ArrayList<String>();
for(File file:files)
ids.add(file.getId().toString());
return ids;
}