2011-10-17 16 views
20

JSF faceletsページがあり、表示しているページに応じてデータの表が表示されます。 1ページ目を表示するときは、view()アクションメソッドを呼び出して、両方のページのデータをデータベースから取得し、それをBeanのプライベートメンバーフィールドとして格納します(2つの配列)。また、view()メソッドの注入された会話インスタンスでconversation.start()と呼び出します。JSF 2 ConversationScopeはどのように機能しますか?

ユーザーが2ページ目に行く「次へ」ボタン(h:commandButton)をクリックすると、next()メソッドを実行してバッキングBeanを配列2を指すように更新します。問題は、配列2はもはや存在しないということです。私はなぜ会話の範囲を失っているのかわかりません。何か案は?

//tells the object which page we are on, and thus what data to display. 
private int part = 1; 

// These arrays are filled with data but conversation scope doesn't 
// keep them on the next postback. 
private int[] part1 = new int[15], part2 = new int[15]; 

答えて

42

私たちはより良いお手伝いをするためにいくつかのコードを貼り付けてください。 会話を終了するための方法をどこから呼び出せなかったか(会話の範囲で作業するときにも必要です) (ウィザードに最適です会話スコープ)

これは、ウィザードの開始ページで

私はここで私はあなたが会話の範囲がどのように動作するかを理解する助けになると思う少し例を貼り付けます

<html xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:ui="http://java.sun.com/jsf/facelets" 
    xmlns:h="http://java.sun.com/jsf/html" 
    xmlns:f="http://java.sun.com/jsf/core"> 

<h:head> 
    <title>ConversationScoped demo CDI(Component Dependency 
    Injection)</title> 
</h:head> 

<h:body> 



    <h3>ConversationScoped demo CDI(Component Dependency Injection)</h3> 

    <p>A conversation scope provides persistence until a goal is 
    reached<br /> 
    In this example the first entered value will remain until the end 
    method is called<br /> 
    in some page.<br /> 
    This is a really useful gadget, for making registration wizards and 
    similar tools...</p> 

    <h:form> 
     <h:outputText value="Type something" /> 
     <h:inputText value="#{ supportBB.someValue}" /> 
     <h:commandButton value="continue" action="#{ supportBB.onClick}" /> 
    </h:form> 

</h:body> 
</html> 

これは、ウィザードの2ページ目です

私はこの例は非常にあると思い
<!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:ui="http://java.sun.com/jsf/facelets" 
    xmlns:h="http://java.sun.com/jsf/html" 
    xmlns:f="http://java.sun.com/jsf/core"> 

<h:head> 
    <title>ConversationScoped demo CDI(Component Dependency 
    Injection)</title> 
</h:head> 

<h:body> 



    <h3>This is the next page(The value is saved in the conversation)</h3> 

     <h4><h:outputText value="#{ supportBB.someValue}"/></h4> 

    <h:form>   
     <h:commandButton value="Finish conversation" action="#{ supportBB.onKeepGoing}"/> 
    </h:form> 

</h:body> 
</html> 

そして、これはスコープがここ

<!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:ui="http://java.sun.com/jsf/facelets" 
    xmlns:h="http://java.sun.com/jsf/html" 
    xmlns:f="http://java.sun.com/jsf/core"> 

<h:head> 
    <title>ConversationScoped demo CDI(Component Dependency 
    Injection)</title> 
</h:head> 

<h:body> 



    <h3>This is the last page.The value still saved in conversation(until the end() method is called)</h3> 

    <h4><h:outputText value="#{ supportBB.someValue}" /></h4> 

    <h:form> 
     <h:outputText 
      value="Click finish to end the conversation(So saved values are disposed)" /> 
     <h:commandButton value="Finish" action="#{ supportBB.onFinish}" /> 
    </h:form> 

</h:body> 
</html> 

を終了ページ会話

package backingbeans; 

import java.io.Serializable; 

import javax.enterprise.context.Conversation; 
import javax.enterprise.context.ConversationScoped; 
import javax.inject.Inject; 
import javax.inject.Named; 

@Named() 
@ConversationScoped() 
public class SupportBB implements Serializable { 
    private static final long serialVersionUID = 1L; 
    private String someValue; 
    @Inject 
    private Conversation conversation; 

    // Control start and end of conversation 
    public void start() { 
     conversation.begin(); 
    } 

    public void end() { 
     conversation.end(); 
    } 

    // Navigation 
    public String onClick() { 
     if(someValue.equals("") || conversation == null) { 
      return "";//Dont go anywhere if the there was no input the field 
     } 
     start(); 
     return "nextpage?faces-redirect=true"; 
    } 

public String onKeepGoing() { 
    return "finish?faces-redirect=true"; 
} 

public String onFinish() { 
    end();// If triggered when there is no conversation(i.e URL Navigation) 
      // there will be an exception 
    return "index?faces-redirect=true"; 
} 

// Getters & Setters 
public String getSomeValue() { 
    return someValue; 
} 

public void setSomeValue(String someValue) { 
    this.someValue = someValue; 
} 

} 

を開始し、終了@ConversationScopedバッキングBeanですシンプルで、どのように動作するかを理解するのに役立ちます。あなたが何か

NOTEを理解していない場合は確認する:

私は思いますが、私は100%を確認していないが、バッキングBeanがCDI BeanであればConversationScopeにのみ機能します。この平均はアノテーション@Namedを使用します。より良いダブルチェック。

+0

ありがとうございます。私は今晩私の他のコンピュータに戻ったときにそれを試してみる必要があります。リダイレクトが必要か、ページを渡すだけですか? – Adam

+3

@Adam Fisherちょうどページを渡すことができますが、私は常にリダイレクトを使用して、確かめるのが好きです。 – sfrj

+4

詳細な例をありがとうございます。私の問題は、スコープで@Named()の代わりに@ManagedBeanを使用していました。 – Adam

関連する問題