📄 htmlformrenderer.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.render.html;import java.io.IOException;import javax.faces.FactoryFinder;import javax.faces.component.ActionSource;import javax.faces.component.UIComponent;import javax.faces.component.UIForm;import javax.faces.context.FacesContext;import javax.faces.context.ResponseWriter;import javax.faces.el.MethodBinding;import javax.faces.event.ActionEvent;import javax.faces.render.RenderKit;import javax.faces.render.RenderKitFactory;import javax.faces.render.Renderer;import org.springframework.util.Assert;import de.mindmatters.faces.FacesUtils;import de.mindmatters.faces.application.ViewBuilder;/** * <strong>HtmlFormRenderer</strong> is a class that renders a * <code>UIForm</code> as a HTML Form. * * @author Andreas Kuhrwahl * */public final class HtmlFormRenderer extends AbstractRendererBase { /** The original form renderer of the underlying JSF implementation. */ private Renderer formRenderer; /** * {@inheritDoc} */ public void decode(final FacesContext context, final UIComponent component) { getOriginalHtmlFormRenderer().decode(context, component); if (context.getExternalContext().getRequestParameterMap().containsKey( component.getClientId(context))) { ActionEvent actionEvent = new ActionEvent(component); component.queueEvent(actionEvent); } } /** * {@inheritDoc} */ public void encodeBegin(final FacesContext context, final UIComponent component) throws IOException { if (context == null || component == null) { throw new NullPointerException("FacesContext or component"); } if (component.isRendered()) { ResponseWriter writer = context.getResponseWriter(); writer.startElement("form", component); String clientId = component.getClientId(context); writer.writeAttribute("id", clientId, null); writer.writeAttribute("name", clientId, null); writer.writeAttribute("method", component.getAttributes().get( "method"), "method"); writer.writeAttribute("action", getAction(context, component), "action"); String styleClass = (String) component.getAttributes().get( "styleClass"); if (styleClass != null) { writer.writeAttribute("class", styleClass, "styleClass"); } String acceptcharset = (String) component.getAttributes().get( "acceptcharset"); if (acceptcharset != null) { writer.writeAttribute("accept-charset", acceptcharset, "acceptcharset"); } writePassThruAttributes(writer, component); writeBooleanPassThruAttributes(writer, component); writer.writeText("\n", null); } } /** * Evaluates the action attribute of the form <code>component</code>. * * @param context * {@link FacesContext} for the current request * @param component * The associated UIComponent * @return The action value for the action attribute. */ private String getAction(final FacesContext context, final UIComponent component) { String action = null; if (component instanceof ActionSource) { MethodBinding actionBinding = ((ActionSource) component) .getAction(); if (actionBinding != null) { action = (String) actionBinding.invoke(context, null); } } if (action == null) { String viewId = context.getViewRoot().getViewId(); action = context.getApplication().getViewHandler().getActionURL( context, viewId); } return context.getExternalContext().encodeActionURL(action); } /** * {@inheritDoc} */ public void encodeEnd(final FacesContext context, final UIComponent component) throws IOException { if (component.isRendered()) { ResponseWriter writer = context.getResponseWriter(); writer.startElement("input", null); writer.writeAttribute("type", "hidden", null); writer.writeAttribute("name", FacesUtils.VIEWID_PARAM, null); writer.writeAttribute("id", FacesUtils.VIEWID_PARAM, null); writer.writeAttribute("value", FacesUtils.encodeViewId(context), null); writer.endElement("input"); writer.startElement("input", null); writer.writeAttribute("type", "hidden", null); writer.writeAttribute("name", component.getClientId(context), null); writer.writeAttribute("id", component.getClientId(context), null); writer.endElement("input"); if (component.isTransient() && "post".equals(component.getAttributes().get("method"))) { writer.startElement("input", null); writer.writeAttribute("type", "hidden", null); writer.writeAttribute("name", FacesUtils.TRANSIENT_PARAM, null); writer.writeAttribute("id", FacesUtils.TRANSIENT_PARAM, null); writer .writeAttribute("value", FacesUtils.TRANSIENT_PARAM, null); writer.endElement("input"); } } boolean isTransient = component.isTransient(); if (isTransient) { ViewBuilder.markForTransientState(context); } try { getOriginalHtmlFormRenderer().encodeEnd(context, component); } finally { if (isTransient) { ViewBuilder.unmarkTransientState(context); } } } /** * {@inheritDoc} */ public String convertClientId(final FacesContext context, final String clientId) { return getOriginalHtmlFormRenderer().convertClientId(context, clientId); } /** * {@inheritDoc} */ public void encodeChildren(final FacesContext context, final UIComponent component) throws IOException { getOriginalHtmlFormRenderer().encodeChildren(context, component); } /** * {@inheritDoc} */ public Object getConvertedValue(final FacesContext context, final UIComponent component, final Object submittedValue) { return getOriginalHtmlFormRenderer().getConvertedValue(context, component, submittedValue); } /** * {@inheritDoc} */ public boolean getRendersChildren() { return getOriginalHtmlFormRenderer().getRendersChildren(); } /** * Returns the original form renderer of the underlying JSF implementation. * * @return The original form renderer. */ private Renderer getOriginalHtmlFormRenderer() { if (this.formRenderer == null) { RenderKit renderKit = ((RenderKitFactory) FactoryFinder .getFactory(FactoryFinder.RENDER_KIT_FACTORY)) .getRenderKit(null, RenderKitFactory.HTML_BASIC_RENDER_KIT); this.formRenderer = renderKit.getRenderer(UIForm.COMPONENT_FAMILY, "javax.faces.Form"); Assert.notNull(this.formRenderer); } return formRenderer; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -