View Helper Design Pattern – Core J2EE Patterns

View Helper Design Pattern separates the static view such as JSPs from the processing of the business model data. The View Helper pattern is used in the presentation layer by adapting the model data and the View components. View Helper can format the model data according to the your business requirement, but it can not generate model data for the business.

View Helper Design Pattern

In order to adapt model data into the presentation layer of the application, the view helper pattern is used, which specifies the use of helpers for such implementation. The presentation layer, generally, have a number of JavaServer (JSP) Pages. In order to present content to the user, these pages contain HTML and images. The problem takes roots when the JSP pages are required to display dynamic content which is stored within the model. The embedding of Java code needs to be avoided in order to display model data by these pages. Instead if that, helpers must be employed to resolve the issue. There are certain choices that the developer has, they can add JDP scriplets by embedding Java code, they can use EL or they can add helpers in order to extract the data for them. It makes more sense to employ a number of helpers in order to adapt the model to the presentation that it does if the java code is cluttered with the presentation code. This helps in keeping the presentation code and the business logic separated.

Spring 5 Design Pattern Book

You could purchase my Spring 5 book that is with title name “Spring 5 Design Patterns“. This book is available on the Amazon and Packt publisher website. Learn various design patterns and best practices in Spring 5 and use them to solve common design problems. You could use author discount to purchase this book by using code- “AUTHDIS40“.
Spring-5-Design-Pattern

Class Diagram for View Helper

Let’s see the following class diagram for the view helper pattern.

View Helper Design Pattern
A View delegates its processing responsibilities to its helper classes, implemented as JavaBean, custom tags, or tag files. Helpers serve as adapters between the view and the model, and perform processing related to formatting logic, such as generating an HTML table.

The view helpers make it a lot easier for the page developers to develop the presentation layer when they replace it by the helper, which are also simpler to use. The developers, however, need to publish a catalog of the helpers along with the description as to how to use them, in order to accomplish this. The problem occurs when the application designer is not ready to give them page developers a model for the helpers, with which they are supposed to work, but the page designer has already developed the respective pages. In order to resolve this problem, the developers can code a set of dummy data into the helper, when there is no model present. An alternative method, to address this problem can be to provide a dummy model with which to work. Whichever of the methods is chosen, the age developer will not have to wait for the developers. The view helper caters certain issues and improves certain features of the application. It gives the developers, improved application portioning, improved re-usability and maintainability. It also improves the role separation, like in between the presentation code and business logic.

It eases the testing process for the page developers. The use of helpers mirrors the scriplets, it completely functions like one and can be put to advantage like one too.

Example for View Helper Pattern

As we know that the View is the static and formatted component of the MVC pattern, but some we need some business processing the presentation layer. If you are using JSPs then you could use scriptlet for the business processing at the view layer but using scriptlet is not best practice because it promotes the tight coupling between the View and Business Logic. But some View Helper classes based on the View Helper pattern take that responsibilities of the Business Processing at the Presentation layer. Some of the technologies based on the View Helper pattern are below:

  • JavaBeans View Helper
  • Tag LibraryView Helper
  • Using JSTL Tags
  • Using Spring Tags
  • Using Third-Party Tag Library
Previous
Next