• JSF结合Spring 引入ViewScope


      当JSF项目的faceConfig中配置了Spring的配置代码

    1 <application>
    2         <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
    3 </application>

      那么JSF里所有的bean都将接受Spring的管理,Spring对实例提供了三种作用域,分别是session、request、application。但JSF的作用域就比较多了,它还有ViewScope、Conversation等等。其中ViewScope用的比较多,翻阅国外站点,找到一个primeFaces论坛提供的可用方法:

    1. 首先创建一个Spring的自定义作用域类,代码如下:

      

     1 import java.util.Map;
     2 
     3 import javax.faces.context.FacesContext;
     4 
     5 import org.springframework.beans.factory.ObjectFactory;
     6 import org.springframework.beans.factory.config.Scope;
     7 
     8 public class ViewScope implements Scope {
     9 
    10     @Override
    11     public Object get(String name, ObjectFactory<?> objectFactory) {
    12 //        System.out.println("**************************************************");
    13 //        System.out.println("-------------------- Getting objects For View Scope ----------");
    14 //        System.out.println("**************************************************");
    15         Map<String, Object> viewMap = FacesContext.getCurrentInstance().getViewRoot().getViewMap();
    16         if(viewMap.containsKey(name)){
    17             return viewMap.get(name);
    18         }else{
    19             Object object = objectFactory.getObject();
    20             viewMap.put(name, object);
    21             return object;
    22         }
    23     }
    24 
    25     @Override
    26     public String getConversationId() {
    27         return null;
    28     }
    29 
    30     @Override
    31     public void registerDestructionCallback(String arg0, Runnable arg1) {
    32 
    33     }
    34 
    35     @Override
    36     public Object remove(String name) {
    37 //        System.out.println("**************************************************");
    38 //        System.out.println("-------------------- View Scope object Removed ----------");
    39 //        System.out.println("**************************************************");
    40 
    41         if (FacesContext.getCurrentInstance().getViewRoot() != null) {
    42             return FacesContext.getCurrentInstance().getViewRoot().getViewMap().remove(name);
    43         } else {
    44             return null;
    45         }
    46     }
    47 
    48     @Override
    49     public Object resolveContextualObject(String arg0) {
    50         return null;
    51     }
    52 
    53 }

    2. 在Spring的配置文件中配置viewScope的作用域。

      

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

    3. 最后是如何引用了,引用比较简单,就是在配置的bean里,加入一个属性 scope="view"即可。ViewScope比RequestScope级别要大,这样对于JSF的里的ajax请求是相当有用的,只要不导航到其他页面,bean就不会离开作用域。

    其他:因为ViewScope在进入页面时会新建,在离开页面时会销毁,所以可以利用这个特点做一些bean的初始化及数据释放销毁的操作,借助于@PostConstruct和@PreDestroy注解的方法就可以达到。

  • 相关阅读:
    项目中 2个或者多个EF模型 表名称相同会导致生成的实体类 覆盖的解决方法
    Validation failed for one or more entities. See ‘EntityValidationErrors’解决方法;关于如何查看 EntityValidationErrors 详细信息的解决方法
    深夜杳杳
    我在学习
    IIS下新建虚拟目录
    Win10资源管理器异常
    Docker笔记三:Docker数据卷和DockerFile
    Docker笔记二:Docker拷贝,提交,进入容器和阿里云加速
    ElasticSearch7.x系列三:Logstash的使用
    ElasticSearch7.x系列二:Kibana的使用和C#的Nest客户端
  • 原文地址:https://www.cnblogs.com/bigbang92/p/jsf_spring_viewScope.html
Copyright © 2020-2023  润新知