📄 htmlform.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.component.html;import javax.faces.component.ActionSource;import javax.faces.component.UIForm;import javax.faces.context.FacesContext;import javax.faces.el.MethodBinding;import javax.faces.el.ValueBinding;import javax.faces.event.ActionEvent;import javax.faces.event.ActionListener;import javax.faces.event.FacesEvent;import javax.faces.event.PhaseId;import org.springframework.util.Assert;import de.mindmatters.faces.application.ViewBuilder;import de.mindmatters.faces.el.ConstantMethodBinding;/** * Represents an HTML form element. <strong>HtmlForm</strong> implements * {@link ActionSource} - therefore it is possible to define an action and an * action listener attribute. Methodbindings declared as action listener will be * invoked on <em>every</em> form submit <em>before</em> action listeners of * any nested {@link javax.faces.component.UICommand} will be executed. * Futhermore this form supports stateless action listeners as direct children * and the immediate attribute. * * <p> * <strong>HtmlForm</strong> supports the <code>"method"</code> attribute. * Supported methods are 'POST' (default method) and 'GET'. 'GET' is * <strong>only</strong> supported if an appropriate {@link ViewBuilder} is * configured as {@link javax.faces.application.ViewHandler}. * </p> * * <p> * By default, the rendererType property must be set to * <code>"de.mindmatters.faces.Form"</code>. This value can be changed by * calling the setRendererType() method. * </p> * * @author Andreas Kuhrwahl */public class HtmlForm extends javax.faces.component.html.HtmlForm implements ActionSource { /** * {@link MethodBinding} that delegates to an internal {@link ValueBinding}. * * @author Andreas Kuhrwahl * */ private class ValueBindingToMethodBinding extends MethodBinding { /** The internal {@link ValueBinding}. */ private final ValueBinding vb; /** * Creates the {@link MethodBinding}. * * @param vb * the internal {@link ValueBinding} */ public ValueBindingToMethodBinding(final ValueBinding vb) { super(); this.vb = vb; } /** * {@inheritDoc} */ public Class getType(final FacesContext context) { return vb.getType(context); } /** * {@inheritDoc} */ public Object invoke(final FacesContext context, final Object[] params) { return vb.getValue(context); } } /** The standard renderer type for this component. */ public static final String RENDERER_TYPE = "de.mindmatters.faces.Form"; /** The standard component type for this component. */ public static final String COMPONENT_TYPE = "de.mindmatters.faces.HtmlForm"; /** The standard component type for this component. */ public static final String COMPONENT_FAMILY = UIForm.COMPONENT_FAMILY; /** * The {@link MethodBinding} that, when invoked, yields the literal outcome * value. */ private MethodBinding action; /** The immediate flag. */ private Boolean immediate; /** The transient flag. */ private Boolean isTransient; /** The method of this form (supports 'POST' and 'GET'). */ private String method; /** The action listener {@link MethodBinding}. */ private MethodBinding actionListener; /** * The standard constructor. */ public HtmlForm() { super(); setRendererType(RENDERER_TYPE); forceClientIdGeneration(); } /** * Forces the generation of the client id. */ private void forceClientIdGeneration() { FacesContext context = getFacesContext(); if (context.getViewRoot() != null) { Assert.notNull(this.getClientId(getFacesContext())); } } /** * {@inheritDoc} */ public MethodBinding getAction() { if (this.action != null) { return this.action; } ValueBinding vb = getValueBinding("action"); if (vb != null) { return new ValueBindingToMethodBinding(vb); } else { return null; } } /** * {@inheritDoc} */ public void setAction(final MethodBinding action) { Assert.isInstanceOf(ConstantMethodBinding.class, action); this.action = action; } /** * {@inheritDoc} */ public MethodBinding getActionListener() { return this.actionListener; } /** * {@inheritDoc} */ public void setActionListener(final MethodBinding actionListener) { this.actionListener = actionListener; } /** * {@inheritDoc} */ public void addActionListener(final ActionListener listener) { addFacesListener(listener); } /** * {@inheritDoc} */ public ActionListener[] getActionListeners() { return (ActionListener[]) getFacesListeners(ActionListener.class); } /** * {@inheritDoc} */ public void removeActionListener(final ActionListener listener) { removeFacesListener(listener); } /** * {@inheritDoc} */ public void setImmediate(final boolean immediate) { this.immediate = Boolean.valueOf(immediate); } /** * {@inheritDoc} */ public boolean isImmediate() { if (this.immediate != null) { return this.immediate.booleanValue(); } ValueBinding vb = getValueBinding("immediate"); Boolean v = Boolean.FALSE; if (vb != null) { v = (Boolean) vb.getValue(getFacesContext()); } return v.booleanValue(); } /** * Returns the method of this form - supports 'POST' or 'GET'. * * @return The method of this form or <code>null</code> if not set */ public String getMethod() { if (this.method != null) { return this.method; } String currentMethod = null; ValueBinding vb = getValueBinding("method"); if (vb != null) { currentMethod = (String) vb.getValue(getFacesContext()); } if (currentMethod == null || !currentMethod.equalsIgnoreCase("get")) { currentMethod = "post"; } setMethod(currentMethod); return getMethod(); } /** * Sets the method of this form - supports 'POST' or 'GET'. * * @param method * The method 'POST' or 'GET' */ public void setMethod(final String method) { this.method = method; if (this.method != null) { this.method = this.method.toLowerCase(); } } /** * {@inheritDoc} */ public Object saveState(final FacesContext context) { Object[] values = new Object[6]; values[0] = super.saveState(context); values[1] = saveAttachedState(context, this.action); values[2] = saveAttachedState(context, this.actionListener); values[3] = this.immediate; values[4] = this.method; return values; } /** * {@inheritDoc} */ public void restoreState(final FacesContext context, final Object state) { Object[] values = (Object[]) state; super.restoreState(context, values[0]); this.action = (MethodBinding) restoreAttachedState(context, values[1]); this.actionListener = (MethodBinding) restoreAttachedState(context, values[2]); this.immediate = (Boolean) values[3]; this.method = (String) values[4]; } /** * {@inheritDoc} */ public void queueEvent(final FacesEvent e) { if (e instanceof ActionEvent) { if (isImmediate()) { e.setPhaseId(PhaseId.APPLY_REQUEST_VALUES); } else { e.setPhaseId(PhaseId.INVOKE_APPLICATION); } } super.queueEvent(e); } /** * {@inheritDoc} */ public void broadcast(final FacesEvent event) { super.broadcast(event); if (event instanceof ActionEvent) { MethodBinding mb = getActionListener(); if (mb != null) { mb.invoke(getFacesContext(), new Object[] { event }); } } } /** * {@inheritDoc} */ public final void setId(final String id) { super.setId(id); forceClientIdGeneration(); } /** * {@inheritDoc} */ public final boolean isTransient() { if (this.isTransient == null) { boolean calculatedTransient = false; if ("get".equals(getMethod())) { calculatedTransient = getFacesContext().getApplication() .getViewHandler() instanceof ViewBuilder; } return calculatedTransient; } else { return this.isTransient.booleanValue(); } } /** * {@inheritDoc} */ public final void setTransient(final boolean transientFlag) { if (transientFlag) { Assert .isInstanceOf( ViewBuilder.class, getFacesContext().getApplication().getViewHandler(), "Method 'get' is not allowed with the applied ViewHandler. Use a ViewBuilder instance instead."); } this.isTransient = Boolean.valueOf(transientFlag); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -