I can't seem to figure out alternatedocroot. I have looked at other questions on stackoverflow, and at the oracle docs for glassfish (but they are as clear as mud to me).
I have a maven project with web, ejb, ear, and an aggregator/parent module. In the web module/project I have added a glassfish-web.xml file with the following entry:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE glassfish-web-app PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Servlet 3.0//EN" "http://glassfish.org/dtds/glassfish-web-app_3_0-1.dtd">
<glassfish-web-app error-url="">
<class-loader delegate="true"/>
<property name="alternatedocroot_1" value="from=/images/* dir=/images " />
<jsp-config>
<property name="keepgenerated" value="true">
<description>Keep a copy of the generated servlet class' java code.</description>
</property>
</jsp-config>
</glassfish-web-app>
I have created an images directory on the root of the Linux box (I'll put it somewhere else once I sort this problem out) and put a "picture.png" file in it. I did a chmod -R 777 /image so permissions should not be an issue.
The welcome.xhtml is this:
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html">
<h:head>
</h:head>
<h:body>
Hello World
<h:graphicImage url="/MyProject-web/images/picture.png"
height="200"
width="960" />
</h:body>
</html>
All I see when I run the project is the
"Hello World"
This is the URL in the browser when run:
http://localhost:8080/MyProject-web/
Can someone help me with the property / directory setup / and image url in the xhtml file?
So here's how it works (finally). Took me a while before I found something that I could understand. So I'll try to lay it out like nothing else did.
Let's start with where you have some resource or static content files on disk.
/opt/project/resources/images/
/opt/project/resources/css/
And you have static files you want to include or reference like this:
/opt/project/resources/images/picture.png
/opt/project/resources/css/screen.css
In the glassfish-web.xml you would put (for two docroots)
<property name="alternatedocroot_1" value="from=/images/* dir=/opt/project/resources" />
<property name="alternatedocroot_2" value="from=/css/* dir=/opt/project/resources" />
In your xhtml file to display an image and link to an external css file you do this:
<h:graphicImage url="images/picture.png" />
<link href="css/screen.css" rel="stylesheet" type="text/css" media="screen, projection" />
You can add other directories under say, the images directory so you have something like:
/opt/project/resources/images/otherimages/anotherpicture.png
and just as you would expect you would use it like:
<h:graphicImage url="images/otherimages/anotherpicture.png" />
However it appears that if you try to add a symbolic link to where the other images are, it won't work. i.e. the following won't work (you won't be able to access the images in the "otherimages" symbolically linked directory)
.../resources/images$ ln -s /somedirectorytree/otherimages/ otherimages
Which is too bad. If someone knows if this is really possible (i.e. if I'm mistaken) I'd love to know. Or if there are other tricks rather than resort to hard (coded... for want of a better word) configuration. It would be nice to just be able to add links to other file systems.
Edit
As a side note, I thought I would add another helpful tidbit that relates to this. If you want to make sure you get the complete (context) path to your file in the url you can add this in front:
<link href="${facesContext.externalContext.requestContextPath}/
Using an example from above:
<link href="${facesContext.externalContext.requestContextPath}/css/screen.css" rel="stylesheet" type="text/css" media="screen, projection" />
This will ensure you are always looking in the correct path on your server.