kha*_*han 9 jsf spring jsf-2 view-scope
@ViewScoped
在Spring 3.0中是否有像JSF这样的范围?我有一个使用JSF + Spring的应用程序,其中支持bean由Spring管理.我在Spring中没有找到像JSF wiew作用域这样的范围.我看到博客将JSF 2.0的ViewScope移植到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;
}
}
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
public class ViewScope implements Scope {\n\npublic static final String VIEW_SCOPE_CALLBACKS = "viewScope.callbacks";\n\n@Override\npublic synchronized Object get(String name, ObjectFactory<?> objectFactory) {\nObject instance = this.getViewMap().get(name);\nif(instance == null){\ninstance = objectFactory.getObject();\nthis.getViewMap().put(name, instance);\n}\nreturn instance;\n}\n\n@SuppressWarnings("unchecked")\n@Override\npublic Object remove(String name) {\nObject instance = this.getViewMap().remove(name);\nif(instance == null){\nMap<String, Runnable> callbacks = (Map<String, Runnable>) this.getViewMap().get(VIEW_SCOPE_CALLBACKS);\nif(callbacks != null)\ncallbacks.remove(name);\n}\nreturn instance;\n}\n\n/**\n* Respons\xc3\xa1vel por registrar uma chamada de destrui\xc3\xa7\xc3\xa3o ao bean\n* que ser\xc3\xa1 armazenadano [b]viewMap[/b] da [b]ViewRoot[/b](nossa p\xc3\xa1gina que ser\xc3\xa1 mostrada)\n* @see #getViewMap()\n* @param name - nome do bean\n* @param runnable\n*/ \n@SuppressWarnings("unchecked")\n@Override\npublic void registerDestructionCallback(String name, Runnable runnable) {\nMap<String, Runnable> callbacks = (Map<String, Runnable>) this.getViewMap().get(VIEW_SCOPE_CALLBACKS);\nif(callbacks != null)\ncallbacks.put(name, runnable);\n}\n\n@Override\npublic Object resolveContextualObject(String key) {\nFacesContext facesContext = FacesContext.getCurrentInstance();\nFacesRequestAttributes facesResquestAttributes = new FacesRequestAttributes(facesContext);\nreturn facesResquestAttributes.resolveReference(key);\n}\n\n@Override\npublic String getConversationId() {\nFacesContext facesContext = FacesContext.getCurrentInstance();\nFacesRequestAttributes facesResquestAttributes = new FacesRequestAttributes(facesContext); \nreturn facesResquestAttributes.getSessionId() + "-" + facesContext.getViewRoot().getViewId();\n}\n\nprivate Map<String, Object> getViewMap(){\nreturn FacesContext.getCurrentInstance().getViewRoot().getViewMap();\n}\n\n}\n
Run Code Online (Sandbox Code Playgroud)\n
归档时间: |
|
查看次数: |
20781 次 |
最近记录: |