2011-10-22 43 views
-1

我新的JSF,並試圖創建JSF登錄腳本。它成功地重定向到loginsuccess和loginfailure。但是,當談到重定向用戶當用戶輸入的密碼不正確的3倍,它不重定向 - >它給了我這個錯誤 - >無法找到與-view-id的「/折射率匹配導航的情況下。 xhtml'進行操作'#{login.checkLogin}',並得到'loginlocked'結果。我正在使用Netbeans 7.0,無法找到faces-config.xml。JSF簡單的登錄屏幕 - 三個錯誤嘗試鎖定

的Index.html

<?xml version='1.0' encoding='UTF-8' ?> 
    <!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"> 


<head><title>JSF Login</title></head> 
<body> 
<h:form> 
<table> 
<tr> 
<td><h:outputText value="Username: " /></td> 
<td><h:inputText id="loginname" 
value="#{login.userName}" /> 
</td> 
</tr> 
<tr> 
<td><h:outputText value="Password: " /></td> 
<td><h:inputSecret id="password" 
value="#{login.password}" /> 
</td> 
</tr> 
<tr> 
<td> </td> 
<td><h:commandButton value="Login" 
action="#{login.checkLogin}"/> 
</td> 
</tr> 
</table> 
<h:outputLabel value="#{login.label1}" /> 
</h:form> 
</body> 
</html> 

loginBean.java

package login; 

import javax.faces.bean.ManagedBean; 
import javax.faces.bean.RequestScoped; 

@ManagedBean(name="login") 
@RequestScoped 
public class loginBean { 
private String userName; 
private String password; 
private String label1; 
private static int numOfAttempts = 0; 
/** Creates a new instance of loginBean */ 
public loginBean() { 
} 

/** 
* @return the userName 
*/ 
public String getUserName() { 
    return userName; 
} 

/** 
* @param userName the userName to set 
*/ 
public void setUserName(String userName) { 
    this.userName = userName; 
} 

/** 
* @return the password 
*/ 
public String getPassword() { 
    return password; 
} 

/** 
* @param password the password to set 
*/ 
public void setPassword(String password) { 
    this.password = password; 
} 

/** 
* @return the label1 
*/ 
public String getLabel1() { 
    return label1; 
} 

/** 
* @param label1 the label1 to set 
*/ 
public void setLabel1(String label1) { 
    this.label1 = label1; 
} 

public String checkLogin() 
{ 

    if (userName.equals("Neetu") && password.equals("123456")) 
    { 
     this.setLabel1("Login Success"); 
     return "loginsuccess"; 
    } 
    else 
    { 
     numOfAttempts++; 
     if (numOfAttempts >= 3) 
     { 
     this.setLabel1("Account Locked"); 
     return "loginlocked"; 
     } 
     else 
     { 
      this.setLabel1("Login Failure" + numOfAttempts); 
      return "loginfailure"; 
     } 

    } 
} 

}

回答

1

假設你是依靠新的JSF 2.0的隱式導航功能,那麼這個錯誤基本上意味着,你沒有一個loginlocked.xhtml文件。

faces-config.xml通常是在webapp的/WEB-INF文件夾。但在JSF 2.0中,你不一定需要它。


無關的具體問題,還有在bean的一個主要設計問題:

private static int numOfAttempts = 0; 

一個static類,應用程序範圍的所有實例共享。如果一個訪問者輸入錯誤密碼3次,然後每隔遊客被鎖定。雖然,當您修復代碼中的另一個錯誤時會發生這種情況。你是不是在checkLogin()方法檢查是否numOfAttempts已超過您檢查用戶名/密碼。因此,任何被鎖定的人在輸入正確的用戶名/密碼時仍可以成功登錄。

工作你的邏輯思維和數學技能:)