📄 form.java
字号:
public void onFormComponent(final FormComponent formComponent) { if (formComponent.isVisibleInHierarchy()) { // Clear input from form component formComponent.clearInput(); } } }); } /** * /** Registers an error feedback message for this component * * @param error * error message * @param args * argument replacement map for ${key} variables */ public final void error(String error, Map args) { error(new MapVariableInterpolator(error, args).toString()); } /** * Gets the IFormSubmittingComponent which submitted this form. * * @return The component which submitted this form, or null if the processing was not triggered * by a registered IFormSubmittingComponent */ public final IFormSubmittingComponent findSubmittingButton() { IFormSubmittingComponent submittingComponent = (IFormSubmittingComponent)getPage().visitChildren( IFormSubmittingComponent.class, new IVisitor() { public Object component(final Component component) { // Get submitting component final IFormSubmittingComponent submittingComponent = (IFormSubmittingComponent)component; // Check for component-name or component-name.x request string if (submittingComponent.getForm() != null && submittingComponent.getForm().getRootForm() == Form.this && (getRequest().getParameter(submittingComponent.getInputName()) != null || getRequest().getParameter( submittingComponent.getInputName() + ".x") != null)) { if (!component.isVisible()) { throw new WicketRuntimeException("Submit Button " + submittingComponent.getInputName() + " (path=" + component.getPageRelativePath() + ") is not visible"); } return submittingComponent; } return CONTINUE_TRAVERSAL; } }); return submittingComponent; } /** * Gets the default IFormSubmittingComponent. If set (not null), a hidden submit component will * be rendered right after the form tag, so that when users press enter in a textfield, this * submit component's action will be selected. If no default component is set (it is null), * nothing additional is rendered. * <p> * WARNING: note that this is a best effort only. Unfortunately having a 'default' button in a * form is ill defined in the standards, and of course IE has it's own way of doing things. * </p> * There can be only one default submit component per form hierarchy. So if you want to get the * default component on a nested form, it will actually delegate the call to root form. </b> * * @return The submit component to set as the default IFormSubmittingComponent, or null when you * want to 'unset' any previously set default IFormSubmittingComponent */ public final IFormSubmittingComponent getDefaultButton() { if (isRootForm()) { return defaultSubmittingComponent; } else { return getRootForm().getDefaultButton(); } } /** * This generates a piece of javascript code that sets the url in the special hidden field and * submits the form. * * Warning: This code should only be called in the rendering phase for form components inside * the form because it uses the css/javascript id of the form which can be stored in the markup. * * @param url * The interface url that has to be stored in the hidden field and submitted * @return The javascript code that submits the form. */ public final CharSequence getJsForInterfaceUrl(CharSequence url) { Form root = getRootForm(); return new AppendingStringBuffer("document.getElementById('").append( root.getHiddenFieldId()).append("').value='").append(url).append( "';document.getElementById('").append(root.getJavascriptId()).append("').submit();"); } /** * Gets the maximum size for uploads. If null, the setting * {@link IApplicationSettings#getDefaultMaximumUploadSize()} is used. * * @return the maximum size */ public Bytes getMaxSize() { if (maxSize == null) { return getApplication().getApplicationSettings().getDefaultMaximumUploadSize(); } return maxSize; } /** * Returns the root form or this, if this is the root form. * * @return root form or this form */ public Form getRootForm() { Form form; Form parent = this; do { form = parent; parent = (Form)form.findParent(Form.class); } while (parent != null); return form; } /** * Returns the prefix used when building validator keys. This allows a form to use a separate * "set" of keys. For example if prefix "short" is returned, validator key short.Required will * be tried instead of Required key. * <p> * This can be useful when different designs are used for a form. In a form where error messages * are displayed next to their respective form components as opposed to at the top of the form, * the ${label} attribute is of little use and only causes redundant information to appear in * the message. Forms like these can return the "short" (or any other string) validator prefix * and declare key: short.Required=required to override the longer message which is usually * declared like this: Required=${label} is a required field * <p> * Returned prefix will be used for all form components. The prefix can also be overridden on * form component level by overriding {@link FormComponent#getValidatorKeyPrefix()} * * @return prefix prepended to validator keys */ public String getValidatorKeyPrefix() { return null; } /** * Gets whether the current form has any error registered. * * @return True if this form has at least one error. */ public final boolean hasError() { // if this form itself has an error message if (hasErrorMessage()) { return true; } // the form doesn't have any errors, now check any nested form // components return anyFormComponentError(); } /** * Returns whether the form is a root form, which means that there's no other form in it's * parent hierarchy. * * @return true if form is a root form, false otherwise */ public boolean isRootForm() { return findParent(Form.class) == null; } /** * Checks if this form has been submitted during the current request * * @return true if the form has been submitted during this request, false otherwise */ public final boolean isSubmitted() { return getFlag(FLAG_SUBMITTED); } /** * Method made final because we want to ensure users call setVersioned. * * @see org.apache.wicket.Component#isVersioned() */ public boolean isVersioned() { return super.isVersioned(); } /** * THIS METHOD IS NOT PART OF THE WICKET PUBLIC API. DO NOT CALL IT. * <p> * Retrieves FormComponent values related to the page using the persister and assign the values * to the FormComponent. Thus initializing them. */ public final void loadPersistentFormComponentValues() { visitFormComponentsPostOrder(new FormComponent.AbstractVisitor() { public void onFormComponent(final FormComponent formComponent) { // Component must implement persister interface and // persistence for that component must be enabled. // Else ignore the persisted value. It'll be deleted // once the user submits the Form containing that FormComponent. // Note: if that is true, values may remain persisted longer // than really necessary if (formComponent.isVisibleInHierarchy() && formComponent.isPersistent()) { // The persister final IValuePersister persister = getValuePersister(); // Retrieve persisted value persister.load(formComponent); } } }); } /** * THIS METHOD IS NOT PART OF THE WICKET API. DO NOT ATTEMPT TO OVERRIDE OR CALL IT. * * Handles form submissions. * * @see Form#validate() */ public final void onFormSubmitted() { markFormsSubmitted(); if (handleMultiPart()) { // Tells FormComponents that a new user input has come inputChanged(); String url = getRequest().getParameter(getHiddenFieldId()); if (!Strings.isEmpty(url)) { dispatchEvent(getPage(), url); } else { // First, see if the processing was triggered by a Wicket IFormSubmittingComponent final IFormSubmittingComponent submittingComponent = findSubmittingButton(); // When processing was triggered by a Wicket IFormSubmittingComponent and that // component indicates it wants to be called immediately // (without processing), call IFormSubmittingComponent.onSubmit() right away. if (submittingComponent != null && !submittingComponent.getDefaultFormProcessing()) { submittingComponent.onSubmit(); } else { // this is the root form Form formToProcess = this; // find out whether it was a nested form that was submitted if (submittingComponent != null) { formToProcess = submittingComponent.getForm(); } // process the form for this request if (formToProcess.process()) { // let clients handle further processing delegateSubmit(submittingComponent); } } } } // If multi part did fail check if an error is registered and call // onError else if (hasError()) { callOnError(); } } /** * Process the form. Though you can override this method to provide your whole own algorithm, it * is not recommended to do so. * <p> * See the class documentation for further details on the form processing * </p> * * @return False if the form had an error */ public boolean process() { if (!isEnabled() || !isEnableAllowed() || !isVisibleInHierarchy()) { // since process() can be called outside of the default form workflow, an additional // check is needed return false; } // run validation validate(); // If a validation error occurred if (hasError()) { // mark all children as invalid markFormComponentsInvalid(); // let subclass handle error callOnError(); // Form has an error return false; } else { // mark all children as valid markFormComponentsValid(); // before updating, call the interception method for clients beforeUpdateFormComponentModels(); // Update model using form data updateFormComponentModels(); // Persist FormComponents if requested persistFormComponentData(); // Form has no error return true; } } /** * Calls onError on this {@link Form} and any enabled and visible nested form, if the respective * {@link Form} actually has errors. */ private void callOnError() { onError(); // call onError on nested forms visitChildren(Form.class, new IVisitor() { public Object component(Component component) { final Form form = (Form)component; if (!form.isEnabled() || !form.isEnableAllowed() || !form.isVisibleInHierarchy()) { return IVisitor.CONTINUE_TRAVERSAL_BUT_DONT_GO_DEEPER; } if (form.hasError()) { form.onError(); } return IVisitor.CONTINUE_TRAVERSAL; } }); } /** * Sets FLAG_SUBMITTED to true on this form and every enabled nested form. */ private void markFormsSubmitted() { setFlag(FLAG_SUBMITTED, true); visitChildren(Form.class, new IVisitor() { public Object component(Component component) { Form form = (Form)component; if (form.isEnabled() && form.isEnableAllowed() && isVisibleInHierarchy()) { form.setFlag(FLAG_SUBMITTED, true); return IVisitor.CONTINUE_TRAVERSAL; } return IVisitor.CONTINUE_TRAVERSAL_BUT_DONT_GO_DEEPER; } }); } /** * Removes already persisted data for all FormComponent children and disable persistence for the * same components. * * @see Page#removePersistedFormData(Class, boolean) * * @param disablePersistence * if true, disable persistence for all FormComponents on that page. If false, it * will remain unchanged. */ public void removePersistentFormComponentValues(final boolean disablePersistence) { // The persistence manager responsible to persist and retrieve // FormComponent data final IValuePersister persister = getValuePersister(); // Search for FormComponents like TextField etc. visitFormComponentsPostOrder(new FormComponent.AbstractVisitor() { public void onFormComponent(final FormComponent formComponent) { if (formComponent.isVisibleInHierarchy()) { // remove the FormComponent's persisted data persister.clear(formComponent); // Disable persistence if requested. Leave unchanged // otherwise. if (formComponent.isPersistent() && disablePersistence) { formComponent.setPersistent(false); } } } }); } /** * Sets the default IFormSubmittingComponent. If set (not null), a hidden submit component will * be rendered right after the form tag, so that when users press enter in a textfield, this * submit component's action will be selected. If no default component is set (so unset by * calling this method with null), nothing additional is rendered. * <p> * WARNING: note that this is a best effort only. Unfortunately having a 'default' button in a * form is ill defined in the standards, and of course IE has it's own way of doing things. * </p> * There can be only one default button per form hierarchy. So if you set default button on a * nested form, it will actually delegate the call to root form. </b> * * @param submittingComponent * The component to set as the default submitting component, or null when you want to * 'unset' any previously set default component */ public final void setDefaultButton(IFormSubmittingComponent submittingComponent) { if (isRootForm()) { defaultSubmittingComponent = submittingComponent; } else { getRootForm().setDefaultButton(submittingComponent); } } /** * Sets the maximum size for uploads. If null, the setting * {@link IApplicationSettings#getDefaultMaximumUploadSize()} is used. * * @param maxSize * The maximum size */ public void setMaxSize(final Bytes maxSize) { this.maxSize = maxSize; } /** * Set to true to use enctype='multipart/form-data', and to process file uploads by default * multiPart = false * * @param multiPart * whether this form should behave as a multipart form
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -