Kept getting Target Unreachable, identifier resolved to null in JSF web app

16,638

You have overridden the @ManagedBean by a <managed-bean> declaration on the faces-config.xml, however that <managed-bean> declaration has a different managed bean name, namely bean instead of upload.

You have basically 3 options:

  1. Use #{bean} instead of #{upload} in your view.

  2. Rename the <managed-bean-name> in faces-config.xml from bean to upload so that you can use #{upload} in your view.

  3. Get rid of <managed-bean> altogether so that the @ManagedBean will be used. The managed bean name defaults to the bean class name with 1st char lowercased, thus #{upload} should work.

For the remaining, you are not consistent with Java naming conventions. Please work on that as well. Package names should be all lowercase and class names should be CamelCase and start with uppercase.

Share:
16,638
Kevin phytagoras
Author by

Kevin phytagoras

Last year student in informatics technology at Institut Teknologi Sepuluh Nopember Indonesia. average programming skill, but eager to learn more. Now i'm working on my final project for undergraduate thesis involving opencv library

Updated on June 04, 2022

Comments

  • Kevin phytagoras
    Kevin phytagoras almost 2 years

    i have a simple problem here i am making a simple jsf web app that can upload file but i kept getting

    Target Unreachable, identifier 'upload' resolved to null
    

    here's my upload class

    package SimpleLogin;
    
    import java.io.IOException;
    
    import javax.faces.application.FacesMessage;
    import javax.faces.bean.ManagedBean;
    import javax.faces.bean.RequestScoped;
    import javax.faces.context.FacesContext;
    
    import org.apache.commons.io.FilenameUtils;
    import org.apache.myfaces.custom.fileupload.UploadedFile;
    
    @ManagedBean
    @RequestScoped
    public class upload {
    
    private UploadedFile uploadedFile;
    
    public void submit() throws IOException {
        String fileName = FilenameUtils.getName(uploadedFile.getName());
        String contentType = uploadedFile.getContentType();
        byte[] bytes = uploadedFile.getBytes();
    
        FacesContext.getCurrentInstance().addMessage(null, 
            new FacesMessage(String.format("File '%s' of type '%s' successfully uploaded!", fileName, contentType)));
    }
    
    public UploadedFile getUploadedFile() {
        return uploadedFile;
    }
    
    public void setUploadedFile(UploadedFile uploadedFile) {
        this.uploadedFile = uploadedFile;
    }
    

    }

    and my index page

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:t="http://myfaces.apache.org/tomahawk"
      >
    <h:head>
        <title>Facelet Title</title>
    <h:outputStylesheet name="css/jsfcrud.css"/>
    </h:head>
    <h:body>
         <ui:composition template="./template.xhtml">
    
            <ui:define name="body">
                    <h:form>
                    <h:commandLink action="/contacts/List" value="Show All Contacts Items"/>
                </h:form>
    
                <h:form enctype="multipart/form-data">
                    <t:inputFileUpload value="#{upload.uploadedFile}" />
                <h:commandButton value="submit" action="#{upload.submit}" />
                <h:messages />
                </h:form>
    
            </ui:define>
         </ui:composition>              
    

    oh yeah, here's my web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee /web-app_3_0.xsd">
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>/faces/*</url-pattern>
    </servlet-mapping>
    <filter>
    <filter-name>MyFacesExtensionsFilter</filter-name>
    <filter-class>org.apache.myfaces.webapp.filter.ExtensionsFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>MyFacesExtensionsFilter</filter-name>
        <servlet-name>Faces Servlet</servlet-name>
    </filter-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>faces/login.xhtml</welcome-file>
    </welcome-file-list>
    

    Sorry for the long delay here my face config.xml

    <faces-config version="2.0"
        xmlns="http://java.sun.com/xml/ns/javaee" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns /javaee/web-facesconfig_2_0.xsd">
    
    <managed-bean>  
        <managed-bean-name>user</managed-bean-name>  
        <managed-bean-class>SimpleLogin.simpleLogin</managed-bean-class>  
        <managed-bean-scope>request</managed-bean-scope>  
    </managed-bean>    
    
    <managed-bean>
         <managed-bean-name>bean</managed-bean-name>
         <managed-bean-class>SimpleLogin.Upload</managed-bean-class>
         <managed-bean-scope>session</managed-bean-scope></managed-bean>
    
    <navigation-rule>
        <from-view-id>/login.xhtml</from-view-id>
        <navigation-case>
            <from-action>
            #{simpleLogin.CheckValidUser}
            </from-action>
            <from-outcome>fail</from-outcome>
            <to-view-id>/resultforfail.xhtml</to-view-id>
        </navigation-case>
        <navigation-case>
            <from-action>
            #{simpleLogin.CheckValidUser}
            </from-action>
            <from-outcome>success</from-outcome>
            <to-view-id>/index.xhtml</to-view-id>
        </navigation-case>
    </navigation-rule>
    <application>
        <resource-bundle>
            <base-name>/Bundle</base-name>
            <var>bundle</var>
        </resource-bundle>
    </application>
    

    and my template

     <html xmlns="http://www.w3.org/1999/xhtml"
          xmlns:ui="http://java.sun.com/jsf/facelets"
          xmlns:h="http://java.sun.com/jsf/html">
         <h:head>
             <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
             <title><ui:insert name="title">Default Title</ui:insert></title>
     <h:outputStylesheet name="css/jsfcrud.css"/>
     <h:outputStylesheet name="css/cssLayout.css"/>
     <h:outputStylesheet name="css/default.css"/>
        </h:head>
    
    <h:body>
        <div id="top">
            <ui:insert name="top">Ini Header</ui:insert>
        </div>
    
        <h1>
            <ui:insert name="title"></ui:insert>
        </h1>
        <p>
            <ui:insert name="body"></ui:insert>
        </p>
    
        <div id="bottom">
            <ui:insert name="bottom">Ini Footer</ui:insert>
        </div>
    
    </h:body>
    

    what is the problem ? do i not clearly state the upload class ? Any help is good :)