2012-12-23 65 views
1
<h:form id="form"> 
Username: <br/> 
<h:inputText id="username" value="#{home.username}" required="true" > 
<f:validator validatorId="loginValidator"></f:validator> 
<f:attribute name="passwordComponent" value="#{passwordComponent}" ></f:attribute> 
</h:inputText> 
<br/> 
Password: <br /> 
<h:inputText id="password" bindig="#{passwordComponent}" value="#{home.password}" required="true"></h:inputText> 
<br/> 
<h:commandButton value="login" id="login" action="#{home.login}"></h:commandButton> 
</h:form> 



public class LoginValidator implements Validator { 

@Override 
public void validate(FacesContext context, UIComponent component, Object value) 
     throws ValidatorException { 
    String username = (String)value; 
    UIInput passwordInput = (UIInput)component.getAttributes().get("passwordComponent"); 
    String password = (String) passwordInput.getValue(); 

    if(username!="aa" || password!="aa"){ 
     passwordInput.setValid(false); 
     throw new ValidatorException(new FacesMessage("Wrong username or password!")); 
    } 
} 

}JSF多個現場驗證的NullPointerException

在此線I得到NuulPointerException:

字符串密碼=(字符串)passwordInput.getValue();

爲什麼?

回答

2

單詞「綁定」的拼寫在以下行中不正確。

<h:inputText id="password" bindig="#{passwordComponent}"... 

變化bindig結合

+0

謝謝。多麼愚蠢的錯誤) –