2011-10-17 13 views
20

मेरे पास एक जेएसएफ फेसलेट पृष्ठ है जो डेटा के एक तालिका को प्रदर्शित करता है कि वे किस पृष्ठ को देख रहे हैं। जब मैं पृष्ठ 1 प्रदर्शित करता हूं, तो मैं दोनों पृष्ठों के लिए डेटाबेस से डेटा प्राप्त करने के लिए view() क्रिया विधि को कॉल करता हूं और इसे बीन (दो सरणी) के निजी सदस्य फ़ील्ड के रूप में संग्रहीत करता हूं। मैं view() विधि में इंजेक्शन वार्तालाप उदाहरण पर conversation.start() पर भी कॉल करता हूं।जेएसएफ 2 वार्तालापस्कोप कैसे काम करता है?

उपयोगकर्ता क्लिक करता है "अगले" बटन (h:commandButton) पेज 2 पर जाने के लिए, मैं सरणी 2 को इंगित करने के तो यह उसकी सामग्री को बाहर प्रिंट होगा समर्थन सेम अद्यतन करने के लिए एक next() विधि को क्रियान्वित कर रहा हूँ। समस्या यह है कि, सरणी 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> 

यह विज़ार्ड के दूसरे पृष्ठ है

<!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> 

समाप्त होता है यहाँ @ConversationScoped समर्थन सेम कि शुरू होता है और बातचीत

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; 
} 

} 

मुझे लगता है कि इस उदाहरण बहुत है समाप्त होता है सरल और यह समझने में आपकी सहायता कर सकता है कि यह कैसे काम करता है।

मुझे लगता है कि लेकिन मैं 100% पर यकीन नहीं है, लेकिन अगर समर्थन सेम एक CDI सेम है ConversationScope ही काम करता है: अगर आप कुछ

नोट समझ में नहीं आता पूछो। इसका मतलब @ नामांकित एनोटेशन का उपयोग करता है। बेहतर दो बार जांचें।

+0

प्रतिक्रिया के लिए धन्यवाद। मुझे यह शाम देना होगा जब मैं इस शाम को अपने दूसरे कंप्यूटर पर वापस आऊंगा। क्या मुझे रीडायरेक्ट की आवश्यकता है या क्या मैं सिर्फ पेज पास कर सकता हूं? – Adam

+3

@ एडम फिशर आप केवल पृष्ठ को पास कर सकते हैं लेकिन मुझे हमेशा यह सुनिश्चित करने के लिए रीडायरेक्ट का उपयोग करना पसंद है :) – sfrj

+4

विस्तृत उदाहरण के लिए धन्यवाद। मेरी समस्या @ नामांकित() के बजाय @ManagedBean का उपयोग कर रही थी। – Adam

संबंधित मुद्दे