Managing BLOB Fields using DbForms is a trivial task. First, you have to tell DbForms about BLOB-Fields in the xml-configuration file:
NOTE: after version 2.2, interceptor based BLOB handling was introduced. The interceptor
org.dbforms.event.BlobInterceptor is used to store the filename of an uploaded file in a
specified column. In previous versions, the BLOB was stored as a serialized Java object in the
database along with the file meta data. However, the disadvantage of this solution was:
the lack of long term storage (i.e. a change in the wrapper class would lead to ClassCastExceptions)
the BLOB is not readable by other (non-DbForms) applications because those apps can't de-serialize the java object
To switch back to the old way of handling BLOBs, you can set the optional attribute
"classic" in the table definition in your
More documentation on this issue will follow; until then this section is a bit outdated. meanwhile,
please refer to the API doc for class
org.dbforms.event.BlobInterceptor to see how to configure the
BLOB interceptor the new way. The following example is for the "classic" way.
<dbforms-config> <table name="pets" blobHandling="classic"> <field name="pet_id" fieldType="int" isKey="true" autoInc="true" /> <field name="name" fieldType ="char" /> <field name="portrait_pic" fieldType ="blob" /> <field name="story" fieldType ="blob" /> </table> </dbforms-config>
The configuration-code-snippet shown above tells DbForms that the fields portrait_pic and story are of type BLOB. As you can see, DbForms allows more than one field defined in a table to be of type BLOB.
After defining our BLOB-powered table, we want to manage the BLOB fields. Of course data-input elements like textField or select cannot be applied to BLOBs. For this purpose a new Data-Element is introduced:
fieldName refers to the name of the field, the file needs to be uploaded into. There are additional attributes available for this element (accept, maxLength, etc.), please see chapter DbForms Custom Tag Library for additional information)
This Custom Tag gets rendered as an HTML <file> - tag, as shown next:
This HTML-element enables multipart-enabled browsers to submit files to the server. Be aware that not all browsers support this functionality.
Important: the multipart-attribute of the <dbforms> element must be set to
true to support multipart requests!
It is possible to save the MIME type and file size of a BLOB. This is done by providing column names for the
BlobInterceptor to use. The example shown below shows how this is done.
<interceptor className="org.dbforms.event.BlobInterceptor"> <param name="blob-column" value="BODY"/> <param name="name-column" value="FILE_NAME"/> <param name="mime-column" value="MIME_TYPE"/> <param name="size-column" value="BODY_SIZE"/> </interceptor>
The above code would be added to a
table element in
Regarding update and delete operations, BLOB fields may be treated similar to common (alphanumerical) fields:
if a user hits the DELETE-button, the BLOB fields of the selected row will be deleted. (this is done automatically by the RDBMS)
If a user hits the UPDATE and no new file is uploaded, the existing BLOB will NOT be modified. If a user has chosen a new file, (using the Browse- Button or by entering a valid file-path into the text-field left to the button) any existing BLOB at the selected row will be overwritten with the new value.
Managing BLOBs as described above may be sufficient in most cases. But sometimes, we may want to reverse the process: we may want to retrieve BLOBs. (for instance, to provide users the ability of visualizing uploaded images, etc.)
DbForms provides a simple way of retrieving BLOBs from the database. You are, however not required to use it. Again, using dbForms is meant to help you increase your productivity, if another tool or techniques is better for a given task include it!
For retrieving a BLOB using DbForms BLOB-retrieving facility, you need to use the following tag:
This tag renders an URL pointing to a servlet built into DbForms. This servlet retrieves the specified field from the database, tries to figure out the MIME type of the file, assigns it to the response and finally returns a stream of bytes containing the data of the BLOB.
To retrieve and render an image, the following JSP-code would need to be specified:
<img src=<db:blobURL fieldName="portrait_pic"/> width=100 height=80 border=0>
To establish a link to a BLOB (for example to a word or pdf document) you could write:
<a href=<db:blobURL fieldName="portrait_pic"/>> Click to download</a>
Dependent of the installed plug-ins and other browser-and system properties, either the MIME-matching plug-in (or helper application) will be opened, a save-as dialog will pop up or a download manager like Gozilla! will be started.
It is important to mention that the <blobURL> element is valid only if embedded in a <body> - element. The <file> tag works in <header>, <body> and <footer> elements. Within the <body> element, both - inserts and updates - are possible, within the other tags, only inserts are possible.