Issue
I used the code from the answer in How to include file from external local disk file system folder in JSF to come up with:
JSF
...
<ui:define name = "content">
<h:form>
<span class="dataSpan" style="border-width:0px">
<object id="thePdf" data="#{request.contextPath}/my.pdf" type="application/pdf" width="1150" height="620">
<a href="#{request.contextPath}/my.pdf">Download file.pdf</a>
</object>
</span>
</h:form>
<h:form class="standardFont">
<span class="notesSpan" style="border-width:0px">
<p:panel header="Data Entry">
<h:panelGrid columns="1" border="0" styleClass="form-grid" columnClasses="form-column-label,form-column-input">
<h:outputLabel />
<h:outputLabel id="fileName" styleClass="centerBoldRed" value="#{pdfServlet.fileName}" >
</h:outputLabel>
<h:outputLabel />
<h:outputLabel for="fileNameList">Files:</h:outputLabel>
<h:selectOneMenu id="fileNameList" value="#{dataEntryBean.fileNameList}" styleClass="boldRed">
<f:selectItems value="#{dataEntryBean.fileNameList}" var="file" itemValue="#{file}" itemLabel="#{file}" />
</h:selectOneMenu>
<h:message class="error" for="fileNameList" id="fileNameListError" />
</h:panelGrid>
...
Java - PdfServlet
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String s = "my.pdf";
File file = new File("//Temp/input/my/pdfs/IncomingPdf/" + s);
response.setHeader("Content-Type", getServletContext().getMimeType(file.getName()));
response.setHeader("Content-Length", String.valueOf(file.length()));
response.setHeader("Content-Disposition", "inline; filename=\"" + file.getName()+ "\"");
Files.copy(file.toPath(), response.getOutputStream());
}
DataEntryBean
...
public List<String> getFileNameList() {
return fileNameList;
}
public final void setFileNameList() {
File folder = new File("//Temp/input/my/pdfs/IncomingPdf/");
FilenameFilter pdfFileFilter = (File dir, String name) -> {
return name.endsWith(".pdf");
};
File[] files = folder.listFiles(pdfFileFilter);
try {
for(File f : files) {
fileNameList.add(f.getName());
}
} catch (ArrayIndexOutOfBoundsException ex) {
fileNameList.add("No PDF file was found.");
}
}
...
And this works great. The PDF file is opened in the viewer and the selectOneMenu displays all of the file names from the directory.
So my question is this: How can I change/select a name from the selectOneMenu and have that file get opened in the object?
I think I have to use the itemValue from the selectOneMenu as a parameter to PdfServlet and use that instead of the hardcoded value in s but I'm not sure how to do that. Any suggestions would be appreciated. TIA.
Solution
So here's what I did...not sure if it's the correct way, but it does what I was looking for...
Added to JSF (at top):
<f:metadata>
<f:viewAction action="#{dataEntryBean.onLoad()}" />
<f:viewAction action="#{pdfServlet.setFileName(dataEntryBean.fileName)}" />
</f:metadata>
And added this before fileNameListError:
<h:commandButton value="Submit" action="#pdfServlet.setFileName(dataEntryBean.fileName)}"/>
Then in PdfServlet I changed
String s = "my.pdf";
to
private static String s;
Added to DataEntryBean:
public void onLoad() {
setFileNameList();
this.fileName = this.fileNameList.get(0);
}
As I said, I'm not sure if this is the 100% correct way to do this but it does work. Any updates or corrections that make this better would be appreciated. Thanks.
Answered By - BigFish
Answer Checked By - Cary Denson (JavaFixing Admin)