无法创建托管bean的UserBean - JSF(Unable to create manag

2019-09-19 21:55发布

我想创建这样当用户名和密码验证,ID,名字,姓氏在类内访问设置以及访问一个DB简单的登录页面。 我得到这个错误,但:

javax.servlet.ServletException: Unable to create managed bean UserBean.  The following problems were found:
 - Property firstName for managed bean UserBean does not exist.  Check that appropriate getter and/or setter methods exist.
 - Property id for managed bean UserBean does not exist.  Check that appropriate getter and/or setter methods exist.
 - Property lastName for managed bean UserBean does not exist.  Check that appropriate getter and/or setter methods exist.
javax.faces.webapp.FacesServlet.service(FacesServlet.java:321)

我有ID,名字,姓氏的getter但没有制定者,因为他们验证后进行设置。

这里是一流的UserBean

public class UserBean {
private static String password, username, id, firstName, lastName;
public enum DBfields { BLANK, USERNAME, ID, PASSWORD, FIRSTNAME, LASTNAME }; 

public String getPassword() {
    return password;
}

public void setPassword( String value ) {
    password = value;
}

public String getUsername() {
    return username;
}

public void setUsername( String value ) {
    username = value;
}

public static String getId() {
    return id;
}

public static String getFirstname() {
    return firstName;
}

public static String getLastname() {
    return lastName;
}

public String loginUser() throws Exception { 

    if( loginValidate( username, password ) )
        return "success";
    else
        return "fail";
}

public static boolean loginValidate( String username, String Password ) throws Exception{

    String DBuser;
    String DBpass = "asdf";
    PreparedStatement table = connectToTable( "firstsql", "users");
    ResultSet row = table.executeQuery();;

    while(row.next()){
        DBuser = row.getString(DBfields.USERNAME.name());
        if(username.equals(DBuser)){
            DBpass = row.getString(DBfields.PASSWORD.name());
            break;
        }
    }

    if(password.equals(DBpass)){
        id = row.getString(DBfields.ID.name());
        firstName = row.getString(DBfields.FIRSTNAME.name());
        lastName = row.getString(DBfields.LASTNAME.name());
        return true;
    } else
        return false;
}

我的脸,配置

 <?xml version="1.0" encoding="UTF-8"?>
<faces-config version="2.0" xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xi="http://www.w3.org/2001/XInclude"
 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>UserBean</managed-bean-name>
  <managed-bean-class>com.jfsdemo.bean.UserBean</managed-bean-class>
  <managed-bean-scope>request</managed-bean-scope>
  <managed-property>
   <property-name>firstName</property-name>
   <property-class>java.lang.String</property-class>
   <value/>
  </managed-property>
  <managed-property>
   <property-name>id</property-name>
   <property-class>java.lang.String</property-class>
   <value/>
  </managed-property>
  <managed-property>
   <property-name>lastName</property-name>
   <property-class>java.lang.String</property-class>
   <value/>
  </managed-property>
  <managed-property>
   <property-name>password</property-name>
   <property-class>java.lang.String</property-class>
   <value/>
  </managed-property>
  <managed-property>
   <property-name>username</property-name>
   <property-class>java.lang.String</property-class>
   <value/>
  </managed-property>
 </managed-bean>
 <navigation-rule>
  <from-view-id>/login.xhtml</from-view-id>
  <navigation-case>
   <from-action>#{UserBean.loginUser}</from-action>
   <from-outcome>success</from-outcome>
   <to-view-id>/timesheet.xhtml</to-view-id>
  </navigation-case>
  <navigation-case>
   <from-action>#{UserBean.loginUser}</from-action>
   <from-outcome>fail</from-outcome>
   <to-view-id>/login.xhtml</to-view-id>
  </navigation-case>
 </navigation-rule>
 <navigation-rule>
  <from-view-id>/timesheet.xhtml</from-view-id>
 </navigation-rule>
</faces-config>

Answer 1:

托管属性通过setter方法设置。 所以,你真的需要为他们提供。 还要注意,Java是区分大小写的。 你的第一个和最后一个名称的属性是基于称为吸气名字firstnamelastname ,而不是firstNamelastName通过预期faces-config.xml 。 因此修复它。 使用getFirstName()setFirstName()代替getFirstname()等。 或者,更好的,只是你的IDE自动生成基于属性的getter / setter方法。

顺便说一句,为什么这些属性声明static ? 这样,他们将在所有使用Web应用程序的所有用户共享。 这真的是你想要的吗?



文章来源: Unable to create managed bean UserBean - JSF