Rachit
Rachit

Reputation: 403

When to go for getViewForPage in Hybris

I have seen methods in Hybris returning a JSP or dynamically doing getViewForPage.What is the difference between the two approaches.Is it the static or dynamic difference?What should be used when?Can I add new components with just returning a JSP using return statement?

Upvotes: 0

Views: 694

Answers (1)

Johannes von Zmuda
Johannes von Zmuda

Reputation: 1822

I would strongly recommend to use the getViewForPage method every time. There are a lot of reasons why. Here are just the 2 most important from my point of view:

  1. For every CMS Page Template you can define the "frontendTemplateName", which is the reference to the JSP view. The advantage here is that it can be maintained in the database. So if you decide to use a different JSP to display a CMS Page Template, all you need is to change the frontend template name and hybris uses a different JSP to render this template. This advantage is gone if you return just the JSP file. It also helps to avoid repetition of the JSP name in your code.
  2. Additionally consider restrictions to pages. hybris is able to display different pages depending on maintained restrictions. It is not possible to use restrictions on CMS Pages when using the JSP name in your code.

As long as you use the "storeCmsPageInModel" method, you can use CMS Components in your template. No matter if you use JSP or getViewForPage. However with the getViewForPage method you make sure, that the right JSP is used and that the configured frontend template name has the right content slots to display your components.

Maybe this question is not intended, but I would strongly recommend to use CMS Pages for every possible purpose. We developers have seen requirements change over time and CMS Pages offer all the flexibility you need to respond to this change.

Upvotes: 2

Related Questions