目标无法访问,标识符'员工'解析为null

时间:2015-01-07 11:48:25

标签: jsf primefaces

我正在尝试使用Primefaces从教程中实现JSF 2.0项目。我只能使用jsf登录,但是当我使用PrimeFaces时,我收到以下错误:

错误日志:

  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.

例外:

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>

我正在尝试使用数据库创建一个登录页面,我收到此错误。请帮我解决这个问题,因为我没有得到这个bean的属性。

0 个答案:

没有答案