Search code examples
javaliferay

How to distinguish pages in Liferay?


How can you tell the difference between two pages in the Liferay App?

So, assume my application has 2 pages and I want to be able to tell which page is first and which is second. What data would I use to accomplish this?

I tried by using the PLID numbers, and this works nicely on a single environment.

But, if I deploy my Liferay app to another environment PLID numbers differ. So, differenting the Liferay pages by their PLID numbers is fine for single environment app, but if my app is to be deployed on multiple environments then this is no longer a valid solution.

So, what is environment-independent way of determing which page is which in Liferay?


Solution

  • While rendering, you can get the current context from a themeDisplay object, retrievable like this:

    ThemeDisplay themeDisplay = (ThemeDisplay) renderRequest.getAttribute(WebKeys.THEME_DISPLAY);
    

    There are plenty of options - but as inherently there is no defined "order" between pages: If you want to use the identical portlet on both pages, you have the option to

    • configure the page, e.g. by adding a custom field to pages and set it to a magically known value on one page. You'll get the currently rendered page from the themeDisplay object
    • configure the portlet on each page, to make each instance aware of its purpose (e.g. read about the portlet's "edit" or "config" mode and implement a UI for its configuration - see PortletPreferences)