gpt4 book ai didi

spring - Spring中的JSF View范围

转载 作者:行者123 更新时间:2023-12-04 04:06:41 24 4
gpt4 key购买 nike

Spring 3.0中是否有类似JSF @ViewScoped的范围?我有一个使用JSF + Spring的应用程序,其中后备bean由Spring管理。我没有在Spring中找到像JSF wiew scope这样的范围。我看到了博客Porting JSF 2.0’s ViewScope to Spring 3.0,但是对我来说不起作用。

这是我对自定义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:
<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>

最佳答案

最近,我创建了可以解决此问题的Maven工件。

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

关于spring - Spring中的JSF View范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13005421/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com