2012-07-20 176 views
0

我想創建一個訪問數據庫的簡單登錄頁面,以便在驗證用戶名和密碼時,在類中也設置id,firstName,lastName以及訪問權限。我得到這個錯誤,但:無法創建託管bean UserBean - JSF

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干將,名字,姓氏,但沒有制定者,因爲他們驗證後進行設置。

這裏是一流的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> 
+0

其中是無參數構造函數? 一個託管bean必須有一個無參數構造函數 – 2012-07-20 16:00:57

+1

@ a.u.r:我不確定我是否理解你的評論,但是你是不是混淆了作爲構造函數的'enum'?無論如何,如果類不包含任何構造函數,Java將始終隱含地提供默認的公共無參數構造函數。如果這個構造函數真的缺失,那麼OP將會得到一個完全不同的異常。 – BalusC 2012-07-20 16:02:59

+0

@BalusC,我在這裏談論缺席的默認構​​造函數;因爲它產生了一個錯誤,讓我花了很長時間才弄清楚.. 我認爲你是正確的OP提供了一個不同的例外 – 2012-07-20 16:08:41

回答

7

託管屬性通過setter方法設置。所以你真的需要提供它們。另請注意,Java區分大小寫。您的名字和姓氏屬性基於名爲firstnamelastname的吸氣劑名稱,而不是firstNamelastName,正如faces-config.xml所預期的那樣。相應地修復它。使用getFirstName()setFirstName()而不是getFirstname()等。或者,更好的是,讓IDE根據屬性自動生成getters/setters。

順便說一句,爲什麼這些屬性聲明static?這樣,他們將共享所有正在使用webapp的用戶。這真的是你想要的嗎?

+0

似乎已經修復了名字,姓氏,但爲什麼我需要一個id而不是first/lastname的setter? id在驗證後與名稱一起設置 – Austin 2012-07-20 16:17:31

+0

您還需要setter。我誤解了代碼,並認爲你已經爲他們設置了setter,但他們只是拼錯了。我更新了答案。 – BalusC 2012-07-20 16:20:22

+0

所以我爲所有3個屬性編寫了setter/getters,並且它在faces-config中與firstName lastName一起工作。接受這個作爲答案,但不知道爲什麼我必須爲屬性設置一個獨立操作的副產品,而不是設置者本身 – Austin 2012-07-20 16:33:52