|
Web applications work over the web and have various resources, such as images or downloadable files, that the web browser has to get from the server. These resources are typically used in A web server can handle many of such requests for static resources without having to ask them from the application, or the Vaadin provides also low-level facilities for retrieving the URI and other parameters of a HTTP request. We will first look into how applications can provide various kinds of resources and then look into low-level interfaces for handling URIs and parameters to provide resources and functionalities. Notice that using URI or parameter handlers to create "pages" is not meaningful in Vaadin or in AJAX applications generally. Please see Section 11.1, “Special Characteristics of AJAX Applications” for a detailed explanation. Vaadin has two interfaces for resources: a generic
File resources are files stored anywhere in the file system. The use of file resources generally falls into two main categories: downloadable files and embedded images. A file object that can be accessed as a file resource is defined with the standard The mainwindow.addComponent(new Embedded ("",
new ClassResource("smiley.jpg",
mainwindow.getApplication()))); Theme resources are files included in a theme, typically images. See Chapter 8, Themes for more information on themes. Stream resources are application resources that allow creating dynamic resource content. Charts are typical examples of dynamic images. To define a stream resource, you need to implement the The following example demonstrates the creation of a simple image in PNG image format. import java.awt.image.*;
public class MyImageSource
implements StreamResource.StreamSource {
ByteArrayOutputStream imagebuffer = null;
int reloads = 0;
/* We need to implement this method that returns
* the resource as a stream. */
public InputStream getStream () {
/* Create an image and draw something on it. */
BufferedImage image = new BufferedImage (200, 200,
BufferedImage.TYPE_INT_RGB);
Graphics drawable = image.getGraphics();
drawable.setColor(Color.lightGray);
drawable.fillRect(0,0,200,200);
drawable.setColor(Color.yellow);
drawable.fillOval(25,25,150,150);
drawable.setColor(Color.blue);
drawable.drawRect(0,0,199,199);
drawable.setColor(Color.black);
drawable.drawString("Reloads="+reloads, 75, 100);
reloads++;
try {
/* Write the image to a buffer. */
imagebuffer = new ByteArrayOutputStream();
ImageIO.write(image, "png", imagebuffer);
/* Return a stream from the buffer. */
return new ByteArrayInputStream(
imagebuffer.toByteArray());
} catch (IOException e) {
return null;
}
}
} The content of the generated image is dynamic, as it updates the reloads counter with every call. The You can use resources in various ways. Some user interface components, such as Below we display the image with the // Create an instance of our stream source.
StreamResource.StreamSource imagesource = new MyImageSource ();
// Create a resource that uses the stream source and give it a name.
// The constructor will automatically register the resource in
// the application.
StreamResource imageresource =
new StreamResource(imagesource, "myimage.png", this);
// Create an embedded component that gets its contents
// from the resource.
main.addComponent(new Embedded("Image title", imageresource)); The image will look as follows: We named the resource as Another solution for creating dynamic content is an URI handler, possibly together with a parameter handler. See Section 11.5.1, “URI Handlers” and Section 11.5.2, “Parameter Handlers”. |
Table of Contents
|