我是Java-ee開發新手,我對此非常困惑。事實上,我想用jsf導入文件並將此文件保存在目錄中,但我始終得到目標不可到達。這是我的豆:使用jsf導入文件時無法訪問目標
@ManagedBean
@RequestScoped
public class Import_2G {
//@EJB
//private GestionCellRef2GLocal gestionCellRef2GLocal;
private UploadedFile uploadedFile;
public void save() throws IOException {
//GestionCellRef2GRemote t = null;
Path folder = Paths.get("C:\\Upload");
String filename = FilenameUtils.getBaseName(uploadedFile.getFileName());
String extension = FilenameUtils.getExtension(uploadedFile.getFileName());
Path file = Files.createTempFile(folder, filename + "-", "." + extension);
//try (InputStream input = uploadedFile.getInputstream()) {
// Files.copy(input, folder, StandardCopyOption.REPLACE_EXISTING);
if (file != null) {
FacesMessage message = new FacesMessage("Succesful", file.getFileName() + " was uploaded.");
FacesContext.getCurrentInstance().addMessage(null, message);
}
}
}
,這是XHTML文件:
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:jsf="http://xmlns.jcp.org/jsf"
xmlns:pt="http://xmlns.jcp.org/jsf/passthrough"
xmlns:p="http://primefaces.org/ui"
template="/WEB-INF/template/template.xhtml">
<ui:define name="title">2G</ui:define>
<ui:define name="content">
<h:form>
<h1> <font color="orange" size="7" > 2G</font></h1>
</h:form>
<h2 >Choose 2 files </h2>
<h:form>
<p:fileUpload fileUploadListener="#{Import_2G.save()}"
mode="advanced" dragDropSupport="true" update="messages"
sizeLimit="100000000000" allowTypes="/(\.|\/)(xls)$/" />
<p:growl id="messages" showDetail="true" />
</h:form>
</ui:define>
</ui:composition>
,這是我得到的錯誤:
警告[javax.enterprise.resource.webcontainer .jsf.lifecycle](默認任務-55)/admin/2g.xhtml @ 23,61 fileUploadListener =「#{Import_2G.save()}」:目標不可達,標識符'Import_2G'解析爲null:javax.el.PropertyNotFoundException :/admin/2g.xhtml @ 23,61 fileUploadListener =「#{Import_2G.save()}」:Targ等不可達,標識符 'Import_2G' 解析爲空
嘗試這樣'fileUploadListener =「#{import_2G.save()}」' –
在視圖層管理bean的默認名稱爲第一個字母小寫,這就是爲什麼它應該是'「#{ import_2G.save()}「'。 – Omar
感謝所有目標無法訪問的問題是固定的,但我得到了這個>警告[javax.enterprise.resource.webcontainer.jsf.lifecycle](默認任務13)/admin/2g.xhtml'@ 23,61'fileUploadListener =「 #{import_2G.save()}「:java.lang.NullPointerException:javax.el.ELException:/admin/2g.xhtml'@ 23,61'fileUploadListener =」#{import_2G.save()}「:java.lang.NullPointerException .NullPointerException – MOhamed