How can I serve static resources from outside a war on WildFly

user3629892 picture user3629892 · Dec 23, 2015 · Viewed 8.9k times · Source

I may be wrong, but to my understanding, the following must be possible in WildFly:

It must be possible to put a link into my JSF views (i. e. the xhtml files) to a resource (pdf, image, other xhtml file) that is already on the WildFly server.

I can do the same thing in php and an apache server.

Where would I need to put those resources and how can I access them from my views? E. g. put a link in the view to a pdf file that opens the pdf file in a new tab.

Thanks a lot for tips and hints!!

EDIT

standalone.xml

<server name="default-server">
    <http-listener name="default" socket-binding="http" max-post-size="974247881"/>
    <host name="default-host" alias="localhost">
        <location name="/" handler="welcome-content"/>
        <location name="/content" handler="ContentDir"/>
        <filter-ref name="server-header"/>
        <filter-ref name="x-powered-by-header"/>
    </host>
</server>
<servlet-container name="default">
    <jsp-config/>
    <websockets/>
</servlet-container>
<handlers>
    <file name="welcome-content" path="${jboss.home.dir}/welcome-content"/>
    <file name="ContentDir" path="${jboss.home.dir}/standalone/data/unzipped" directory-listing="true"/> 
</handlers>

link in JSF view

<h:outputLink value="http://localhost:8181/content">KLICK</h:outputLink>

When I click on this, I get the directory listing, as you said.

But how can I make it so that the index.xhtml in the directory that content points to is displayed?? That's really what I want.

content points to ${jboss.home.dir}/standalone/data/unzipped and in unzipped there is an index.xhtml as well as another folder with more .xhtml files.

In the index.xhtml there are relative links to the .xhmtl files in the folder:

<ul>
    <li><a href="t/rt.html">hg</a></li>
    <li><a href="t/tert.html">jghj</a></li>
    <li><a href="t/gf.html">jghj</a></li>
    <li><a href="t/hg.html">jghj</a></li>
    <li><a href="t/hgfh.html">jghj</a></li>
    <li><a href="t/hfgh.html">jhgj</a></li>
    <li><a href="t/hfgh.html">jhgj</a></li>
    <li><a href="t/hg.html">jghj</a></li>
    <li><a href="t/hghh.html">jghj</a></li>
</ul>

I want to display the index.xhtml file in unzipped and from there navigate to the other .xhtml files.

Something like that must be possible, must not it??

Or how else would you write an application where a user can upload html files to a Java ee server and then see those files displayed?

Answer

Sevan picture Sevan · Dec 23, 2015

You may not want to deploy all your static content with your application. These may be images, PDF documents, or other types of files. You should configure Undertow in order to solve this problem. Below example shows you how to do this by configuring Undertow subsytem.

<server name="default-server">
    <http-listener name="default" socket-binding="http"/>
    <host name="default-host" alias="localhost">
        <location name="/" handler="welcome-content"/>
        <location name="/img" handler="images"/>
    </host>
</server>
<handlers>
    <file name="welcome-content" path="${jboss.home.dir}/welcome-content" directory-listing="false"/>
    <file name="images" path="/var/images" directory-listing="true"/>
</handlers>

With this additional configuration, any request for resources to www.sampledomain.com/contextroot/img will be redirected to the filesystem on your hard disk. If you mark "directory-listing" attribute as false, then requests will be redirected as a properly displayed file.