5

I've problems displaying images in my index.xhtml file.

I want the link from my database to point to the image file so that a <h:graphicImage/> or <img /> tag can display it. Currently using localhost only.

I've managed to save an image to c:\var\webapp\images\ and also added <property name="alternatedocroot1" value="from=/images/* dir=/var/webapp" /> to my glassfish-web.xml and using:

<h:graphicImage value="#{entity.imageLink}" />
<img alt="#{entity.title}" src="#{entity.imageLink}" />

But I never manage to display any images! What should be written in my database to display imageExample.jpg which is saved in c:\var\webapp\images\imageExample.jpg? My thought is that the problem lies in my path but I might very well be wrong.

TheZeph
  • 61
  • 5
  • 1
    Isn't it recommended to store it elsewhere? I've more or less followed BalucsC's ([link](http://stackoverflow.com/a/19142316/3669047)) answer in another thread. – TheZeph Jun 21 '15 at 10:51
  • With this configuration, the URL must be (either absolutely or relatively) pointing to `http://example.com/images/filename.ext`. Test it first by plain entering it in browser's address bar without using an intermediate HTML/JS page. – BalusC Jun 22 '15 at 08:51
  • This is my address: `http://localhost:8080/AnimeSeriesV3/faces/public/index.xhtml` but I can't find the image on `http://localhost:8080/AnimeSeriesV3` with `/images/imageExample.jpg` or `/faces//images/imageExample.jpg`. – TheZeph Jun 29 '15 at 09:44
  • 1
    As said, given this configuration, it's on `http://localhost:8080/images/imageExample.jpg`. If that still doesn't work, and you have copypasted the code into question, then it can be caused by that typo in `alternatedocroot1`. You forgot the `_` there (doublecheck documentation/examples where you found this information). – BalusC Jun 29 '15 at 09:46
  • Cheers for noticing the lack of `_`, BalusC! After adding the underscore, glassfish complained that `glassfish4\glassfish\domains\domain1` didn't have `var\webapp` so I added it and the GF server started. Still no luck with `http://localhost:8080/images/imageExample.jpg`. Not even if I manually copy the image into the "domain1" mapstructure. – TheZeph Jun 29 '15 at 11:13
  • Hmm, this is Windows? Try a Windows path in dir like so `dir=c:\var\webapp`. Which GF version? – BalusC Jun 29 '15 at 11:20
  • Windows8, Glassfish 4, JSF 2.2. Got "URL pattern \images\* for alternate docbase is invalid" when changing "/" to "\" and adding c: Changed back to "/" which removed the "warning" but still no luck finding the image. – TheZeph Jun 29 '15 at 11:27
  • Aight, solved it! Like a combination lock, you'll only be told that the code are incorrect, not which number is wrong. So eventually I got all back- and forwardslashes right, and ofcourse my address right and now it works :) Cheers for the help BalusC! – TheZeph Jun 29 '15 at 12:36
  • Post the answer as an answer, not as an update to the question. – BalusC Jun 29 '15 at 13:03
  • Cool, will do that! Cheers for guiding me through my first stackoverflow question! – TheZeph Jun 29 '15 at 13:50

2 Answers2

1

Solved this as follows:

Changed
<property name="alternatedocroot1" value="from=/images/* dir=/var/webapp" />
to
<property name="alternatedocroot_1" value="from=/images/* dir=c:\var\webapp" />
Notice the slashes.

In my database (MySQL) I save /images/imageExample.jpg which then will be displayed with <h:graphicImage value="#{entity.imageLink}" /> .

I can now access the image at http://localhost:8080/<MyAppName>/images/imageExample.jpg.

Community
  • 1
  • 1
TheZeph
  • 61
  • 5
0

It's always a good idea, when in doubt, to println to the server log to see what you are passing to the tag, but I think the problem is that when using backslash it should be doubled \ since it is used for escape characters.

prmottajr
  • 1,816
  • 1
  • 13
  • 22
  • I've inspected the DOM to see which kind of link is used and tried with "\\". I've also changed the text in my database to different paths but no luck. With "alternatedocroot1" I at least get what looks like a proper link: //images/imageExample.jpg but still no image is displayed :( – TheZeph Jun 21 '15 at 11:28
  • Do you get the image if you write the link you're receiving directly in the browser? – prmottajr Jun 21 '15 at 11:56
  • Nope. If I type h:graphicImage value="/#{entity.imageLink}" (notice the "/") the path is displayed as "/projectName/images/imageExample.jpg". In my database I've "images/imageExample.jpg" as a varchar (MySQL). Without the "/" in the begining only "images/imageExample.jpg" is shown. – TheZeph Jun 21 '15 at 12:28
  • Ok, but if ypu type this address on the browser directly does it load the image? – prmottajr Jun 22 '15 at 09:44
  • No, it doesn't. Which I apperently fail to say in my previous answer :P – TheZeph Jun 23 '15 at 11:44
  • So, maybe that is the problem. Is there any image available on the folder you are trying to load from? – prmottajr Jun 23 '15 at 11:48
  • Yes, I've images in the folder I'm trying to reach. Atleast my intention is to load from that map. Have a feeling that my intentions and actions doesn't collaborate. – TheZeph Jun 23 '15 at 12:07