0

I am trying to implement a JSF 2.0 project with Primefaces from a tutorial. I can login with only jsf, but when I use PrimeFaces I'm getting the following error:

Error Log:

  HTTP Status 500 - /login2.xhtml @23,98 value="#{employee.userName}": Target Unreachable, identifier 'employee' resolved to null type Exception report
  message /login2.xhtml @23,98 value="#{employee.userName}": Target Unreachable, identifier 'employee' resolved to null description The server encountered an internal error that prevented it from fulfilling this request.

Exception:

javax.servlet.ServletException: /login2.xhtml @23,98 value="#{employee.userName}":     Target Unreachable, identifier 'employee' resolved to null
javax.faces.webapp.FacesServlet.service(FacesServlet.java:321)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)root cause
javax.el.PropertyNotFoundException: /login2.xhtml @23,98 value="#{employee.userName}":     Target Unreachable, identifier 'employee' resolved to null
com.sun.faces.facelets.el.TagValueExpression.getType(TagValueExpression.java:97)
com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getConvertedValue(HtmlBasicInputRenderer.java:91)
javax.faces.component.UIInput.getConvertedValue(UIInput.java:1023)
javax.faces.component.UIInput.validate(UIInput.java:953)
javax.faces.component.UIInput.executeValidate(UIInput.java:1204)
javax.faces.component.UIInput.processValidators(UIInput.java:693)
javax.faces.component.UIForm.processValidators(UIForm.java:240)
javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1081)
javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1081)
javax.faces.component.UIViewRoot.processValidators(UIViewRoot.java:1159)
com.sun.faces.lifecycle.ProcessValidationsPhase.execute(ProcessValidationsPhase.java:72)
com.sun.faces.lifecycle.Phase.doPhase(Phase.java:97)
com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:114)
javax.faces.webapp.FacesServlet.service(FacesServlet.java:308)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.57 logs. in Apache Tomcat/7.0.57

Employee.java:

package com.login.sample;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

@ManagedBean(name= "employee")
public class Employee {

    private String userName;
    private String endName;
    private String pass;
    private String email;
    private String dbPassword;
    private String dbName;
    DataSource ds;

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getEndName() {
        return endName;
    }

    public void setEndName(String endName) {
        this.endName = endName;
    }

    public String getPass() {
        return pass;
    }

    public void setPass(String pass) {
        this.pass = pass;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getDbPassword() {
        return dbPassword;
    }

    public void setDbPassword(String dbPassword) {
        this.dbPassword = dbPassword;
    }

    public String getDbName() {
        return dbName;
    }

    public void setDbName(String dbName) {
        this.dbName = dbName;
    }

    public Employee() {

        try {
            Context ctx = new InitialContext();
            ds = (DataSource) ctx.lookup("java:comp/env/jdbc/database");
        } catch (NamingException e) {
            e.printStackTrace();
        }
    }

    public String add() {
        int i = 0;
        if (userName != null) {
            PreparedStatement ps = null;
            Connection con = null;
            try {
                if (ds != null) {
                    con = ds.getConnection();
                    if (con != null) {
                        String sql = "INSERT INTO employee(firstname, lastname, password, email) VALUES(?,?,?,?)";
                        ps = con.prepareStatement(sql);
                        ps.setString(1, userName);
                        ps.setString(2, endName);
                        ps.setString(3, pass);

                        ps.setString(4, email);
                        i = ps.executeUpdate();
                        System.out.println("Data Added Successfully");
                    }
                }
            } catch (Exception e) {
                System.out.println(e);
            } finally {
                try {
                    con.close();
                    ps.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        if (i > 0) {
            return "success";
        } else {
            return "unsuccess";
        }
    }

    public void dbData(String uName) {
        if (uName != null) {
            PreparedStatement ps = null;
            Connection con = null;
            ResultSet rs = null;

            if (ds != null) {
                try {
                    con = ds.getConnection();
                    if (con != null) {
                        String sql = "select firstname,password from employee where firstname = '"
                                + uName + "'";
                        ps = con.prepareStatement(sql);
                        rs = ps.executeQuery();
                        rs.next();
                        dbName = rs.getString("firstname");
                        dbPassword = rs.getString("password");
                    }
                } catch (SQLException sqle) {
                    sqle.printStackTrace();
                }
            }
        }
    }

    public String login() {
        dbData(userName);
        if (userName.equals(dbName) && pass.equals(dbPassword)) {
            return "output";
        } else {
            return "invalid";
        }
    }

    public void logout() {
        FacesContext.getCurrentInstance().getExternalContext()
                .invalidateSession();
        FacesContext.getCurrentInstance()
                .getApplication().getNavigationHandler()
                .handleNavigation(FacesContext.getCurrentInstance(), null, "/login2.xhtml");
    }
}

login2.xhtml:

<!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:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:p="http://primefaces.prime.com.tr/ui">

    <h:head>
        <title>Login Page</title>
    </h:head>
    <h:body>
        <f:view>

            <h:form id="loginForm">
                <table frame="box">
                    <tr>
                        <th>Login Page</th>
                    </tr>
                    <tr>
                        <td><h:outputText value="Enter Your Name : " /></td>
                        <td><h:inputText id="inputName" value="#{employee.userName}"
                                         required="true" requiredMessage="Name field must not be empty" />
                        </td>
                        <td><h:message for="inputName" style="color:red" /></td>
                    </tr>
                    <tr>
                        <td><h:outputText value="Enter password :" /></td>
                        <td><h:inputSecret id="inputPassword" value="#{employee.pass}"
                                           required="true"
                                           requiredMessage="Password field must not be empty" /></td>
                        <td><h:message for="inputPassword" style="color:red" /></td>
                    </tr>
                    <tr>
                        <td><h:commandButton value="Sign In" action="#{employee.login2}" /></td>
                        <td><h:outputText value="Not a User " />
                            <h:outputLink value="register.xhtml">Sign Up</h:outputLink>
                        </td>
                    </tr>
                </table>
            </h:form>
        </f:view>
    </h:body>
</html>

faces-config.xml:

<?xml version="1.0" encoding="UTF-8"?>

<faces-config 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"
              version="2.0">
    <application>
        <message-bundle>resources.application</message-bundle>
        <resource-bundle>
            <base-name>resources.application</base-name>
            <var>ErrMsg</var>
        </resource-bundle>
        <locale-config>
            <default-locale>en</default-locale>
        </locale-config>
    </application>
    <navigation-rule>
        <description>login user</description>
        <from-view-id>/login.xhtml</from-view-id>
        <navigation-case>
            <from-action>#{user.login}</from-action>
            <from-outcome>output</from-outcome>
            <to-view-id>/success.xhtml</to-view-id>
        </navigation-case>

        <navigation-case>
            <from-action>#{user.login}</from-action>
            <from-outcome>invalid</from-outcome>
            <to-view-id>/error.xhtml</to-view-id>
        </navigation-case>
        <navigation-case>
            <from-action>#{employee.login}</from-action>
            <from-outcome>output</from-outcome>
            <to-view-id>/success.xhtml</to-view-id>
        </navigation-case>

        <navigation-case>
            <from-action>#{employee.login}</from-action>
            <from-outcome>invalid</from-outcome>
            <to-view-id>/error.xhtml</to-view-id>
        </navigation-case>
    </navigation-rule>

    <navigation-rule>
        <description>register new user</description>
        <from-view-id>/register.xhtml</from-view-id>
        <navigation-case>
            <from-action>#{user.add}</from-action>
            <from-outcome>success</from-outcome>
            <to-view-id>/success.xhtml</to-view-id>
        </navigation-case>
        <navigation-case>
            <from-action>#{user.add}</from-action>
            <from-outcome>unsuccess</from-outcome>
            <to-view-id>/unsuccess.xhtml</to-view-id>
        </navigation-case>
        <navigation-case>
            <from-action>#{employee.add}</from-action>
            <from-outcome>success</from-outcome>
            <to-view-id>/success.xhtml</to-view-id>
        </navigation-case>
        <navigation-case>
            <from-action>#{employee.add}</from-action>
            <from-outcome>unsuccess</from-outcome>
            <to-view-id>/unsuccess.xhtml</to-view-id>
        </navigation-case>
    </navigation-rule>

    <navigation-rule>
        <from-view-id>/template.xhtml</from-view-id>
        <navigation-case>
            <from-outcome>grid center</from-outcome>
            <to-view-id>/WEB-INF/templates/gridcenter.xhtml</to-view-id>
        </navigation-case>
    </navigation-rule>
    <navigation-rule>
        <from-view-id>/template.xhtml</from-view-id>
        <navigation-case>
            <from-outcome>grid left</from-outcome>
            <to-view-id>/WEB-INF/templates/gridleft.xhtml</to-view-id>
        </navigation-case>
    </navigation-rule>
    <navigation-rule>
        <from-view-id>/template.xhtml</from-view-id>
        <navigation-case>
            <from-outcome>grid top</from-outcome>
            <to-view-id>/WEB-INF/templates/gridtop.xhtml</to-view-id>
        </navigation-case>
    </navigation-rule>
    <navigation-rule>
        <from-view-id>/template.xhtml</from-view-id>
        <navigation-case>
            <from-outcome>sidebar</from-outcome>
            <to-view-id>/WEB-INF/templates/sidebar.xhtml</to-view-id>
        </navigation-case>
    </navigation-rule>
    <navigation-rule>
        <from-view-id>/template.xhtml</from-view-id>
        <navigation-case>
            <from-outcome>sidebar</from-outcome>
            <to-view-id>/WEB-INF/templates/sourceview.xhtml</to-view-id>
        </navigation-case>
    </navigation-rule>
</faces-config>

Web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
         http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         id="WebApp_ID" version="3.0">

    <display-name>jsfLoginExample</display-name>
    <welcome-file-list>
        <welcome-file>/home.xhtml</welcome-file>
    </welcome-file-list>
    <resource-ref>
        <description>Login Database</description>
        <res-ref-name>jdbc/database</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
    </resource-ref>
    <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>
        <url-pattern>*.jsf</url-pattern>
        <url-pattern>*.xhtml</url-pattern>
        <url-pattern>*.jsp</url-pattern>
    </servlet-mapping>
    <context-param>
        <description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description>
        <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
        <param-value>client</param-value>
    </context-param>
    <context-param>
        <param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
        <param-value>resources.application</param-value>
    </context-param>
    <listener>
        <listener-class>com.sun.faces.config.ConfigureListener</listener-class>
    </listener>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.faces</url-pattern>
    </servlet-mapping>
</web-app>

I am trying to create a login page with a database in which I am getting this error. Please help me on this issue as I am not getting this bean's properties.

Tiny
  • 27,221
  • 105
  • 339
  • 599
Shivu09
  • 51
  • 9
  • "*i con login with only jsf but not when i use primeface where i am getting following error*". Where are you using PrimeFaces? The namespace `http://primefaces.prime.com.tr/ui` is also unused. Do you mean that you got the mentioned exception only after you started with PrimeFaces. Thus only PrimeFaces gives you troubles (which is essentially not related to that exception)? Did you mistakenly forget to include some stuff that you might have added after you started with PrimeFaces? – Tiny Jan 07 '15 at 12:05
  • when I use and I can't able to login getting the same error as target unreachable. when I use normal with form tag I am able to login. – Shivu09 Jan 07 '15 at 12:16
  • In your previous question, you were using `` in which you set the `position` attribute using some values like `top`, `right`, `bottom`, `left` (and `center`). Those values should be `north`, `east`, `south`, `west` respectively. Are you still running with those values? Are you able to see the page with the said template corresponding the whole XHTML stuff? – Tiny Jan 07 '15 at 12:43
  • Why doesn't your managed bean have a scope? – kolossus Jan 07 '15 at 14:13
  • _doesn't your managed bean have a scope_ can you tell me more description about this because I have used 'ManagedBean(name = "employee")' to create a object for user – Shivu09 Jan 08 '15 at 04:00
  • @Tiny I can see the page with `` with various **position** attribute but not able to login with the fields i.e `` and getting same error still. – Shivu09 Jan 08 '15 at 05:04
  • This line, `Target Unreachable, identifier 'employee' resolved to null` means that the bean `Employee` being referred to be by the name `employee` is not instantiated for some unclear reasons. The mentioned exception itself has whatsoever absolutely nothing to do with PrimeFaces. No obvious symptoms are found according to the contents in the question in its current form. Please try debugging the project by isolating some piece code alternatively to get to the cause of the exception. – Tiny Jan 08 '15 at 07:14
  • "*doesn't your managed bean have a scope*" means that you have not provided your bean a scope which defaults to `@RequestScoped`. Although you this should not harm, you should always be explicit about providing a bean an appropriate scope. You might be interested in [this](http://stackoverflow.com/q/7031885/1391249). – Tiny Jan 08 '15 at 07:17
  • @Tiny Thanks for the help. Now I'll work in piece of code so that i can find the root cause of the error. Once again thanks for help.. :) – Shivu09 Jan 08 '15 at 10:09

0 Answers0