0

我想processDecodes在 ajax 请求上执行 JSF UI 组件子树。如果我指定,我会详细一点:

<f:ajax event="change" execute="componentId" render="componentPanelId" />

我希望processDecodes在从 UIViewRoot 到 ID 为 componentId 的组件以及所有 componentId 祖先的每个组件上调用它。我会这样做是因为,目标组件的 ui 自定义组件父级会进行一些评估,特别是它们缩小了 XML 文档的部分,其中 ui 组件值适用。

假设有以下 facelet 片段:

 <m:node id="root" ref="/Root">
  <m:node id="element" ref="Element">
    <m:label>Some Label</m:label>
    <m:selectOne id="componentId" ref="@Attribute1" execute="@this" render="toRender">
        ...
    </m:selectOne>
    <m:selectOne id="toRender" ref="@Attribute2">
        ...
    </m:selectOne>
  </m:node>
</m:node>

前提是我已经在自定义组件中实现了 ajax 行为,带有属性executerender. 当componentId ajax事件触发时,我会按照指定的顺序处理id为'root'、'element'和'componentId'的组件。

我尝试将其@form用作执行属性值:

<m:selectOne id="componentId" ref="@Attribute1" execute="@form" render="toRender">
    ...
</m:selectOne>   

它有效,因为 JSF 处理表单中的所有 ui 组件,但这不是所需的行为。

也许通过扩展PartialViewContextFactory

任何提示或帮助将不胜感激谢谢

4

1 回答 1

0

我找到了一个解决方案,也许不是性能最好的。我扩展PartialViewContextFactory,包装原来的PartialViewContextFactory

新类(如下所示)执行以下操作:

1)它创建一个新的CustomPartialViewContextImpl传递它原来的PartialViewContext。2) 它覆盖CustomPartialViewContextImpl.getExecuteIds(), 以便找到所有的祖先。

关于 id 列表的注释,它应该从叶子的根开始排序。

感谢 BalusC 的查找组件算法

    public class CustomPartialViewContextFactory extends PartialViewContextFactory {

        public static class CustomPartialViewContextImpl extends PartialViewContext {

            private FacesContext facesContext;
            private PartialViewContext wrappedViewContext;

            /**
             * Id to execute during ajax request.
             */
            private Collection<String> executeIds;

            public CustomPartialViewContextImpl(PartialViewContext wrappedViewContext, FacesContext facesContext) {
                this.facesContext = facesContext;
                this.wrappedViewContext = wrappedViewContext;
            }

            public Collection<String> getExecuteIds() {
                if(this.executeIds == null){                
                    this.executeIds = findAncestors(executeIds);
                }

                return this.executeIds;
            }

            // ... delegate other methods to wrappedViewContext

            private Collection<String> findAncestors(Collection<String> executeIds) {
                Set<String> ids = new HashSet<>(executeIds);
                for (String id : executeIds) {
                    findAncestors(ids, findComponent(id, facesContext));
                }


                List<String> idList = new ArrayList<>(ids);
                Collections.reverse(idList);
                return idList;
            }

            private UIComponent findComponent(final String id,  FacesContext context){
                UIViewRoot root = context.getViewRoot();
                final UIComponent[] found = new UIComponent[1];
                root.visitTree(new FullVisitContext(context), new VisitCallback() {
                    @Override
                    public VisitResult visit(VisitContext context, UIComponent component) {
                        if(component.getId().equals(id)){
                            found[0] = component;
                            return VisitResult.COMPLETE;
                        }
                        return VisitResult.ACCEPT;
                    }
                });
                return found[0];
            }

            private void findAncestors(Set<String> ids, UIComponent component) {

                if(component == null){
                    return;
                }

                UIComponent parent = component.getParent();

                if (parent == null) {
                    return;
                }


                String clientId = parent.getClientId(facesContext);
                if (StringUtils.isBlank(clientId) || ids.contains(clientId)) {
                  return;
                }

                ids.add(clientId);                                  

                findAncestors(ids, parent);
            }

        }

        private PartialViewContextFactory parentFactory;

        public CustomPartialViewContextFactory(PartialViewContextFactory parentFactory) {
            this.parentFactory = parentFactory;
        }

        @Override
        public PartialViewContext getPartialViewContext(FacesContext context) {
            return new CustomPartialViewContextImpl(parentFactory.getPartialViewContext(context), context);
        }

        @Override
        public PartialViewContextFactory getWrapped() {
            return this.parentFactory;
        }

    }
于 2013-06-18T14:35:16.173 回答