Search in sources :

Example 1 with PageContext

use of org.apache.myfaces.view.facelets.PageContext in project myfaces by apache.

the class ForEachHandler method apply.

@Override
public void apply(FaceletContext ctx, UIComponent parent) throws IOException, FacesException, FaceletException, ELException {
    int e = this.getEnd(ctx);
    Object src = null;
    ValueExpression srcVE = null;
    if (this.items != null) {
        srcVE = this.items.getValueExpression(ctx, Object.class);
        src = srcVE.getValue(ctx);
    } else {
        byte[] b = new byte[e + 1];
        for (int i = 0; i < b.length; i++) {
            b[i] = (byte) i;
        }
        src = b;
    }
    FaceletCompositionContext fcc = FaceletCompositionContext.getCurrentInstance(ctx);
    AbstractFaceletContext actx = (AbstractFaceletContext) ctx;
    // Just increment one number to ensure the prefix doesn't conflict later if two
    // c:forEach are close between each other. Note c:forEach is different from
    // c:if tag and doesn't require a section because c:forEach requires to provide
    // multiple sections starting with a specified "base" related to the element
    // position and value in the collection.
    fcc.incrementUniqueComponentId();
    String uniqueId = actx.generateUniqueFaceletTagId(fcc.generateUniqueId(), tagId);
    if (src != null) {
        PageContext pctx = actx.getPageContext();
        // c:forEach is special because it requires FaceletState even if no pss is used.
        FaceletState restoredFaceletState = ComponentSupport.getFaceletState(ctx, parent, false);
        IterationState restoredSavedOption = (restoredFaceletState == null) ? null : (IterationState) restoredFaceletState.getState(uniqueId);
        if (restoredSavedOption != null) {
            if (!PhaseId.RESTORE_VIEW.equals(ctx.getFacesContext().getCurrentPhaseId())) {
                // Refresh, evaluate and synchronize state
                applyOnRefresh(ctx, fcc, pctx, parent, uniqueId, src, srcVE, restoredSavedOption);
            } else {
                // restore view, don't record iteration, use the saved value
                applyOnRestore(ctx, fcc, pctx, parent, uniqueId, src, srcVE, restoredSavedOption);
            }
        } else {
            // First time
            applyFirstTime(ctx, fcc, pctx, parent, uniqueId, src, srcVE);
        }
    }
    if (fcc.isUsingPSSOnThisView() && fcc.isRefreshTransientBuildOnPSS() && !fcc.isRefreshingTransientBuild()) {
        // Mark the parent component to be saved and restored fully.
        ComponentSupport.markComponentToRestoreFully(ctx.getFacesContext(), parent);
    }
    if (fcc.isDynamicComponentSection()) {
        ComponentSupport.markComponentToRefreshDynamically(ctx.getFacesContext(), parent);
    }
}
Also used : FaceletCompositionContext(org.apache.myfaces.view.facelets.FaceletCompositionContext) ValueExpression(jakarta.el.ValueExpression) FaceletStateValueExpression(org.apache.myfaces.view.facelets.el.FaceletStateValueExpression) PageContext(org.apache.myfaces.view.facelets.PageContext) FaceletState(org.apache.myfaces.view.facelets.tag.faces.FaceletState) AbstractFaceletContext(org.apache.myfaces.view.facelets.AbstractFaceletContext)

Example 2 with PageContext

use of org.apache.myfaces.view.facelets.PageContext in project myfaces by apache.

the class ForEachHandler method setVar.

private void setVar(FaceletContext ctx, UIComponent parent, String uniqueId, String base, String v, ValueExpression ve, ValueExpression srcVE) {
    AbstractFaceletContext actx = ((AbstractFaceletContext) ctx);
    PageContext pctx = actx.getPageContext();
    if (srcVE != null) {
        FaceletState faceletState = ComponentSupport.getFaceletState(ctx, parent, true);
        faceletState.putBinding(uniqueId, base, ve);
        // Put the indirect EL into context
        ValueExpression fve = new FaceletStateValueExpression(uniqueId, base);
        pctx.getAttributes().put(v, fve);
    } else {
        pctx.getAttributes().put(v, ve);
    }
}
Also used : FaceletStateValueExpression(org.apache.myfaces.view.facelets.el.FaceletStateValueExpression) ValueExpression(jakarta.el.ValueExpression) FaceletStateValueExpression(org.apache.myfaces.view.facelets.el.FaceletStateValueExpression) PageContext(org.apache.myfaces.view.facelets.PageContext) FaceletState(org.apache.myfaces.view.facelets.tag.faces.FaceletState) AbstractFaceletContext(org.apache.myfaces.view.facelets.AbstractFaceletContext)

Example 3 with PageContext

use of org.apache.myfaces.view.facelets.PageContext in project myfaces by apache.

the class LegacyForEachHandler method apply.

@Override
public void apply(FaceletContext ctx, UIComponent parent) throws IOException, FacesException, FaceletException, ELException {
    int s = this.getBegin(ctx);
    int e = this.getEnd(ctx);
    int m = this.getStep(ctx);
    Integer sO = this.begin != null ? Integer.valueOf(s) : null;
    Integer eO = this.end != null ? Integer.valueOf(e) : null;
    Integer mO = this.step != null ? Integer.valueOf(m) : null;
    boolean t = this.getTransient(ctx);
    Object src = null;
    ValueExpression srcVE = null;
    if (this.items != null) {
        srcVE = this.items.getValueExpression(ctx, Object.class);
        src = srcVE.getValue(ctx);
    } else {
        byte[] b = new byte[e + 1];
        for (int i = 0; i < b.length; i++) {
            b[i] = (byte) i;
        }
        src = b;
    }
    FaceletCompositionContext fcc = FaceletCompositionContext.getCurrentInstance(ctx);
    if (src != null) {
        try {
            fcc.startComponentUniqueIdSection();
            AbstractFaceletContext actx = (AbstractFaceletContext) ctx;
            PageContext pctx = actx.getPageContext();
            Iterator<?> itr = this.toIterator(src);
            if (itr != null) {
                int i = 0;
                // move to start
                while (i < s && itr.hasNext()) {
                    itr.next();
                    i++;
                }
                String v = this.getVarName(ctx);
                String vs = this.getVarStatusName(ctx);
                ValueExpression ve = null;
                ValueExpression vO = this.capture(v, pctx);
                ValueExpression vsO = this.capture(vs, pctx);
                int mi = 0;
                Object value = null;
                try {
                    boolean first = true;
                    while (i <= e && itr.hasNext()) {
                        value = itr.next();
                        // set the var
                        if (v != null) {
                            if (t || srcVE == null) {
                                if (value == null) {
                                    pctx.getAttributes().put(v, null);
                                } else {
                                    pctx.getAttributes().put(v, ctx.getExpressionFactory().createValueExpression(value, Object.class));
                                }
                            } else {
                                ve = this.getVarExpr(srcVE, src, value, i);
                                pctx.getAttributes().put(v, ve);
                            }
                        }
                        // set the varStatus
                        if (vs != null) {
                            IterationStatus itrS = new IterationStatus(first, !itr.hasNext(), i, sO, eO, mO, value);
                            if (t || srcVE == null) {
                                if (srcVE == null) {
                                    pctx.getAttributes().put(vs, null);
                                } else {
                                    pctx.getAttributes().put(vs, ctx.getExpressionFactory().createValueExpression(itrS, Object.class));
                                }
                            } else {
                                ve = new IterationStatusExpression(itrS);
                                pctx.getAttributes().put(vs, ve);
                            }
                        }
                        // execute body
                        this.nextHandler.apply(ctx, parent);
                        // increment steps
                        mi = 1;
                        while (mi < m && itr.hasNext()) {
                            itr.next();
                            mi++;
                            i++;
                        }
                        i++;
                        first = false;
                    }
                } finally {
                    // Remove them from PageContext
                    if (v != null) {
                        pctx.getAttributes().put(v, vO);
                    } else {
                        pctx.getAttributes().remove(v);
                    }
                    if (vs != null) {
                        pctx.getAttributes().put(vs, vsO);
                    } else {
                        pctx.getAttributes().remove(vs);
                    }
                }
            }
        } finally {
            fcc.endComponentUniqueIdSection();
        }
    }
    if (fcc.isUsingPSSOnThisView() && fcc.isRefreshTransientBuildOnPSS() && !fcc.isRefreshingTransientBuild()) {
        // Mark the parent component to be saved and restored fully.
        ComponentSupport.markComponentToRestoreFully(ctx.getFacesContext(), parent);
    }
    if (fcc.isDynamicComponentSection()) {
        ComponentSupport.markComponentToRefreshDynamically(ctx.getFacesContext(), parent);
    }
}
Also used : FaceletCompositionContext(org.apache.myfaces.view.facelets.FaceletCompositionContext) AbstractFaceletContext(org.apache.myfaces.view.facelets.AbstractFaceletContext) ValueExpression(jakarta.el.ValueExpression) PageContext(org.apache.myfaces.view.facelets.PageContext)

Example 4 with PageContext

use of org.apache.myfaces.view.facelets.PageContext in project myfaces by apache.

the class DefaultFaceletContext method popPageContext.

@Override
public PageContext popPageContext() {
    if (!_isolatedPageContext.isEmpty()) {
        int currentPageContext = _isolatedPageContext.size() - 1;
        PageContext itc = _isolatedPageContext.get(currentPageContext);
        _isolatedPageContext.remove(currentPageContext);
        if (!_isolatedPageContext.isEmpty()) {
            _defaultVarMapper.setPageContext(getPageContext());
        } else {
            _defaultVarMapper.setPageContext(null);
        }
        return itc;
    }
    return null;
}
Also used : PageContext(org.apache.myfaces.view.facelets.PageContext)

Aggregations

PageContext (org.apache.myfaces.view.facelets.PageContext)4 ValueExpression (jakarta.el.ValueExpression)3 AbstractFaceletContext (org.apache.myfaces.view.facelets.AbstractFaceletContext)3 FaceletCompositionContext (org.apache.myfaces.view.facelets.FaceletCompositionContext)2 FaceletStateValueExpression (org.apache.myfaces.view.facelets.el.FaceletStateValueExpression)2 FaceletState (org.apache.myfaces.view.facelets.tag.faces.FaceletState)2