Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
book-of-vaadin.pdf
Скачиваний:
88
Добавлен:
24.03.2015
Размер:
13.43 Mб
Скачать

Writing a Server-Side Web Application

//Create an image component that gets its contents

//from the resource.

layout.addComponent(new Image("Image title", resource));

The resulting image is shown in Figure 4.8, “A Stream Resource”.

Figure 4.8. A Stream Resource

Another way to create dynamic content is a request handler, described in Section 11.4, “Request Handlers”.

4.5. Handling Errors

4.5.1. Error Indicator and message

All components have a built-in error indicator that is turned on if validating the component fails, and can be set explicitly with setComponentError(). Usually, the error indicator is placed right of the component caption. The error indicator is part of the component caption, so its placement is usually managed by the layout in which the component is contained, but some components handle it themselves. Hovering the mouse pointer over the field displays the error message.

textfield.setComponentError(new UserError("Bad value")); button.setComponentError(new UserError("Bad click"));

The result is shown in Figure 4.9, “Error Indicator Active”.

Figure 4.9. Error Indicator Active

4.5.2. Customizing System Messages

System messages are notifications that indicate a major invalid state in an application that usually requires restarting the application. Session timeout is perhaps the most typical such state.

System messages are strings managed in the SystemMessages class.

74

Handling Errors

Writing a Server-Side Web Application

sessionExpired

Application servlet session expired. A session expires if no server requests are made during the session timeout period. The session timeout can be configured with the session-timeout parameter in web.xml, as described in Section 4.8.3, “Deployment Descriptor web.xml”.

communicationErrorURL

An unspecified communication problem between the Vaadin Client-Side Engine and the application server. The server may be unavailable or there is some other problem.

authenticationError

This error occurs if 401 (Unauthorized) response to a request is received from the server.

internalError

A serious internal problem, possibly indicating a bug in Vaadin Client-Side Engine or in some custom client-side code.

outOfSync

The client-side state is invalid with respect to server-side state.

cookiesDisabled

Informs the user that cookies are disabled in the browser and the application does not work without them.

Each message has four properties: a short caption, the actual message, a URL to which to redirect after displaying the message, and property indicating whether the notification is enabled.

Additional details may be written (in English) to the debug console window described in Section 11.3, “Debug and Production Mode”.

You can override the default system messages by setting the SystemMessagesProvider in the VaadinService.You need to implement the getSystemMessages() method, which should return a SystemMessages object. The easiest way to customize the messages is to use a

CustomizedSystemMessages object as follows:

VaadinService.getCurrent().setSystemMessagesProvider( new SystemMessagesProvider() {

@Override

public SystemMessages getSystemMessages( SystemMessagesInfo systemMessagesInfo) { CustomizedSystemMessages messages =

new CustomizedSystemMessages(); messages.setCommunicationErrorCaption("Comm Err"); messages.setCommunicationErrorMessage("This is bad."); messages.setCommunicationErrorNotificationEnabled(true); messages.setCommunicationErrorURL("http://vaadin.com/"); return messages;

}

});

4.5.3. Handling Uncaught Exceptions

Handling events can result in exceptions either in the application logic or in the framework itself, but some of them may not be caught properly by the application. Any such exceptions are eventually caught by the framework. It delegates the exceptions to the DefaultErrorHandler, which displays the error as a component error, that is, with a small red "!" -sign (depending on the theme). If the user hovers the mouse pointer over it, the entire backtrace of the exception is

Handling Uncaught Exceptions

75

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]