2

I am using JSF and glassfish 4.0. I know that all the images that are placed in images directory under the resources folder of the project can be accessed directly. I wanted to know how I can display images which are located in glass fish folder.I created a images folder under glass fish domains and I want to display this images in the JSF page. Could anyone let me know how I can do that? I am new to this so need some help.

<?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="">
    <property name="alternatedocroot_1" value="from=/images/* dir=/test/webapp" /> 
    <class-loader delegate="true"/>
    <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>

JSF page:

<header>
    <h:graphicImage id="banner" style="width:700px;height: 52px;"  
                    value="/images/header.jpg" />
</header>
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
Valla
  • 2,414
  • 11
  • 42
  • 73
  • I tried adding this in glassfish-web.xml and in my jsf page I added . But it gives me failed to load resource.Could anyone let me know what I am missing. /test/webapp/images is the folder structure i created in c:/test/webapp/images. Thanks. – Valla Nov 14 '13 at 15:40

1 Answers1

6

As per the comments,

I tried adding this in glassfish-web.xml

<property name="alternatedocroot_1" value="from=/images/* dir=/test/webapp/images" /> 

Assuming that you're using Windows with GlassFish installed on C:\, then this specific configuration expects a folder C:\test\webapp\images\images with all the images therein.

If you actually have a folder C:\test\webapp\images, then you should be configuring as follows:

<property name="alternatedocroot_1" value="from=/images/* dir=/test/webapp" /> 

Also note that this is absolutely not in any way relative to the GlassFish installation folder or the webapp's deploy space, so "I created a images folder under glass fish domains" as per the question would absolutely not work this way.

If you actually have a C:\path\to\glassfish\domains\test\webapp\images folder, then you should be configuring it as follows:

<property name="alternatedocroot_1" value="from=/images/* dir=/path/to/glassfish/domains/test/webapp" /> 

See also:


Update: as per the glassfish-web.xml contents which you edited in your question afterwards, that <property> shouldn't go in <jsp-config>, but needs to be an immediate child of <glassfish-web-app>.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I tried what was suggested in the links that you have mentioned, but still I am not able to get the images. I am not able to understand what I am missing here. My project name is "TestProject" so when i say http://localhost:8080/TestProject/images/header.jpg then it gives me 404 error. I have glassfish installed on c:/Program Files so is that causing a problem . I also tried giving the glass fish path but it did not work. I also tried the suggestions that you gave me but still the same problem. Could be please suggest what I am missing here. – Valla Nov 14 '13 at 16:42
  • I have tried that also it says page not found..These are the two ways that I have tried create d anew folder as mentioned above in C:/test/webapp and put the entire images folder here and did the same thing in glassfish/domains/domain1/applications. in JSF page accesing it like this . Still not able to figure out what I am missing here. Also do i need to write anything in the servlet for it work. – Valla Nov 14 '13 at 18:16
  • Works just for me. No need for a custom servlet. Are you sure that you've properly cleaned GlassFish cache/work/deploy folder? – BalusC Nov 14 '13 at 18:21
  • Yes, I have restarted the server but still did not work. I am actually using a maven project and I manually created the glassfish-web.xml in the WEB-INF folder. Is that causing the error. I have edited the question and added both the glasfish and jsf code that I am using. – Valla Nov 14 '13 at 18:37
  • Also I wanted to know since i am using JSF and the glassfish-web.xml has jsp-config tag, is that causing the issue? – Valla Nov 14 '13 at 18:46
  • Oh right, that `` has to be an immediate child of `` and not ``. Indeed, move the property one level higher in the XML tree hierarchy. And I have to revert my previous comment, I just tested on GF4 and the image URL indeed includes the webapp's context path. – BalusC Nov 14 '13 at 18:48
  • I tried to move the property out of the jsp-config and I get this error " Error occurred during deployment: Exception while loading the app : java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException:" when i try to run my application. When i move the property back inside the jsp-config this does not happen. I have updated the glassfish-web.xml in the question. Please let me know. Thanks for your help. – Valla Nov 14 '13 at 19:03
  • That's a step in the good direction! This error just suggests that the `dir` cannot be found. When using Windows, try `dir=C:\test\webapp` instead of `dir=/test/webapp`. – BalusC Nov 14 '13 at 19:08