1

I'm new to Java-ee development, and I'm really confused about this. In fact i wanna to import file using jsf and save this file in a directory but i got always Target Unreachable. this is my bean:

@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);
            }


}
}

and this is the xhtml file :

   <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>

and this is the errors that i got :

WARNING [javax.enterprise.resource.webcontainer.jsf.lifecycle] (default task-55) /admin/2g.xhtml @23,61 fileUploadListener="#{Import_2G.save()}": Target Unreachable, identifier 'Import_2G' resolved to null: javax.el.PropertyNotFoundException: /admin/2g.xhtml @23,61 fileUploadListener="#{Import_2G.save()}": Target Unreachable, identifier 'Import_2G' resolved to null

Tiny
  • 24,933
  • 92
  • 299
  • 571
MOhamed
  • 47
  • 1
  • 2
  • 10
  • 1
    try like this `fileUploadListener="#{import_2G.save()}" ` – Subodh Joshi Mar 28 '16 at 10:13
  • the default name of the managed bean in the views layer is 1st letter lower-cased, that's why it should be `"#{import_2G.save()}"`. – Omar Mar 28 '16 at 10:15
  • Thanks all the problem of target unreachable is fixed but i got this >WARNING [javax.enterprise.resource.webcontainer.jsf.lifecycle] (default task-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 – MOhamed Mar 28 '16 at 10:24
  • and i didnt found the file uploaded in C:/Upload any idea guys ? – MOhamed Mar 28 '16 at 10:25
  • It mean `import_2G` is null now i think u can find out why it is null or why it is not initialized.This is not the same problem so you should ask another question with proper example – Subodh Joshi Mar 28 '16 at 10:27
  • @MOhamed, have you declared the managed-bean's constructor ? – Omar Mar 28 '16 at 10:33
  • i know that is another problem and im trying to resolve it but if u know how to upload file in a directory or some ideas it will help me ^^ u know im a beginner --' – MOhamed Mar 28 '16 at 10:34
  • Do you have this line in the managed-bean class `public Import_2G() { }` ? – Omar Mar 28 '16 at 10:37
  • ah the default constructor of the class yes i have it – MOhamed Mar 28 '16 at 10:40

1 Answers1

1

First add name in your managedBean

@ManagedBean(name="import_2G")
@RequestScoped
public class Import_2G{
   ......
    ......
   ......
}

then access it with this name in your jsf/xhtml pages

<p:fileUpload fileUploadListener="#{import_2G.save()}"
                mode="advanced" dragDropSupport="true" update="messages"
                sizeLimit="100000000000" allowTypes="/(\.|\/)(xls)$/" />

and this is also a valid scenario

An example of using the @ManagedBean annotation in a class is as follows:

@ManagedBean
@SessionScoped
public class Import_2G{
...
}

but in this case if you are accessing(Variable/methods) in jsf pages use import_2G and not Import_2G

@ManagedBean is not specified, then the managed bean name will default to class name portion of the fully qualified class name. In this case it would be import_2G.

Subodh Joshi
  • 10,006
  • 23
  • 84
  • 177