Tips and tricks for writing JSF custom composite components

Source: Internet
Author: User

This article describes new ideas for developing JavaServer Faces (JSF) custom composite components, and provides tips on how to quickly develop reusable JSF components rather than implementing renderer (renderer), state management, and event listeners as you would in traditional ways. The principles and techniques provided in this article are also useful for general JSF development.

Introduced

JavaServer Faces (JSF) provides an extensible component model that enables developers to create reusable components that use these custom components to improve development efficiency and reduce development costs. Although the JSF component model is very powerful for customization and reuse, developers generally believe that it is not easy to develop JSF custom components, since at least you need to familiarize yourself with the internal mechanisms of JSF Encode/decode and state holder and override the appropriate methods, such as Encodebe Gine (), Decode (), SaveState (), and Restorestate (), for developing complex custom components, even require an in-depth understanding of more interfaces, such as NamingContainer, Stateholder, Interfaces such as Editablevalueholder and Actionsource.

However, the ability to reuse JSF standard components can greatly simplify the development of custom components, especially for custom composite components. In most cases, we can reuse the standard renderer, state management, event listeners, converters, and validators that the JSF framework has already provided. There are few articles or books on how to reuse these standard features, and this paper proposes the principles and techniques for rapidly developing JSF custom composite components based on a reusable strategy.

This article first summarizes the general principles of JSF component development, and then explains what standard features can be reused and how to reuse it through an example (Value scroller Custom composite component) to simplify the development of JSF custom composite components.

Principles and Techniques

There are two main principles for developing JSF custom composite components, emphasizing the reuse of existing standard components, and how to make sure that custom components are easy to reuse.

1, as far as possible reuse the function of standard components and implementation

Traditional custom composite Component development recommends full coverage to implement Encode/decode logic, but this is time-consuming and error-prone. There is no doubt that we can reduce or not even write this part of the code by reusing a standard component's renderer. In addition, to enable flexible configuration and use, custom composite components often need to provide many properties, and we need to write a lot of code to handle the read and write and state management of these attributes. In fact, we can simply pass the properties of a custom composite component to the standard components it contains, which are handled by existing standard code without having to write the code repeatedly.

2, clear separation of component classes, tag classes and model classes

The JSF component model suggests a clear and clear allocation of responsibilities between component classes, tag classes, and model classes for reuse and extension. Component classes should not rely on javax.faces.component.html packages, because component classes can be used not only for HTML, but also for other markup languages such as WML. That is, the component class should not refer directly to the HTML component within the Javax.faces.component.html package. For example, it is undesirable to create an Htmlcommandbutton instance in your component class, and you should consider using the Uicommand in the Javax.faces.component package. On the other hand, if you want your model classes to be reusable for different Web frames, Then your model class should not rely on any of the JSF packages, that is, the model class represents only the business object and does not contain any user interface-related components, data, and state.

Based on these principles, comparing the traditional approach and the techniques presented in this article, we can see that development strategies based on reuse greatly simplify the authoring of JSF custom composite components. Developing a JSF custom component typically requires the following 3 steps.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.