【发布时间】:2012-10-11 21:47:35
【问题描述】:
Spring 3.0 中是否有类似 JSF @ViewScoped 的范围?我有一个使用 JSF+Spring 的应用程序,其中支持 bean 由 Spring 管理。我在 Spring 中没有找到像 JSF wiew 范围这样的范围。我看到了博客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>
【问题讨论】:
-
我的这篇文章可能会有所帮助:stackoverflow.com/q/12884822/1055089
-
是的,但是在我的代码中实现不工作
-
能否请您发布代码?我在我的应用程序中使用了相同的方法并且它有效。我也在使用 JSF2 + Spring 3...
-
是的,我正在使用相同的 JSF2 + Spring 3 ...我已经粘贴了 ..
-
你能详细说明什么不起作用吗?只是为了确保您的代码可以与 @Scope("request") 一起使用?
标签: spring jsf jsf-2 view-scope