2016-04-23 76 views
0

我想知道這是否可能。我有一個登錄表單,可以接收用戶的ID和密碼。如果它在後端檢出,則會顯示另一個表單。到目前爲止,我實際上是將登錄詳細信息發送到另一個xhtml頁面進行處理。但是,如果用戶錯誤,頁面仍會顯示。JSF用AJAX顯示錶格

這是我的代碼

的index.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://xmlns.jcp.org/jsf/html"> 
<h:head> 
    <title>Index</title> 
</h:head> 
<h:body> 
    <h:form> 
     <p>Login</p> 
     <h:inputText value="#{jsfbean.username}"/> 
     <h:inputText value="#{jsfbean.password}"/> 
     <h:commandButton action="#{jsfbean.message}" value="Results Page"/> 
    </h:form> 
</h:body> 
</html> 

導航規則

<?xml version='1.0' encoding='UTF-8'?> 
<faces-config version="2.2" 
      xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"> 


<navigation-rule> 
    <from-view-id>>index.xhtml</from-view-id> 
    <navigation-case> 
     <from-action>#{jsfbean.getMessage}</from-action> 
     <from-outcome>Success</from-outcome> 
     <to-view-id>result.xhtml</to-view-id> 
    </navigation-case> 
    <navigation-case> 
     <from-action>#{jsfbean.getMessage}</from-action> 
     <from-outcome>Fail</from-outcome> 
     <to-view-id>index.xhtml</to-view-id> 
    </navigation-case> 
</navigation-rule> 

JSF豆

public String getMessage() { 
    if(getUser(this.username,this.password)==true){ 
     this.userID = aUser.getUserid(); 
     this.secqn = aUser.getSecqn(); 
     this.secans = aUser.getSecans(); 
     return "Success"; 
    }else{ 
     return "Fail"; 
    } 
} 

Structure

回答

0

當用戶輸入用戶名和密碼時,您只是重定向到另一個頁面。

1)你需要修改你的行動:

<h:form> 
     <p>Login</p> 
     <h:inputText value="#{jsfbean.username}"/> 
     <h:inputText value="#{jsfbean.password}"/> 
     <h:commandButton action="#{jsfbean.authenticateUser}" value="Results Page"/> 
    </h:form> 

2)寫操作方法你的bean裏面

public String authenticateUser(){ 
    if (username!=null&&password!=null) { 
     // Write your business logic here 
     if (username.equals("username")&&password.equals("password")) { 
      return "SUCCESS";// In your case it is result 
     } 
    } 
    return "LOGIN"; 
} 

3)添加導航規則

<navigation-rule> 
      <from-view-id>/pages/*</from-view-id> 
      <navigation-case> 
       <from-outcome>LOGIN</from-outcome> 
       <to-view-id>/pages/public/login.xhtml</to-view-id> 
      </navigation-case> 
      <navigation-case> 
       <from-action>#{authenticateController.authenticateUser}</from-action> 
       <from-outcome>LOGIN</from-outcome> 
       <to-view-id>/pages/public/login.xhtml</to-view-id> 
      </navigation-case> 
      <navigation-case> 
       <from-action>#{authenticateController.authenticateUser}</from-action> 
       <from-outcome>SUCCESS</from-outcome> 
       <to-view-id>/pages/private/home.xhtml</to-view-id> 
       <redirect/> 
      </navigation-case> 
</navigation-rule> 
+0

謝謝!但是,這是JSF 1的導航規則,還是我可以使用它2? – JianYA

+0

您也可以將它用於JSF 2。 – Unknown

+0

對不起,我正在執行此操作,並收到錯誤javax.el.MethodNotFoundException:/index.xhtml @ 13,80 action =「#{jsfbean.message}」:Method not found:stateless.jsfbean @ 573e86b7.message()。我編輯了我的問題。 – JianYA