注册 登录  
 加关注
查看详情
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

流星永恒的博客

JSF,Facelets,Rich(Prime)Faces,和java的笔记

 
 
 

日志

 
 

Porting JSF 2.0′s ViewScope to Spring 3.0  

2010-06-19 14:16:23|  分类: jsf2.0 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

                                                                                    By cagataycivici(PrimeFaces)

 

 

If you are using JSF&Spring and decided upgrading to JSF 2.0, one thing you would probably like to have is the JSF 2.0′s built-in viewscope. Core Spring doesn’t provide this scope as it is more specific to JSF. No worries, using Spring’s custom scope feature it is straightforward to port JSF 2.0′s viewscope as a Spring bean scope.

 
01 package org.primefaces.spring.scope;
02   
03 import java.util.Map;
04   
05 import javax.faces.context.FacesContext;
06   
07 import org.springframework.beans.factory.ObjectFactory;
08 import org.springframework.beans.factory.config.Scope;
09   
10 public class ViewScope implements Scope {
11   
12     public Object get(String name, ObjectFactory objectFactory) {
13         Map<String,Object> viewMap = FacesContext.getCurrentInstance().getViewRoot().getViewMap();
14   
15         if(viewMap.containsKey(name)) {
16             return viewMap.get(name);
17         } else {
18             Object object = objectFactory.getObject();
19             viewMap.put(name, object);
20   
21             return object;
22         }
23     }
24   
25     public Object remove(String name) {
26         return FacesContext.getCurrentInstance().getViewRoot().getViewMap().remove(name);
27     }
28   
29     public String getConversationId() {
30         return null;
31     }
32   
33     public void registerDestructionCallback(String name, Runnable callback) {
34         //Not supported
35     }
36   
37     public Object resolveContextualObject(String key) {
38         return null;
39     }
40 }

And register this scope in Spring configuration;

 
1 <bean class="org.springframework.beans.factory.config.CustomScopeConfigurer">
2         <property name="scopes">
3             <map>
4                 <entry key="view">
5                     <bean class="org.primefaces.spring.scope.ViewScope"/>
6                 </entry>
7             </map>
8         </property>
9     </bean>

That’s it, now you can use ViewScope for JSF beans that are managed by Spring. As an example to test, assume there’s a simple counter page, each click increments the counter, as long as you stay on the same page, counter state is preserved but when you leave the page the counter is destroyed. If you access counter page again, counter will be reset.

 
01 package org.primefaces.spring.view;
02   
03 import org.springframework.context.annotation.Scope;
04 import org.springframework.stereotype.Component;
05   
06 @Component
07 @Scope("view")
08 public class CounterBean {
09   
10     private int counter = 0;
11   
12     public int getCounter() {
13         return counter;
14     }
15     public void setCounter(int counter) {
16         this.counter = counter;
17     }
18   
19     public void increment() {
20         counter++;
21     }
22 }
 
1 <h:form>
2     <h:outputText id="counter" value="#{counterBean.counter}" />
3   
4     <p:commandButton value="Count" actionListener="#{counterBean.increment}" update="counter"/>
5 </h:form>

By making JSF2′s ViewScope as an available scope to your Spring powered JSF backing beans, you can still take advantage of Spring Container and at the same time have this very handy scope. Similary FlashScope can be ported as well.

  评论这张
 
阅读(1299)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018