JSF selectOneMenu onChange 更新对象
JSF selectOneMenu onChange to update an object
我使用 How to include file from external local disk file system folder in JSF 中答案的代码得出:
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.");
}
}
...
这很管用。 PDF 文件在查看器中打开,selectOneMenu 显示目录中的所有文件名。
所以我的问题是:
我怎样才能 change/select 来自 selectOneMenu 的名称并在对象中打开该文件?
我想我必须使用 selectOneMenu 中的 itemValue 作为 PdfServlet 的参数并使用它而不是 s 中的硬编码值,但我不确定该怎么做。任何建议,将不胜感激。 TIA.
这就是我所做的...不确定这是否正确,但它确实满足了我的要求...
添加到 JSF(顶部):
<f:metadata>
<f:viewAction action="#{dataEntryBean.onLoad()}" />
<f:viewAction action="#{pdfServlet.setFileName(dataEntryBean.fileName)}" />
</f:metadata>
并在 fileNameListError 之前添加:
<h:commandButton value="Submit" action="#pdfServlet.setFileName(dataEntryBean.fileName)}"/>
然后在PdfServlet中我改了
String s = "my.pdf";
到
private static String s;
添加到 DataEntryBean:
public void onLoad() {
setFileNameList();
this.fileName = this.fileNameList.get(0);
}
正如我所说,我不确定这是否是 100% 正确的方法,但它确实有效。任何使这更好的更新或更正将不胜感激。谢谢。
我使用 How to include file from external local disk file system folder in JSF 中答案的代码得出:
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.");
}
}
...
这很管用。 PDF 文件在查看器中打开,selectOneMenu 显示目录中的所有文件名。
所以我的问题是: 我怎样才能 change/select 来自 selectOneMenu 的名称并在对象中打开该文件?
我想我必须使用 selectOneMenu 中的 itemValue 作为 PdfServlet 的参数并使用它而不是 s 中的硬编码值,但我不确定该怎么做。任何建议,将不胜感激。 TIA.
这就是我所做的...不确定这是否正确,但它确实满足了我的要求...
添加到 JSF(顶部):
<f:metadata>
<f:viewAction action="#{dataEntryBean.onLoad()}" />
<f:viewAction action="#{pdfServlet.setFileName(dataEntryBean.fileName)}" />
</f:metadata>
并在 fileNameListError 之前添加:
<h:commandButton value="Submit" action="#pdfServlet.setFileName(dataEntryBean.fileName)}"/>
然后在PdfServlet中我改了
String s = "my.pdf";
到
private static String s;
添加到 DataEntryBean:
public void onLoad() {
setFileNameList();
this.fileName = this.fileNameList.get(0);
}
正如我所说,我不确定这是否是 100% 正确的方法,但它确实有效。任何使这更好的更新或更正将不胜感激。谢谢。