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>