2012-10-22 10 views
9

में स्कोप देखें स्प्रिंग 3.0 में जेएसएफ @ViewScoped जैसे कोई गुंजाइश है? मेरे पास जेएसएफ + स्प्रिंग का उपयोग करके एक एप्लीकेशन है जहां बैकिंग बीन्स स्प्रिंग द्वारा प्रबंधित किए जाते हैं। मुझे स्प्रिंग में जेएसएफ व्यू स्कोप की तरह कोई गुंजाइश नहीं मिली। मैंने ब्लॉग Porting JSF 2.0’s ViewScope to Spring 3.0 देखा, लेकिन यह मेरे लिए काम नहीं करता था।जेएसएफ स्प्रिंग

import java.util.Map; 

import javax.faces.context.FacesContext; 

import org.springframework.beans.factory.ObjectFactory; 
import org.springframework.beans.factory.config.Scope; 

/** 
* Implements the JSF View Scope for use by Spring. This class is registered as a Spring bean with the CustomScopeConfigurer. 
*/ 
public class ViewScope implements Scope { 

    public Object get(String name, ObjectFactory<?> objectFactory) { 

     System.out.println("**************************************************"); 
     System.out.println("-------------------- Getting objects For View Scope ----------"); 
     System.out.println("**************************************************"); 
     if (FacesContext.getCurrentInstance().getViewRoot() != null) { 
      Map<String, Object> viewMap = FacesContext.getCurrentInstance().getViewRoot().getViewMap(); 
      if (viewMap.containsKey(name)) { 
       return viewMap.get(name); 
      } else { 
       Object object = objectFactory.getObject(); 
       viewMap.put(name, object); 
       return object; 
      } 
     } else { 
      return null; 
     } 
    } 

    public Object remove(String name) { 
     System.out.println("**************************************************"); 
     System.out.println("-------------------- View Scope object Removed ----------"); 
     System.out.println("**************************************************"); 

     if (FacesContext.getCurrentInstance().getViewRoot() != null) { 
      return FacesContext.getCurrentInstance().getViewRoot().getViewMap().remove(name); 
     } else { 
      return null; 
     } 
    } 

    public void registerDestructionCallback(String name, Runnable callback) { 
     // Do nothing 
    } 

    public Object resolveContextualObject(String key) {   return null; 
    } 

    public String getConversationId() { 
     return null; 
    } 

} 

application-context.xml:

<bean class="org.springframework.beans.factory.config.CustomScopeConfigurer"> 
    <property name="scopes"> 
      <map> 
       <entry key="view"> 
        <bean class="com.delta.beans.ViewScope"/> 
       </entry> 
      </map> 
     </property> 
</bean> 
+1

मेरा यह पोस्ट सहायता कर सकता है: http://stackoverflow.com/q/12884822/1055089 – Vrushank

+0

हाँ लेकिन कार्यान्वयन मेरे कोड – khan

+0

में काम नहीं कर रहा है क्या आप कोड पोस्ट कर सकते हैं ?? मैंने अपने ऐप में इसका इस्तेमाल किया और यह काम किया। मैं जेएसएफ 2 + स्प्रिंग 3 का भी उपयोग कर रहा हूं ... – Vrushank

उत्तर

1
public class ViewScope implements Scope { 

public static final String VIEW_SCOPE_CALLBACKS = "viewScope.callbacks"; 

@Override 
public synchronized Object get(String name, ObjectFactory<?> objectFactory) { 
Object instance = this.getViewMap().get(name); 
if(instance == null){ 
instance = objectFactory.getObject(); 
this.getViewMap().put(name, instance); 
} 
return instance; 
} 

@SuppressWarnings("unchecked") 
@Override 
public Object remove(String name) { 
Object instance = this.getViewMap().remove(name); 
if(instance == null){ 
Map<String, Runnable> callbacks = (Map<String, Runnable>) this.getViewMap().get(VIEW_SCOPE_CALLBACKS); 
if(callbacks != null) 
callbacks.remove(name); 
} 
return instance; 
} 

/** 
* Responsável por registrar uma chamada de destruição ao bean 
* que será armazenadano [b]viewMap[/b] da [b]ViewRoot[/b](nossa página que será mostrada) 
* @see #getViewMap() 
* @param name - nome do bean 
* @param runnable 
*/ 
@SuppressWarnings("unchecked") 
@Override 
public void registerDestructionCallback(String name, Runnable runnable) { 
Map<String, Runnable> callbacks = (Map<String, Runnable>) this.getViewMap().get(VIEW_SCOPE_CALLBACKS); 
if(callbacks != null) 
callbacks.put(name, runnable); 
} 

@Override 
public Object resolveContextualObject(String key) { 
FacesContext facesContext = FacesContext.getCurrentInstance(); 
FacesRequestAttributes facesResquestAttributes = new FacesRequestAttributes(facesContext); 
return facesResquestAttributes.resolveReference(key); 
} 

@Override 
public String getConversationId() { 
FacesContext facesContext = FacesContext.getCurrentInstance(); 
FacesRequestAttributes facesResquestAttributes = new FacesRequestAttributes(facesContext); 
return facesResquestAttributes.getSessionId() + "-" + facesContext.getViewRoot().getViewId(); 
} 

private Map<String, Object> getViewMap(){ 
return FacesContext.getCurrentInstance().getViewRoot().getViewMap(); 
} 

} 
2
public class ViewScopeCallbackRegistrer implements ViewMapListener { 

@SuppressWarnings("unchecked") 
@Override 
public void processEvent(SystemEvent event) throws AbortProcessingException { 
if (event instanceof PostConstructViewMapEvent) { 
PostConstructViewMapEvent viewMapEvent = (PostConstructViewMapEvent) event; 
UIViewRoot viewRoot = (UIViewRoot) viewMapEvent.getComponent(); 
viewRoot.getViewMap().put(ViewScope.VIEW_SCOPE_CALLBACKS, 
new HashMap<String, Runnable>()); 
} else if (event instanceof PreDestroyViewMapEvent) { 
PreDestroyViewMapEvent viewMapEvent = (PreDestroyViewMapEvent) event; 
UIViewRoot viewRoot = (UIViewRoot) viewMapEvent.getComponent(); 
Map<String, Runnable> callbacks = (Map<String, Runnable>) viewRoot 
.getViewMap().get(ViewScope.VIEW_SCOPE_CALLBACKS); 
if (callbacks != null) { 
for (Runnable c : callbacks.values()) { 
c.run(); 
} 
callbacks.clear(); 
} 
} 
} 

@Override 
public boolean isListenerForSource(Object source) { 
return source instanceof UIViewRoot; 
} 

} 
3

मैं वसंत के लिए सेम Porting बिना कुछ इस तरह किया

यहां कस्टम वसंत गुंजाइश पर मेरी प्रयास है। यह मेरे लिए काम कर रहा है।

@ManagedBean(name="bean") 
@ViewScoped // actual jsf viewscoped only with javax.faces.viewscoped import 
public class Bean implements 
Serializable { 


@ManagedProperty(value="#{appService}") // Spring Manged Bean and singleton 
private transient AppService appService; 

    // Getting AppService Object which is singleton in the application during deserialization 
private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException { 
      stream.defaultReadObject(); 
      FacesContext context = FacesContext.getCurrentInstance(); 
      appService = (AppService)context.getApplication() 
       .evaluateExpressionGet(context, "#{appService}", AppService.class); 
     } 
} 
+0

यह एक शुद्ध जेएसएफ समाधान है। यह ठीक काम करता है, लेकिन इसमें वसंत की विशेषताओं की कमी है। उदाहरण के लिए, यह पहलू उन्मुख प्रोग्रामिंग की अनुमति नहीं देता है। –

+0

मुझे 'स्प्रिंग एओपी' के बारे में बहुत यकीन नहीं है। बन गया मैंने उस समय मेरी आवश्यकता के अनुसार इसका परीक्षण किया। लेकिन मेरे पास 'स्प्रिंग कोर' था जो पूरी तरह से मेरे लिए काम करता था। – SRy

+0

आह, मैं देखता हूं। सबसे अधिक संभावना है कि आपने spring.config पर SpringBeanFacesELResolver को जोड़कर अपने जेएसएफ समाधान को वसंत समाधान में परिवर्तित कर दिया है। यदि हां, तो एओपी को ठीक काम करना चाहिए। –

-2

मैं दोनों JSF 2.1 & JSF 2.2 के लिए JSF दृश्य सेम स्मृति रिसाव जारी करने के लिए चारों ओर एक काम की कोशिश की है। निम्नलिखित लिंक Memory leak with ViewScoped bean? में कोड आज़माएं। अगले पृष्ठ पर नेविगेट करते समय सत्र में बीन दृश्य को साफ़ कर देगा।

+1

बनाते समय 'प्रोटोटाइप = सत्य' निर्दिष्ट कर सकते हैं। आप जेएसएफ विशिष्ट नहीं हैं। और इस सवाल से पूरी तरह से असंबंधित, बीटीडब्ल्यू। –

4

हाल ही में मैंने मैवेन आर्टिफैक्ट बनाया है जो इस समस्या को हल करेगा।

मेरे github javaplugs/spring-jsf भंडार देखें।

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

  • कोई संबंधित समस्या नहीं^_^