Spring 中的 JSF 视图范围

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13005421/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 05:27:16  来源:igfitidea点击:

JSF View scope in Spring

springjsfjsf-2view-scope

提问by khan

Is there any scope like JSF @ViewScopedin Spring 3.0? I have an application using JSF+Spring where backing beans are managed by Spring. I didn't find any scope like JSF wiew scope in Spring. I saw the blog Porting JSF 2.0's ViewScope to Spring 3.0, but it didn't work for me.

@ViewScopedSpring 3.0 中是否有类似 JSF 的范围?我有一个使用 JSF+Spring 的应用程序,其中支持 bean 由 Spring 管理。我在 Spring 中没有找到像 JSF wiew 范围这样的任何范围。我看到博客Porting JSF 2.0's ViewScope to Spring 3.0,但它对我不起作用。

Here's my attempt on the custom Spring scope:

这是我对自定义 Spring 范围的尝试:

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:

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>

回答by rumatoest

Recently I've created maven artifact which will solve this problem.

最近我创建了 maven artifact 来解决这个问题。

See my github javaplugs/spring-jsfrepository.

请参阅我的 github javaplugs/spring-jsf存储库。

回答by SRy

I did something like this without Porting bean to Spring. It's working for me.

我在没有将 bean 移植到 Spring 的情况下做了这样的事情。它对我有用。

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

回答by Alfaville

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

回答by Alfaville

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

}

回答by Sathish Kumar

I have tried a work around for the Jsf view bean memory leak issue for both Jsf 2.1 & Jsf 2.2. Try the code in following link Memory leak with ViewScoped bean?. It will clear the view bean in session while navigating to next page.

我已经尝试解决 Jsf 2.1 和 Jsf 2.2 的 Jsf 视图 bean 内存泄漏问题。尝试以下链接中的代码内存泄漏与 ViewScoped bean?. 导航到下一页时,它将清除会话中的视图 bean。