How to display images located in glass fish folder

2020-02-29 11:25发布

问题:

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>

回答1:

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:

  • Load images from outside of webapps / webcontext / deploy folder using <h:graphicImage> or <img> tag
  • How to write a file to resource/images folder of the app?

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>.