Class FileFieldSizeCheckBehavior
- All Implemented Interfaces:
Serializable,IComponentAwareEventSink,IRequestListener,IComponentAwareHeaderContributor,IClusterable
A Behavior that can be added to FileUploadField-s to provide a convenient and
fast size checking for users using FileApi supporting browsers. Using this has the benefit that
the user does not have to upload the whole file data to receive the error about it exceeding the
size limit. In browsers not supporting the FileApi this class does not change or break anything
(so it is safe for IE users).
Works with single or multiple enabled file inputs.
Longer explanation:
Today (2011 first half) browsers do not read the http response until they have fully written out
the http request. This means that if you post a form with files in it and wicket stops reading
the input because lets say the files are too big, the servlet container will still have to read
and throw away the rest of the request before it can send back the response to the client. This
means that a lot of user time, network bandwidth and server cpu time is wasted. Adding this
behavior cures this problem for those users with a FileApi supporting browsers. This class does
not replace the standard wicket size validation (this is only a convenience feature). It will
still be done at upload.
Usage is very simple (similar to an AjaxButton), just add it to the field and specify what should happen when validation passes or fails
final FileUploadField uploadField = new FileUploadField("file");
// add our FileApi based size check, errors are reported trough a
// feedback panel
uploadField.add(new FileFieldSizeCheckBehaviour() {
private static final long serialVersionUID = 7228537141239670625L;
@Override
protected void onSubmit(AjaxRequestTarget target, FileList fileList) {
target.add(feedback);
}
@Override
protected void onError(AjaxRequestTarget target, FileList fileList) {
target.add(feedback);
}
});
- Author:
- akiraly
- See Also:
-
Field Summary
Fields inherited from class org.wicketstuff.html5.fileapi.FileFieldChangeBehavior
DEFAULT_MAX_NUM_OF_FILES, JAVASCRIPT_REFFields inherited from class org.apache.wicket.ajax.AbstractDefaultAjaxBehavior
INDICATOR -
Constructor Summary
ConstructorsModifierConstructorDescriptionDefault constructor.FileFieldSizeCheckBehavior(int maxNumOfFiles) Constructor.protectedFileFieldSizeCheckBehavior(String event, int maxNumOfFiles) -
Method Summary
Modifier and TypeMethodDescriptionprotected voidaddErrorMsg(AjaxRequestTarget target, FileList fileList) Adds an error message about the exceeded file size limit.protected final FormComponent<?>Helper method to get the boundFormComponent.Retrieves the max size limit from the bound form component's parent form.protected voidonBind()protected abstract voidonError(AjaxRequestTarget target, FileList fileList) Called when size validation failed.protected voidonEvent(AjaxRequestTarget target, FileList fileList) Called during the ajax callback.protected abstract voidonSubmit(AjaxRequestTarget target, FileList fileList) Called when size validation passed.Methods inherited from class org.wicketstuff.html5.fileapi.FileFieldChangeBehavior
getMaxNumOfFiles, onEvent, renderHead, updateAjaxAttributesMethods inherited from class org.apache.wicket.ajax.AjaxEventBehavior
getEvent, onEvent, respondMethods inherited from class org.apache.wicket.ajax.AbstractDefaultAjaxBehavior
findIndicatorId, getAttributes, getCallbackFunction, getCallbackFunctionBody, getCallbackScript, getCallbackScript, onMethodMismatch, onRequest, postprocessConfiguration, renderAjaxAttributes, renderAjaxAttributesMethods inherited from class org.apache.wicket.behavior.AbstractAjaxBehavior
afterRender, bind, getCallbackUrl, getComponent, onComponentRendered, onComponentTag, onComponentTag, onUnbind, unbindMethods inherited from class org.apache.wicket.behavior.Behavior
beforeRender, canCallListener, detach, getStatelessHint, isEnabled, isTemporary, onAttribute, onConfigure, onEvent, onException, onRemove, onTagMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.apache.wicket.IRequestListener
rendersPage
-
Constructor Details
-
FileFieldSizeCheckBehavior
public FileFieldSizeCheckBehavior()Default constructor. Calls same constructor in parent. -
FileFieldSizeCheckBehavior
public FileFieldSizeCheckBehavior(int maxNumOfFiles) Constructor. Calls same constructor in parent.- Parameters:
maxNumOfFiles- maximum number of files that are processed from the ajax request, non-negative
-
FileFieldSizeCheckBehavior
-
-
Method Details
-
onEvent
Description copied from class:FileFieldChangeBehaviorCalled during the ajax callback.- Specified by:
onEventin classFileFieldChangeBehavior- Parameters:
target- wicket object representing the ajax response, not-nullfileList- list of files chosen in the upload field, not-null
-
getMaxSize
Retrieves the max size limit from the bound form component's parent form.- Returns:
- max size limit in bytes
-
onBind
protected void onBind()- Overrides:
onBindin classAbstractDefaultAjaxBehavior
-
getFormComponent
Helper method to get the boundFormComponent.- Returns:
- form component owning this behavior
-
addErrorMsg
Adds an error message about the exceeded file size limit. By default it mimics the upload error handling of Form. In o.a.w.markup.html.form.Form look for #handleMultiPart() and #onFileUploadException().- Parameters:
target- wicket object representing the ajax response, not-nullfileList- list of files chosen in the upload field, not-null
-
onSubmit
Called when size validation passed.- Parameters:
target- wicket object representing the ajax response, not-nullfileList- list of files chosen in the upload field, not-null
-
onError
Called when size validation failed.- Parameters:
target- wicket object representing the ajax response, not-nullfileList- list of files chosen in the upload field, not-null
-