📄 restoreviewphase.java
字号:
/* * Copyright 2002-2004 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package de.mindmatters.faces.lifecycle;import java.util.Iterator;import java.util.Locale;import javax.faces.FacesException;import javax.faces.application.ViewHandler;import javax.faces.component.UIComponent;import javax.faces.component.UIInput;import javax.faces.component.UIViewRoot;import javax.faces.context.ExternalContext;import javax.faces.context.FacesContext;import javax.faces.el.ValueBinding;import javax.faces.event.PhaseId;import de.mindmatters.faces.FacesUtils;import de.mindmatters.faces.application.ViewBuilder;/** * Implements the default <em>Restore View</em> phase of the faces lifecycle. * * @author Andreas Kuhrwahl */public class RestoreViewPhase extends AbstractPhase { /** * Represents the view-id of the view the incoming request was submitted * from. * <p> * Checks whether the view-id was derived from a request parameter or from * the request path. * </p> * * @author Andreas Kuhrwahl */ private class ViewId { /** View-id from request parameter or not? */ private final boolean requestParameter; /** The view-id - used for view creation. */ private final String viewId; /** * Constructor. * * @param requestParameter * view id derived from request paramter or not? * @param viewId * the derived view id */ public ViewId(final boolean requestParameter, final String viewId) { super(); this.requestParameter = requestParameter; this.viewId = viewId; } /** * @return <code>true</code> if the view-id was derived from a request * parameter <code>false</code> otherwise */ public boolean isRequestParameter() { return requestParameter; } /** * @return the derived view id */ public String get() { return viewId; } /** * {@inheritDoc} */ public String toString() { return this.viewId; } } /** * @param context * {@link FacesContext} for the current request * @return the derived {@link ViewId} */ private ViewId deriveViewId(final FacesContext context) { ExternalContext externalContext = context.getExternalContext(); boolean isRequestParameter = true; String viewId = FacesUtils.decodeViewId(context); if (viewId == null) { isRequestParameter = false; viewId = externalContext.getRequestPathInfo(); if (viewId == null) { viewId = externalContext.getRequestServletPath(); if (viewId != null) { int dot = viewId.lastIndexOf('.'); if (dot != -1) { viewId = viewId.substring(0, dot) + FacesUtils.getDefaultSuffix(context); } } } } return new ViewId(isRequestParameter, viewId); } /** * {@inheritDoc} */ public final PhaseId getId() { return PhaseId.RESTORE_VIEW; } /** * {@inheritDoc} */ protected final void executePhase(final FacesContext context) { if (context == null) { throw new FacesException("FacesContext"); } UIViewRoot viewRoot = context.getViewRoot(); if (viewRoot != null) { Locale locale = context.getExternalContext().getRequestLocale(); context.getViewRoot().setLocale(locale); executePerComponentActions(context, viewRoot); } else { ViewId viewId = deriveViewId(context); if (viewId.get() == null) { throw new FacesException("ViewId not derived"); } ViewHandler viewHandler = context.getApplication().getViewHandler(); boolean isViewBuilder = viewHandler instanceof ViewBuilder; viewRoot = restoreView(context, viewHandler, viewId.get()); if (viewRoot == null && viewId.isRequestParameter() && isViewBuilder) { viewRoot = buildView(context, (ViewBuilder) viewHandler, viewId .get()); } if (viewRoot == null) { viewRoot = createView(context, viewHandler, viewId.get()); } context.setViewRoot(viewRoot); if (!isViewBuilder) { executePerComponentActions(context, viewRoot); } } } /** * Perform whatever actions are required to restore the view associated with * the specified {@link FacesContext} and <code>viewId</code>. Default * implementation delegates to * {@link ViewHandler#restoreView(FacesContext, String)}. * * @param context * {@link FacesContext} for the current request * @param viewHandler * {@link ViewHandler} for the current request * @param viewId * the view identifier for the current request * @return the restored view * @throws FacesException * in case of errors */ protected UIViewRoot restoreView(final FacesContext context, final ViewHandler viewHandler, final String viewId) { return viewHandler.restoreView(context, viewId); } /** * Perform whatever actions are required to build the view associated with * the specified {@link FacesContext} and <code>viewId</code>. Default * implementation delegates to * {@link ViewBuilder#buildView(FacesContext, String)}. * * @param context * {@link FacesContext} for the current request * @param viewBuilder * {@link ViewBuilder} for the current request * @param viewId * the view identifier for the current request * @return the built view * @throws FacesException * in case of errors */ protected UIViewRoot buildView(final FacesContext context, final ViewBuilder viewBuilder, final String viewId) { return viewBuilder.buildView(context, viewId); } /** * Perform whatever actions are required to create the view associated with * the specified {@link FacesContext} and <code>viewId</code>. Default * implementation delegates to * {@link ViewBuilder#createView(FacesContext, String)}. * * @param context * {@link FacesContext} for the current request * @param viewHandler * {@link ViewHandler} for the current request * @param viewId * the view identifier for the current request * @return the built view * @throws FacesException * in case of errors */ protected UIViewRoot createView(final FacesContext context, final ViewHandler viewHandler, final String viewId) { UIViewRoot viewRoot = viewHandler.createView(context, viewId); context.renderResponse(); return viewRoot; } /** * Do any per-component actions necessary during reconstitute. * * @param context * {@link FacesContext} for the current request * @param component * the component the actions will be applied to * @throws FacesException * in case of errors */ private void executePerComponentActions(final FacesContext context, final UIComponent component) { Iterator kids = component.getFacetsAndChildren(); while (kids.hasNext()) { executePerComponentActions(context, (UIComponent) kids.next()); } doPerComponentActions(context, component); } /** * Do any per-component actions necessary during reconstitute. * * @param context * {@link FacesContext} for the current request * @param component * the component the actions will be applied to * @throws FacesException * in case of errors */ protected void doPerComponentActions(final FacesContext context, final UIComponent component) { ValueBinding valueBinding = component.getValueBinding("binding"); if (valueBinding != null) { valueBinding.setValue(context, component); } if (component instanceof UIInput) { ((UIInput) component).setValid(true); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -