JSF Graphic Image is not changing [duplicate] - jsf

I'm using primeFaces to display the coverimage of a series in it's detailView. My problem is, that it is always showing the same picture, except if I refresh my cache (Strg+F5 in Chrome).
My code to display the picture is the following
<ui:define name="content">
<h:body>
<f:view>
<h:form>
<h1>#{seriesController.selectedSeries.name}</h1>
<p:graphicImage value="#{coverController.getFirstCoverOfSeries(seriesController.selectedSeries.id)}"/>
[Here are some fields...]
</h:form>
</f:view>
</h:body>
</ui:define>
I already checked my controller manually - it loads the cover just fine, but primeFaces does not display it correctly (Controller returns a DefaultStreamedContent with the byte[] and the correct type).
I've seen some people using <f:param> to give the params to the method loading the picture.
I'd really appreciate some help - has <p:graphicImage> some quirks or am I just using it wrong?

As stated in the documentation, by default the p:graphicImage is caching, you must add cache="false" in order to force a refresh.
More info
p:graphicImage

Related

Why doesn't <rich:datascroller> work in a <h:form>?

I have a jsf page in the form of
<h:form>
<rich:dataTable id="myTable">
<!--Table Data-->
</rich:dataTable>
<rich:dataScroller for="myTable"/>
</h:form>
When the table is in a <h:form> it doesn't scroll (the scroller is disabled) but once I take them out of the <h:form> it works fine. Issue is I need them to be in a form for the rest of my code to work. Anyone know the reason for the issue? I've seen people ask about it but no good answers.
I figured it out. I was using the form in a modal pane and the <ui:include> was within a <h:form> so there was an issue with it being a form within a form.

Primefaces graphicImage without ?pfdrid_c=true

I am trying to render a graphic image in primefaces but always returns a 404.
In Chrome Debug I can see the address image like this:
/UltracarWeb/Images/1427303591376_image.png?pfdrid_c=true"
Using the Chrome Debug I changed for:
/UltracarWeb/Images/1427303591376_image.png?pfdrid_c=false" , then the image is rendered.
I call the graphicImage in xhtml this way:
<p:graphicImage style="max-width: 110px; max-height: 140px;"
value="#{product.PhotoUrl}"
rendered="#{not empty product.Url}">
</p:graphicImage>
I already tryed with cache false and true, and still not work.
The url is not empty because I checked this in the Debug.
Debugging the java code I saw the product.url without the ?pfdrid_c=true, but in browser Debug the ?pfdrid_c=true appears.
This ?pfdrid_c=true is a primefaces constant do refers the dynamic cache, but I don't know how can I remove it?
I have had the same problem with this component of primefaces to images, because it adds the parameter ?pfdrid_c=true used to control the Cache-Control
I was using it like this (primefaces element):
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:p="http://primefaces.org/ui"...>
<p:graphicImage value="#{product.PhotoUrl} />
and I chose to use this other jsf element that works perfectly for me since it does not introduce parameter:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"...>
<h:graphicImage value="#{product.PhotoUrl} />
This works only if the value is a String representing an URL but this does not work if the value is a DefaultStreamedContent. This isn't supported by <h:graphicImage>

Primefaces graphicImage always showing the same picture

I'm using primeFaces to display the coverimage of a series in it's detailView. My problem is, that it is always showing the same picture, except if I refresh my cache (Strg+F5 in Chrome).
My code to display the picture is the following
<ui:define name="content">
<h:body>
<f:view>
<h:form>
<h1>#{seriesController.selectedSeries.name}</h1>
<p:graphicImage value="#{coverController.getFirstCoverOfSeries(seriesController.selectedSeries.id)}"/>
[Here are some fields...]
</h:form>
</f:view>
</h:body>
</ui:define>
I already checked my controller manually - it loads the cover just fine, but primeFaces does not display it correctly (Controller returns a DefaultStreamedContent with the byte[] and the correct type).
I've seen some people using <f:param> to give the params to the method loading the picture.
I'd really appreciate some help - has <p:graphicImage> some quirks or am I just using it wrong?
As stated in the documentation, by default the p:graphicImage is caching, you must add cache="false" in order to force a refresh.
More info
p:graphicImage

h:commandbutton not working in <o:form>

<html xmlns:h="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui"
xmlns:o="http://omnifaces.org/ui">
<o:form includeViewParams="true">
<h:commandButton value="Home" action="/index?faces-redirect=true"/>
<p:dataTable>
</p:dataTable>
</o:form>
The h:commandButton is not working under the o:form. When I click on it, it remains on the same page. But when I change to h:form, it works. Nevertheless I need to use o:form for the includeViewParams. Is there any way I could resolve this?
This construct works for me, as in, it actually navigates to /index. Only the view params disappear from the URL because you're forcing a redirect. But there is more, the <h:commandButton> is here essentially the wrong tool for the purpose. You want pure page-to-page navigation. You should then not be using a command link/button at all, but a plain link/button. You need <h:button>.
<h:button value="Home" outcome="/index" includeViewParams="true" />
Note: this doesn't require any form.
See also:
How to navigate in JSF? How to make URL reflect current page (and not previous one)

Unable to understand <h:head> behaviour

I have a template composition Button.xhtml which contains a <p:commandLink>:
<ui:composition
xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
<p:commandLink value="View" action="#{printClass.printPdf}"/>
</ui:composition>
The link's purpose is to generate PDF.
I have a template client defaultPage.xhtml where the Button.xhtml is been included.
<ui:composition template="../../WebPages/MasterPage/Template.xhtml">
<ui:define name="MainContent">
<ui:include src="../../WebPages/Facelets/Button.xhtml"/>
</ui:define>
</ui:composition>
The last one is Template.xhtml which inserts the MainContent template definition inside a <h:form>.
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<h:body>
<h:form>
<ui:insert name="MainContent" />
</h:form>
</h:body>
</html>
When I place <h:head></h:head> in Template.xhtml, then the <p:commandLink> in Button.xhtml stops working, but CSS of page works perfect. When I remove the <h:head></h:head> or replace it by <head></head> then the <p:commandLink> starts working, but CSS stops working.
How is this caused and how can I solve it?
The <h:head> will auto-include all necessary JavaScript files for ajax behavior and CSS files for layout. When you remove it, then CSS will not be auto-included and ajax behavior will not be enabled. The <p:commandLink> would then act like as a plain vanilla link.
The <h:head> is absolutely necessary for proper functioning of JSF and PrimeFaces components and applying of the PrimeFaces look'n'feel. So you should not remove or replace it.
Let's concentrate on the problem of the failing <p:commandLink>. There are relatively a lot of possible causes, which are all mentioned in this answer: commandButton/commandLink/ajax action/listener method not invoked or input value not updated
You didn't show a fullworthy SSCCE, so it's impossible to copy'n'paste'n'run your code to see the problem ourselves (work on that as well in your future questions). So, I'll just mention the most probable cause for this problem based on the symptoms: you're nesting <h:form> components in each other. Placing the <h:form> in the master template is also a design smell. You should rather place it in the template client. Als note that the <p:dialog> should have its own form but that the <p:dialog> should by itself not be nested in another form.
Update: based on the comments, you're trying to return a whole PDF file as a response to an ajax request. This will indeed not work. The ajax engine expects a XML response with information about changes in the HTML DOM tree. A PDF file isn't valid information. Also, JavaScript has for obvious security reasons no facilities to programmatically trigger a Save As dialogue whereby possibly arbitrary content is provided.
You can't download files by ajax. You have to turn off ajax. In case of <p:commandLink> there are basically 2 solutions:
Use ajax="false".
<p:commandLink ... ajax="false" />
Just use <h:commandLink> instead.
<h:commandLink ... />
In Button.xhtml where you placed
<h:commandLink value="View" action="#{printClass.printPdf}"/>
You need to disable the ajax.So your new code should be like
<h:commandLink value="View" action="#{printClass.printPdf}">
<f:ajax disabled="true"></f:ajax>
</h:commandLink>

Resources