Sophisticated Web pages present content from numerous data sources, using multiple subviews that comprise a single display page. Additionally, a variety of individuals with different skill sets contribute to the development and maintenance of these Web pages.
Instead of providing a mechanism to combine modular, atomic portions of a view into a composite whole, pages are built by embedding formatting code directly within each view.
Modification to the layout of multiple views is difficult and error prone, due to the duplication of code.
Use composite views that are composed of multiple atomic subviews. Each component of the template may be included dynamically into the whole and the layout of the page may be managed independently of the content.
This solution provides for the creation of a composite view based on the inclusion and substitution of modular dynamic and static template fragments. It promotes the reuse of atomic portions of the view by encouraging modular design. It is appropriate to use a composite view to generate pages containing display components that may be combined in a variety of ways. This scenario occurs, for example, with portal sites that include numerous independent subviews, such as news feeds, weather information, and stock quotes on a single page. The layout of the page is managed and modified independent of the subview content.
Another benefit of this pattern is that Web designers can prototype the layout of a site, plugging static content into each of the template regions. As site development progresses, the actual content is substituted for these placeholders.
Figure shows a screen capture of Sun's Java homepage, java.sun.com. Four regions are identified: Navigation, Search, Feature Story, and Headlines. While the content for each of these component subviews may originate from different data sources,they are laid out seamlessly to create a single composite page.
Screen shot of a modular page, including Search, Navigation, Feature Story, and Headlines regions
This pattern is not without its drawbacks. There is a runtime overhead associated with it, a tradeoff for the increased flexibility that it provides. Also, the use of a more sophisticated layout mechanism brings with it some manageability and development issues, since there are more artifacts to maintain and a level of implementation indirection to understand.
Figure shows the class diagram that represents the Composite View pattern.
Composite View class diagram
Participants and Responsibilities
Figure shows the sequence diagram for the Composite View pattern.
Composite View sequence diagram
A composite view is a view that is an aggregate of multiple subviews.
The View Manager manages the inclusion of portions of template fragments into the composite view. The View Manager may be part of a standard JSP runtime engine, in the form of the standard JSP include tag (<jsp:include>), or it may be encapsulated in a JavaBean helper(JSP 1.0+) or custom tag helper (JSP 1.1+) to provide more robust functionality.
A benefit of using a mechanism other than the standard include tag is that conditional inclusion is easily done. For example, certain template fragments may be included only if the user fulfills a particular role or certain system conditions are satisfied. Furthermore, using a helper component as a View Manager allows for more sophisticated control of the page structure as a whole, which is useful for creating reusable page layouts.
An included view is a subview that is one atomic piece of a larger whole view. This included view could also potentially be a composite, itself including multiple subviews.
StrategiesJSP View Strategy
See “JSP View Strategy”.
Servlet View Strategy
See “Servlet View Strategy”.
JavaBean View Management Strategy
View management is implemented using JavaBeans,as shown in Example. The view delegates to the JavaBean, which implements the custom logic to control view layout and composition. The decisions on page layout may be based on user roles or security policies, making it much more powerful than the standard JSP include functionality. While it is semantically equivalent to the Custom Tag View Management Strategy, it is not nearly as elegant, since it introduces scriptlet code into the view.
Using the JavaBean View Management Strategy requires less up-front work than using the preferred Custom Tag View Management Strategy, since it is easier to construct JavaBeans and integrate them into a JSP environment. Additionally, even novice developers understand JavaBeans. This strategy is also easier from a manageability standpoint, because the completed JavaBeans are the only resulting artifacts to manage and configure.
Example JavaBean View Management Strategy<%@page
Standard Tag View Management Strategy
View management is implemented using standard JSP tags, such as the <jsp:include> tag. Using standard tags for managing the layout and composition of views is an easy strategy to implement, but does not provide the power and flexibility of the preferred Custom Tag View Management Strategy, since the layout for individual pages remains embedded within that page. Thus, while this strategy allows for the underlying content to vary dynamically, any site-wide layout changes would require individual modifications to numerous JSPs. This is shown in Example.
Example Standard Tag View Management Strategy<html>
When creating a composite display using standard tags, both static content, such as an HTML file, and dynamic content, such as a JSP, can be included. Additionally, the content can be included at translation time or at runtime. If the content is included at translation time, then the page display will remain unchanged until the JSP is recompiled, at which point any modifications to included content will be visible. In other words, the page is laid out and generated once, each time the JSP is recompiled. Example shows an excerpt of a JSP that generates a composite page in this way, using the standard JSP include directive <%@ include %>, which includes content at translation time.
Runtime inclusion of content means that changes to underlying subviews are visible in the composite page the next time a client accesses the page. This is much more dynamic and can be accomplished using the standard JSP include tag <jsp:include>, as shown in Example. There is of course some runtime overhead associated with this type of view generation, but it is the tradeoff for the increased flexibility of on-the-fly content modifications.
Example Composite View with Translation--Time Content Inclusion<table border=1 valign="top" cellpadding="2%"
Example Composite View with Runtime Content Inclusion<table border=1 valign="top" cellpadding="2%"
View management is implemented using custom tags(JSP 1.1+), which is the preferred strategy. Logic implemented within the tag controls view layout and composition. These tags are much more powerful and flexible than the standard JSP include tag, but also require a higher level of effort. Custom actions can base page layout and composition on such things as user roles or security policies.
Using this strategy requires more upfront work than do the other view management strategies, since custom tag development is more complicated than simply using JavaBeans or standard tags. Not only is there more complexity in the development process, but there is much more complexity with respect to integrating and managing the completed tags. Using this strategy requires the generation of numerous artifacts, including the tag itself, a tag library descriptor, configuration files, and configuring the environment with these artifacts.
The following JSP excerpt shows a possible implementation of this strategy and is excerpted from Example. Please refer to that code sample for more detail.<region:render
Transformer View Management Strategy
View management is implemented using an XSL Transformer. This strategy would typically be combined with the Custom Tag View Management Strategy, using custom tags to implement and delegate to the appropriate components. Using this strategy can help to enforce the separation of the model from the view, since much of the view markup must be factored into a separate stylesheet. At the same time, it involves technologies that require new and sophisticated skill sets to implement correctly, an issue that makes this strategy impractical in many environments where these technologies are not already established.
The following excerpt shows the use of a custom tag from within a JSP to convert a model using a stylesheet and transformer:<xsl:transform model="portfolioHelper"
Early-Binding Resource Strategy
This is another name for translation-time content inclusion, as described in the Standard Tag View Management Strategy and shown in Example. It is appropriate for maintaining and updating a relatively static template and is recommended if a view includes headers and footers that change infrequently.
Late-Binding Resource Strategy
This is another name for runtime-content inclusion, as described in the Standard Tag View Management Strategy and shown in Example. It is appropriate for composite pages that may change frequently. One note: If the subview included at runtime is a dynamic resource, such as a JSP, then this subview may also be a composite view, including more runtime content. The flexibility offered by such nested composite structures should be weighed against their runtime overhead and considered in light of specific project requirements.
The Composite View pattern can be implemented using any number of strategies, but one of the more popular is the Custom Tag View Management Strategy. In fact, there are a number of custom tag libraries currently available for implementing composite views that separate view layout from view content and provide for modular and pluggable template subviews.
The template library describes three basic components: sections, regions, and templates.
A region can be defined and rendered as shown in Example.
Example A Region and Sections<region:render template='portal.jsp'>
A region defines its content by matching logical section names with a portion of content, such as banner.jsp. The layout for the region and its sections is defined by a template, to which each region is associated. In this case, the template is named portal.jsp, as defined in Example.
Example Template Definition<region:render section='banner'/>
A site with numerous views and a single consistent layout has one JSP containing code that looks similar to the template definition in Example, and many JSPs that look similar to Example, defining alternate regions and sections.
Sections are JSP fragments that are used as subviews to build a composite whole as defined by a template. The banner.jsp section is shown in Example.
Example Section Subview-banner.jsp<table width="100%" bgcolor="#C0C0C0">
Composite views are a modular, flexible and extensible way to build JSP views for your J2EE application.
The Composite View pattern may be used as the view in the View Helper pattern.
The Composite View pattern is based on the Composite pattern, which describes part-whole hierarchies where a composite object is comprised of numerous pieces, all of which are treated as logically equivalent.
J2EE Related Interview Questions
|Java Script Interview Questions||Adv Java Interview Questions|
|J2SE Interview Questions||Core Java Interview Questions|
|Java Struts Interview Questions||Hibernate Interview Questions|
|JavaServer Faces (JSF) Interview Questions||Java 8 Interview Questions|
|JavaFX Interview Questions||NHibernate Interview Questions|
|Spring MVC Framework Interview Questions||The Java Debugger (JDB) Interview Questions|
J2ee Platform Overview
Presentation Tier Design Considerations And Bad Practices
Business Tier Design Considerations And Bad Practices
J2ee Patterns Overview
Presentation Tier Patterns
Business Tier Patterns
Integration Tier Patterns
Epilogue J2ee Patterns Applied
All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.