2013-06-04 137 views
0

我正在使用JSF 2.這是我的控制器。從控制器獲取驗證消息

package com.acc.validations; 

import java.io.Serializable; 
import javax.faces.application.FacesMessage; 
import javax.faces.bean.ManagedBean; 
import javax.faces.bean.SessionScoped; 
@ManagedBean(name="user") 
@SessionScoped 
public class Controller implements Serializable{ 

private static final long serialVersionUID = 1L; 
String name; 
String password; 

public String getName() { 
    return name; 
} 
public void setName(String name) { 
    this.name = name; 
} 
public String getPassword() { 
    return password; 
} 
public void setPassword(String password) { 
    this.password = password; 
} 

private String checkUser(String name,String password) { 

    if(("admin".equalsIgnoreCase(name)) && ("admin".equalsIgnoreCase(password))){ 
     return "result"; 
    }else{ 
     /*System.out.println("Enter Valid details");*/ 
     FacesMessage msg = new FacesMessage("Authentication Failed.","Authentication Failed"); 
     msg.setSeverity(FacesMessage.SEVERITY_ERROR); 
     return null; 
    } 
} 
public void execute(){ 
    checkUser(name, password); 
} 

}

這是我的XHTML頁面。

<?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" 
    xmlns:f="http://java.sun.com/jsf/core" > 
<h:body> 

<h1>Custom validator in JSF 2.0</h1> 

    <h:form > 

    <h:panelGrid columns="3"> 

    User Name: 

     <h:inputText id="username" value="#{user.name}" 
     required="true" > 
     <f:validateLength minimum="4" maximum="15" /> 
     </h:inputText> 

     <h:message for="username" style="color:red" /> 
    Password : 
     <h:inputSecret id="password" value="#{user.password}" 
     required="true" > 
     <f:validator validatorId="com.acc.validations.PasswordValidator" /> 
     </h:inputSecret> 
    </h:panelGrid> 

    <h:commandButton value="Submit" action="#{user.execute}" /> 


     </h:form> 

    </h:body> 
</html> 

我的要求是,如果用戶輸入用戶名和密碼爲「admin」的控制應該重定向到result.xhtml否則它應該在我寫在控制器上FacesContext中顯示的信息。 這是我result.xhtml

<?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"> 
<h:body> 
<h1>Custom validator in JSF 2.0</h1> 
    <h:panelGrid columns="2"> 
    Welcome to User : 
    <h:outputText value="#{user.name}" /> 
    </h:panelGrid> 
</h:body> 
</html> 

我不能eventhough呈現result.xhtml我輸入admin作爲用戶名和密碼,還我沒有得到任何消息,而我也進入錯誤credentials.And我我沒有得到任何錯誤。請您在這方面幫助我,以便按照我的要求工作。

+0

代碼。代碼在哪裏? – kolossus

回答

0

您是否嘗試過這樣的事情: 在你.xhtml

<p:inputText id="j_username" required="true" value="#{login.nomLogin}" /> 

在您的「控制器」

@ManagedBean(name="login") 
@SessionScoped 

public class LoginBean implements Serializable { 


     private String nomLogin; 
     private Boolean isConnected = false; 
     //Getter and Setter for this one 

public String checkValidUser(){ 
//To see the value of login 
System.out.println("Login a pour valeur :"+ getNomLogin()); 
if((getNomLogin().equals("admin")== true){ 
isConnected =true; 
return "ok"; 
}}} 

這是怎麼我的登錄作品,你可以做同樣的事情用布爾值取決於你所需要的。

我想你可以測試值,如果你有類似的東西: 你有可能測試這個勇氣,例如,如果有人連接我的菜單隱藏和顯示,如果管理員連接:

<p:panel id="HidePanel" header="Menu caché" rendered="#{login.isConnected == 'true'}" /> 
+0

嗨我得到值的控制器我必須顯示驗證消息,如果我已經提到上述條件是true或false.How我可以從控制器得到消息 – Padmini

+0

Plop,我編輯我的答案與測試控制器的例子值。你能用一些代碼例子編輯你的第一條消息,看看你正在試圖做什麼,請^ _ ^ – Freest