2016-04-23 8 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"; 
    } 
} 
01です

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)

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にBean内のアクションメソッドを書く)ナビゲーション・ルールに

<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}":メソッドが見つかりません:stateless.jsfbean @ 573e86b7.message()。私は私の質問を編集しました。 – JianYA

関連する問題