Why can't I update a p:graphicImage twice after upload a file with p:uploadFile - primefaces

I have been trying to upload an image with a p:uploadFile and it work fine at first time, but after the first uploaded file it not update the p:graphicImage, but if I reload the page the value of the graphicImage is fine and the image is showed. So, I think that is a problem with the primefaces uploadFile component, but I not sure.
My xhtml is this:
<h:form id="formNuevo" size="150%" enctype="multipart/form-data">
<h:outputLabel value="DiseƱo Neumatico: *" />
<p:fileUpload fileUploadListener="#{serviciosVentanaDiseno.manejarUploadedFile}" mode="advanced" auto="true" sizeLimit="9000000000" allowTypes="/(\.|\/)(gif|jpe?g|png)$/" update="mensajes graficImage"/>
<p:graphicImage id="graficImage" styleClass="disenoNeumatico" value="#{serviciosVentanaDiseno.streamedContentImagen}" ajax="true"/>
</h:form>
and my bean is a #SessionScoped bean, it's here:
public void manejarUploadedFile(FileUploadEvent event) {
UploadedFile uploadedFile = (UploadedFile)event.getFile();
try {
diseno.setImagen(uploadedFile.getContents());
streamedContentImagen = new DefaultStreamedContent(new ByteArrayInputStream(diseno.getImagen()));
} catch (IOException e) {
//log error
}
}
public StreamedContent getStreamedContentImagen() {
return streamedContentImagen;
}
public void setStreamedContentImagen(StreamedContent streamedContentImagen) {
this.streamedContentImagen = streamedContentImagen;
}

Try setting the cache attribute of the graphicImage component to false:
<p:graphicImage cache="false"

change scope #SessionScoped then <p:graphicImage cache="false">

Related

How to set a converter dynamically for a PrimeFaces component?

I am using p:autoComplete inside column of a data table. And I am setting
converter by using variable assigned to the data table converter="#{fieldVar.possibleValues}"
but, it throws :
converter="#{fieldVar.possibleValues}": java.lang.IllegalArgumentException: Cannot convert userConverter of type class java.lang.String to interface javax.faces.convert.Converter
possibleValues is a string
If this is not possible then how we can change the converter dynamically.
<p:dataTable var="fieldVar" value="#{dynamicReportBean.screenMetadataDTOs}" id="edit_meta_data_datatable">
<p:column headerText="Filter Names">
<h:outputText value="#{fieldVar.filedName}"/>
</p:column>
<p:column headerText="Filter Values">
<p:inputText value ="#{fieldVar.filedValue}"
placeholder="#{fieldVar.filedName}"
rendered="#{dynamicReportBean.canRenderFieldType(fieldVar.fieldId, 'TEXTFIELD')}" style="width: 220px">
</p:inputText>
<p:selectOneMenu value="#{fieldVar.filedValue}"
rendered="#{dynamicReportBean.canRenderFieldType(fieldVar.fieldId, 'DROPDOWN')}" style="width: 220px">
<f:selectItems value="#{dynamicReportBean.retrievePossibleDropDownValues(fieldVar.fieldId, fieldVar.fieldType, fieldVar.possibleValues)}"/>
</p:selectOneMenu>
<p:selectManyMenu value="#{fieldVar.filedValue}"
style="width: 220px"
showCheckbox="true"
rendered="#{dynamicReportBean.canRenderFieldType(fieldVar.fieldId, 'SELECT_MANY_CHECKBOX')}">
<f:selectItems value="#{dynamicReportBean.retrievePossibleDropDownValues(fieldVar.fieldId, fieldVar.fieldType, fieldVar.possibleValues)}"/>
</p:selectManyMenu>
<p:autoComplete value="#{fieldVar.filedValue}"
style="width: 220px"
placeholder="autocomplete"
completeMethod="#{dynamicReportBean.getAutoCompleteMethod}"
rendered="#{dynamicReportBean.canRenderFieldType(fieldVar.fieldId, 'AUTOCOMPLETE')}"
scrollHeight="300"
converter="#{fieldVar.possibleValues}"
size="25">
<f:attribute name="converterName" value="#{fieldVar.possibleValues}" />
<p:ajax event="itemSelect" listener="#{dynamicReportBean.onHandleSelect}"/>
</p:autoComplete>
</p:column>
</p:dataTable>
if I pass converter = "userConverter" It works fine but instead of that I want converter values come dynamically
public class UserConverter implements Converter {
#Override
public Object getAsObject(FacesContext context, UIComponent component,
String value) {
UserLOVBean lOVBean = null;
try {
UserService service = new UserServiceImpl();
UserFilterDTO filterDTO = new UserFilterDTO();
UserContext userContext
= (UserContext) context.getExternalContext().getSessionMap().
get("USER_CONTEXT");
filterDTO.setCmpCode(userContext.getCmpCode());
filterDTO.setOrgCode(userContext.getOrgCode());
if (value != null) {
filterDTO.setUserId(value);
}
for (UserLOVDTO lOVDTO : service.retrieveUserForAutoComplete(
filterDTO)) {
if (value.equals(lOVDTO.getUserId())) {
lOVBean = new UserLOVBean();
CopyUtil.copyValues(lOVDTO, lOVBean);
}
}
} catch (Exception ex) {
ex.printStackTrace();
Logger.getLogger(UserConverter.class.getName()).log(Level.SEVERE,
null, ex);
}
return lOVBean;
}
#Override
public String getAsString(FacesContext context, UIComponent component,
Object value) {
return String.valueOf(((UserLOVBean) value).getUserId());
}
}
Setting a converter for p:autoComplete (and any other PrimeFaces component) using the converter attribute is documented as:
An EL expression or a literal text that defines a converter for the component. When it's an EL expression, it's resolved to a converter instance. In case it's a static text, it must refer to a converter id.
So, if you want to use an EL expression, it must point to an instance of a converter. In your bean you could simply add:
public UserConverter getUserConverter() {
return new UserConverter();
}
And then, in your XHTML, use:
<p:anyComponent converter="#{myBean.userConverter}"/>

Dynamic images with StreamedContent

I have implemented two StreamedContent beans to dynamic load a graphic image, following the solution found
here
1) With the first one I display images in a contentFlow (EVERYTHING OK)
<p:contentFlow value="#{imageBean.images}" var="image">
<p:graphicImage value="#{imageStreamer.fileContent}" styleClass="content" cache="false">
<f:param name="index" value="#{image.index}"/>
</p:graphicImage>
</p:contentFlow>
2) With the second one I try to display images using a foreach (or repeat):
<c:forEach items="#{imageBean.images}" var="item" varStatus="varStatus">
<p:graphicImage value="#{imageStreamer.fileContent}" cache="false">
<f:param name="index" value="#{varStatus.index}" />
</p:graphicImage>
</c:forEach>
This doesn't work. The check (context.getCurrentPhaseId() == PhaseId.RENDER_RESPONSE) is always true.
If I change the scope of the Bean to RequestScoped than it works! I'm confused... can someone help me?
Managed bean:
#ManagedBean
#ApplicationScoped
public class ImageStreamer{
public ImageStreamer(){}
public StreamedContent getFileContent(){
List<Link> links = this.getLinkItems();
ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
FacesContext fc = FacesContext.getCurrentInstance();
String linkIndex = externalContext.getRequestParameterMap().get("index");
if(fc.getCurrentPhaseId() == PhaseId.RENDER_RESPONSE){
return new DefaultStreamedContent();
}else{
int parsedIndex = Integer.parseInt(linkIndex);
Link ql = links.get(parsedIndex);
return new DefaultStreamedContent(new ByteArrayInputStream(ql.getBytes()), "image/png");
}
}
}
Since you are using primefaces, try using p:repeat
Repeat is an extension to standard repeat component to provide interoperability between JSF implementations and PrimeFaces components.
https://www.primefaces.org/showcase/ui/data/repeat.xhtml

Call backing bean from p:inputText ondblclick

I need to do a double click on a p:inputText inside a p:dataTable so that it will bring me to another page and launch the details. Is it possible to call backing bean method from Primefaces p:inputText ondblclick?
p:inputText is a normal input in the end, you can use jQuery to register ondblclick on it, then you can call a p:remoteCommand to reach your bean.
I assume you would have multiple inpuText since you have a dataTable.
xhtml
<p:dataTable var="car" value="#{bean.cars}">
<p:column headerText="Model">
<p:inputText value="#{car.model}" styleClass="dbClickInput" />
</p:column>
</p:dataTable>
<p:remoteCommand name="callDetailsCommand"
actionListener="#{bean.callDetails()}" />
<script>
$(document).ready(function() {
$('.dbClickInput').dblclick(
function() {
callDetailsCommand([{name: 'carModelValue', value: $(this).val()}]);
}
);
});
</script>
Bean
public void callDetails() {
FacesContext context = FacesContext.getCurrentInstance();
Map map = context.getExternalContext().getRequestParameterMap();
String carModelValue = (String) map.get("carModelValue");
}
Hope this helps.

PrimeFaces <f:attribute not working correctly with p:galleria attribute

I have issue when trying to make command link from inside p:galleria component
The problem is despite the fact at run time the link value value="Show present #{present.name} #{present.presentId}" contains the correct value of the id as example value="Show present Foo 1" , when pressing the command link it sends the wrong id of the second object every time
<h:form>
<p:galleria value="#{presentBean.allPresentList}" var="present" panelWidth="500" panelHeight="313" showCaption="true">
<f:facet name="content">
<h:commandLink value="Show present #{present.name} #{present.presentId}" action="pretty:present" actionListener="#{presentBean.setPresentObj}">
<f:attribute name="present" value="#{present.presentId}"/>
</h:commandLink>
</f:facet>
</p:galleria>
</h:form>
#ManagedBean(name="presentBean")
#SessionScoped
public class PresentBean implements Serializable{
ArrayList<Present> allUserPresentList = new ArrayList<Present>();
#PostConstruct
private void usersPresent(){
PresentDao presentDao = new PresentDaoImpl();
allPresentList = (ArrayList<Present>) presentDao.findAllPresents();
}
public ArrayList<Present> getAllUserPresentList() {
return allUserPresentList;
}
public void setAllUserPresentList(ArrayList<Present> allUserPresentList) {
this.allUserPresentList = allUserPresentList;
}
private String presentId ;
public String getPresentId() {
return presentId;
}
public void setPresentId(String presentId) {
this.presentId = presentId;
}
public void setPresentObj(ActionEvent ev){
Object presentOb = ev.getComponent().getAttributes().get("present");
if(presentOb != null){
this.presentId = (String) presentOb;
}else{
presentId = null ;
}
}
}
You need to use a setPropertyActionListener instead of <f:attribute name="present" value="#{present.presentId}"/> as the f:attribute tag is only evaluated when the component is created (only once) not when the component generates html based on the iterated rows.
So you'll need to instead use:
<f:setPropertyActionListener target="#{presentBean.presentId}" value="#{present.presentId}" />
That will set the value of the presentId in your managed bean, so in your action method you can just access the presentId itself already without having to work it out.
Alternatively if you're using a later version of JSF (using Servlet 3.0 or above), then you could create a method in the managed bean which takes the presentId or even the present object as a parameter
e.g. in your managed bean:
public void myAction(Present p){
//do whatever you want with the Present object
}
and in your .xhtml:
<h:commandLink value="Show present #{present.name} #{present.presentId}" actionListener="#{presentBean.myAction(present)}">
</h:commandLink>

Haw can I do for making button show me fileupload

haw can I do for diplay fileupload when I press a commande button.
<p:commandButton icon="ui-icon-refresh" onclick="data.show()"></p:commandButton>
this is my Fileupload
<h:form enctype="multipart/form-data" id="t" >
<p:fileUpload auto="true" disabled="true" id="data"
fileUploadListener="#{composantbean.handleFileUpload}"
sizeLimit="2097152"
label="Choose"
allowTypes="/(\.|\/)(pdf)$/"
description="Images"/>
</h:form>
on this solution the file uplad is displayed ..haw can I do
I don't quite understand your need. You want to display the p:fileUpload only after you click the p:commandButton?
If so, you need to create a boolean variable in your bean(controller) and when clicking on the button, set it true. It would look like this:
.xhtml
<p:commandButton icon="ui-icon-refresh" action="#{testController.renderFileUpload()}" update="#this"/>
<p:fileUpload auto="true" id="data" rendered="#{testController.isRenderFU()}"
fileUploadListener="#{composantbean.handleFileUpload}"
sizeLimit="2097152"
label="Choose"
allowTypes="/(\.|\/)(pdf)$/"
description="Images"/>
testController
...
private boolean renderFU = false;
public void renderFileUpload(){
renderFU = true;
}
public boolean isRenderFU() {
return renderFU;
}
public void setRenderFU(boolean renderFU) {
this.renderFU = renderFU;