0

Trying to reproduce the following reference Load image from outside webapp, I am not able to display images from the following url http://localhost:8080/images/eventHead1.jpg on xhtml page.

The url alone works fine: the Wildfly service is serving the image located in the standalone\data\images folder without any issue directly on the browser. However the image cannot be not rendered on the xhtml page due to the following error.

I am suspecting the Bean that I may not properly defined especially the parameter "pathString" as the getImage method returns null. Appreciate your help.

Used versions: Mojarra: 2.3.9; Primefaces: 7.0; Wildfly:16

Error:

23:08:42,869 ERROR [io.undertow.request] (default task-1) UT005023: Exception handling request to /Intranet/dashboard.xhtml: javax.servlet.ServletException: Method not found: class com.org.ui.ImageBean.getImage(null)

standalone.xml:

    <server name="default-server">
        <http-listener name="default" socket-binding="http" redirect-socket="https" enable-http2="true"/>
        <https-listener name="https" socket-binding="https" security-realm="ApplicationRealm" enable-http2="true"/>
        <host name="default-host" alias="localhost">
            <location name="/" handler="welcome-content"/>
            <location name="/images" handler="ContentDir"/>      //added
            <http-invoker security-realm="ApplicationRealm"/>
        </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/images" directory-listing="true"/>  // added
    </handlers>

Bean:

@ManagedBean
@RequestScoped

public class ImageBean {

        public StreamedContent getImage() throws IOException {
            FacesContext context = FacesContext.getCurrentInstance();

            if (context.getCurrentPhaseId() == PhaseId.RENDER_RESPONSE) {
                // So, we're rendering the view. Return a stub StreamedContent so that it will generate right URL.
                return new DefaultStreamedContent();
            }
            else {
                // So, browser is requesting the image. Return a real StreamedContent with the image bytes.
                String filename = context.getExternalContext().getRequestParameterMap().get("filename");

                String pathString = "http://localhost:8080/images/";

                return new DefaultStreamedContent(new FileInputStream(new File(pathString, filename)));
            }
        }
}

xhtml:

<p:graphicImage value="#{imageBean.image}">                 
   <f:param name="filename" value="#{myevent.imageName}" />       
</p:graphicImage>
LEK
  • 83
  • 2
  • 11
  • The link you found even says this *For the first way and the Tomcat and WildFly approaches in second way, the images will be available by http://example.com/images/filename.ext and thus referencable in plain HTML as follows ``* – BalusC Feb 17 '20 at 05:14
  • @BalusC Thank you, I tried your suggestion `` but it does not retrieve images. – LEK Feb 17 '20 at 10:40
  • 1
    It's not serving on same context, use `` as indicated in previous comment. – BalusC Feb 17 '20 at 10:52
  • @BalusC. works like a charm. I just limited myself in my mind believing that I could not use `#{myevent.imageName}` in ``. Thank you – LEK Feb 17 '20 at 12:15

0 Answers0