Force reload image jsf - image

I would like to display a graphic image in a jsf web application from a html file that's changing for each run.
The problem is that the displayed image was always the same.
I tried to solve this by generating a random number at the end of the image name.
public String getImageId () {
String imageId = "";
int nb = (int)Math.random();
imageId = "?id="+Integer.toString(nb);
return imageId;
}
Then, I call it in my jsf page
<h:graphicImage value="/images/Report.html_files/img_0_0_0#{MyBean.imageId}" cacheable="false" />
But nothing change.
Do you Have any idea about this ?
Thanks

For my application, I
created a Java Servlet that returns a different image
each time, with HTTP header Cache-Control
set to prevent caching and I use the servlet URL in my JSF tag.
I do not think you can dynamically change the image with a URL
to a static file, like in your example.

Another palliative solution, is force refresh in your browser using the keys CTRL + SHIFT + R.

Related

Can't parse html correctly / empty body after parsing

I am facing an odd problem. I im trying to parse the following html:
The problem is that when I do
response.xpath('//div//section//div[#id="hiring-candidate-app"]')[0].extract()
I only get
'<div id="hiring-candidate-app"></div>'
instead of all the content under hiring-candidate-app.
I would like to get, for instance, inside-content, but it looks like I am not even getting that in the response. This webpage requires to be logged in, which I am.
Thanks in advance!
It looks like your Xpath is grabbing the right thing. But your issue might have to do with the '[0]' part of the call. I would remove that to get the full content of the div.
It looks like the elements in question sit on an <iframe>, and therefore live in a different context. You need to activate or switch to the context of the iframe, eg. using JavaScript to interact with an iframe and the document inside of it, e.g.
//Note: Assigning document.domain is forbidden for sandboxed iframes, i.e. on stacksnippets
//document.domain = "https://stacksnippets.net";
var ifrm = document.getElementById("myFrame");
// reference to iframe's window
//var win = ifrm.contentWindow;
// reference to document in iframe
var doc = ifrm.contentDocument ? ifrm.contentDocument : ifrm.contentWindow.document;
// reference an element via css selector in iframe
//var form = doc.getElementById('body > div > div.message');
// reference an element via xpat in iframe
var xpathResult = doc.evaluate("/html/body/div/div[1]", doc, null, XPathResult.ANY_TYPE, null);
<iframe id="myFrame" src="https://stacksnippets.net" style="height:380px;width:100%"></iframe>
However, as you can see when you run the snipped, cross-document interactions are only possible if the documents have the same origin. There are other, more involved methods like the postMessage method that provide the means of interacting cross-domain.

Angular 2 image not refreshing with Zone run

I'm creating an image from an external source (a qrcode on a java ee server). When my Angular2 app updates the data for the image, everything gets updated on the server but the resulting image is not refreshed in the angular app. I'm currently doing a zone.run but it doesn't seem to refresh the image, Everything is fine if the entire page gets refreshed (eg. if I place a location.reload). Anybody no how to properly refresh an image from an external source?
template has this:
<img [src]=url />
component has this:
this.zone.run(()=>{
this.url = QRURL + this.selectedProduct.id;
});
QRURL is a constant that contains the server's url that executes the generation of the image.
The quotes didn't fix it, but Maxime's 2nd comment did. My constant alread y had a query string, so I just added the date stuff as another parameter on it, and that did the trick and I was able to strip off the zone.run. The final variable looked like this:
this.url = QRURL + this.selectedProduct.id + '&date=' + new Date().getTime();
Thanks!

ASP.NET MVC3 Html.Raw changing absolute URLs?

I'm using ASP.NET MVC 3 and the helper #Html.Raw in my view.
I'm passing it some HTML markup that I have stored in a database. The markup contains some URLs that point to other places on my site. For example http://www.foo.fom/events. These data are forum posts, so the page they're displayed on has the form http://www.foo.com/forums/thread/42/slug.
However, when the page is rendered, the saved URLs are rendered in modified form as:
http://www.foo.com/forums/thread/42/events/
This only happens for URLs on my site. If the URL points to some external site, it is unchanged.
I have verified that what I'm passing into #Html.Raw is the correct URL (http://www.foo.com/events). Why is it getting changed as the page is rendered? Is there an easy way to disable this "feature"?
Here's my code for displaying the markup:
<div>
#Html.Raw(post.Body)
</div>
and here's the controller code that genrates the page data:
var post = _forumRepository.GetPostById(id)
var model = new ForumPostView()
{
Body = post.Body,
PostDate = post.DatePosted,
PostedBy = post.Author,
PostId = post.Id
};
return View(model);
I have verified via debugger that the exact URL in the post.Body before being passed back to the View is of the form "http://www.foo.com/events" (no trailing slash). I have also verified via debugger that the value is unchanged before it is passed into #Html.Raw.
It sounds like the urls that are pointing to other pages on your site are non-absolute. Are you certain they start with a / or http? If not, it's behaving exactly as it's supposed and treating them like relative urls -- and thus appending them to the current url.
(Html.Raw will not manipulate the string, so it's not at fault here)
Also, it wouldn't hurt to show us your code.
No, in fact I am an idiot. The URLs were indeed stored in relative form without a leading /, which is why they ended up being relative to the current page. The text displayed was absolute, which is what I saw when I looked at the db. That's what I get for debugging on a few hours' sleep ;)

How to clear validation errors after providing a file download on form submit?

I've got a page where there is an input field and a button. Clicking the button generates a PDF report, based on the value entered in the input field. My problem lies in the fact that the input field needs some special formatting, and I have a validator that will fire if the required format is not present.
Suppose I enter an invalid value in the field, then click the button. The PDF report won't be generated because of the validation, and the field becomes red, as expected. But then I enter a correct value (with the field still red) and press the button. The report gets generated but the field remains red. That's because I've used FacesContext.getCurrentInstance().responseComplete() after the PDF generation so the normal Faces lifecycle has been bypassed and the clearing of the validation errors does not trigger anymore for this request.
How can I generate the PDF report but still force Faces to determine that the validation error should be cleared? Thanks!
This cannot be done in one HTTP response. You'd need to let the client send two requests so that you can return two responses; one to clear the errors and other to return the PDF. You'd only need to do some rework in the bean's action method and probably add a file serving servlet.
Basically, you need to store the PDF in local (temp) disk location or maybe in memory and let JSF conditionally render some JavaScript which in turn downloads the PDF. The PDF can be downloaded through a servlet or some additional webapp context pointing to the local disk location.
E.g.
<h:form>
...
<h:commandButton value="Download" action="#{bean.submit}" />
<h:panelGroup rendered="#{not empty bean.pdfURL}">
<script>window.location = '#{bean.pdfURL}';</script>
</h:panelGroup>
</h:form>
with
public void submit() {
// Create PDF and store as byte[] in memory, or as File on disk.
// Then create an unique URL to the PDF.
pdfURL = externalContext.getRequestContextPath() + "/pdf/" + pdfID;
}
which should generate the following on a succesful form submit (which should also clear the validation errors!)
<script>window.location = '/contextname/pdf/uniquefilename.pdf';</script>
If you've registered /some/path/to/pdf as another webapp context in the server configuration, then you can just store the File in there and it will be downloaded. But if you can't because you have no control over the server configuration, then you'd need to create a servlet which is mapped on an URL pattern of /pdf/* and does something like the following in doGet() method:
String filename = request.getPathInfo().substring(1);
File pdf = new File("/some/path/to/pdf", filename);
response.setHeader("Content-Type", "application/pdf");
response.setHeader("Content-Length", String.valueOf(file.length()));
InputStream input = new FileInputStream(pdf);
OutputStream output = response.getOutputStream();
// Now just write input to output.

image using .ashx

i am using .ashx to retrive image and i place the the image inside the ajax update panel it retrive the image when a new image is added to the form but when we change the image it is not updating the image it dont even call the .ashx file but when i refresh the browser it works properly
Sounds like a caching issue. Try adding some of the lines found here to your ashx file and it should hopefully force the browser to rerequest the image. (I know that the link is for ASP rather than ASP.NET, but things like Response.Expires = -1 should work)
Alternatively, can you change the path to the image in the updatepanel? If you just add a random parameter on to the end of it the browser will treat it as a fresh request (we use the current date/time as a parameter when we're doing this. The parameter is ignored by ASP.NET unless you explicitly reference it)
Do something like this:
var sPath = "../../handlers/ProcessSignature.ashx?type=View&UserID=" + userID + "&d=" + (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
That puts a 4 character alpha numeric string at the end of your query string. It's not needed, but it will force browsers to pick up the latest version of that image because the URL is different.
I tried the above and some browsers ignore the headers. I threw all of those in and Chrome/FireFox 3 didn't try to update.
IE7 worked sometimes
IE6 just twiddled it's thumbs and asked why it was still in existence.
Changing the path above will fix it in all browsers.

Resources