http://cagataycivici.wordpress.com/2010/02/17/port-jsf-2-0s-viewscope-to-spring-3-0/
译注:作者Cagatay Civici
是PrimeFaces
项目的创建者,也是MyFaces
的重要开发人员,他还是JSF2.0标准(JSR314)
的专家组成员之一,对JSF有在非常丰富的经验。
如果你正在使用JSF和Spring并且打算升级到JSF 2.0,或许你乐意去使用JSF2.0内建的viewscope。Spring不支持这个scope,因为它是特定于JSF2.0规范的。不过不用担心,使用Spring的自定义scope功能可以代理JSF2.0的viewscope,使其成为一个Spring bean的scope。
package org.primefaces.spring.scope;
import java.util.Map;
import javax.faces.context.FacesContext;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.config.Scope;
blic class ViewScope implements Scope {
public Object get(String name, ObjectFactory objectFactory) {
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;
}
}
public Object remove(String name) {
return FacesContext.getCurrentInstance().getViewRoot().getViewMap().remove(name);
}
public String getConversationId() {
return null;
}
public void registerDestructionCallback(String name, Runnable callback) {
//Not supported
}
public Object resolveContextualObject(String key) {
return null;
}
}
然后再Spring中注册这个自定义的scope:
<bean class="org.springframework.beans.factory.config.CustomScopeConfigurer">
<property name="scopes">
<map>
<entry key="view">
<bean class="org.primefaces.spring.scope.ViewScope"/>
</entry>
</map>
</property>
</bean>
现在就可以使用Spring的bean作为JSF的ViewScope的ManagedBean了。做一个例子测试一下,假设有一个简单的计数器页面,每点击一下计数器就随之增加。只要你待在这个页面,计数器的状态就被保持,直到你离开这个页面,计数器才被销毁。如果重新访问这个页面的话,计数器将重新开始。
package org.primefaces.spring.view;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@Component
@Scope("view")
public class CounterBean {
private int counter = 0;
public int getCounter() {
return counter;
}
public void setCounter(int counter) {
this.counter = counter;
}
public void increment() {
counter++;
}
}
<h:form>
<h:outputText id="counter" value="#{counterBean.counter}" />
<p:commandButton value="Count" actionListener="#{counterBean.increment}" update="counter"/>
</h:form>
让Spring提供JSF可以使用的ViewScope的backing beans,你可以享受到Spring容器提供的好处,同时也可以使用这个很方便的scope。对于JSF2.0中的FlashScope可以用同样的方式来实现。
分享到:
相关推荐
jSF2.0与Spring整合实例+教程
JSF2.0标签手册DQSV
Mojarra JSF 2.0库文件。开发JSF2.0 Web应用时放在lib目录下。
JSF 2.0 Programming Cookbook JSF PAGE Ajax JSF J2EE Eclipse IDE support
JSF2.0实战 - 6、TextBox示例代码
这个例子集成了Spring,JPA,JSF2.0,并且提供了比较容易理解,常用的增,删,改,查示例,希望大家会用到,如有不对的地方,请赐教! ^_^
jsf2.0的开发配置说明,和简单的配置例子,开发例子,及JSF2.0的所有标签的详细分析。
JSF2.0实战 - 10、自定义Ajax更新方式源代码
JSF2.0_开发入门JSF2.0_开发入门JSF2.0_开发入门JSF2.0_开发入门JSF2.0_开发入门JSF2.0_开发入门JSF2.0_开发入门
jsf2.0+richfaces4.2.3需要的jar包
jsf2.0 hibernate3.2 spring2.5环境所有jar包,包括richfaces mysql log4j 在eclipse里手动配置环境经常会因jar出现种种问题,我把完整的jsf2.0 spring2.5 hibernate3整合所有jar传上来希望有用 如果有需要我会传上一...
使用 Netbeans 开发JSF 2.0,使用Primefaces 及JPA ,一步一步指导如何开发
JSF2.0+Spring+Hibernate
个人收集的JSF2.0的开发简介 共64页 讲解了绝大部份JSF2.0的新特性
利用JSF2.0和servlet3.0做的上传的例子,并实现单个文件上传的组件,虽然有些小限制,但是对单个上传功能完全可以实现。花了一整天的时间来研究啊,不容易!顶起吧。有什么想法请发表评论
JSF2.0系列简介.doc 和 文档想配的源代码,后面的AJAX的例子做,留给你们自己完成.希望能帮助到大家
在Java EE 6中使用JSF 2.0简化页面制作 在Java EE 6中使用JSF 2.0简化页面制作 在Java EE 6中使用JSF 2.0简化页面制作
JSF2.0 Tutorial,总共18个章节,详细介绍了jsf2.0的overview, new feature, managedbean, el, ajax,spring集成等,并配source code.由于文件比较大切分成了3个压缩包。
JSF2.0 Tutorial,总共18个章节,详细介绍了jsf2.0的overview, new feature, managedbean, el, ajax,spring集成等,并配source code.由于文件比较大切分成了3个压缩包。
JSF2.0 Tutorial,总共18个章节,详细介绍了jsf2.0的overview, new feature, managedbean, el, ajax,spring集成等,并配source code.由于文件比较大切分成了3个压缩包。