hybris JSP secrets: Global JSP variables reference


Introduction

The JSP capabilities are poorly documented in hybris Wiki. It is anticipated that the frontend and backend developers work closely, and frontend developers commonly expect that all necessary data are provided by page and component controllers. However, some data has already available in JSP and the frontend developers can leverage it. Most of the variables listed below are hybris-specific and not officially documented. However, you can find some examples of the usage in hybris OOTB templates. I decided to put all this stuff in one place to use it as a reference for future. See the last section of this article for technical details.

Reference

  • ${cmsPage.name}. CMS Page name (String). Example: “Homepage”
  • ${cmsSite.uid} CMS Site Id (String). Example: “electronics”
  • ${request.secure} https? (Boolean) Example: “true”
  • ${header[‘host‘]} Current host+port (String) Example: “electronics.local:9002”
  • ${request.contextPath} Context path without encoded attributes (String) Example: ‘/trainingstorefront’
  • ${encodingAttributes} Encoding attributes (String) Example: ‘/electronics/en’
  • ${encodedContextPath} Context path (String) Example: ‘/trainingstorefront/electronics/en’
  • <c:url value=”/”/> Site root path (String) Example: ‘/trainingstorefront/electronics/en/’
  • ${siteRootUrl} Site root URL (String) Example: ‘/trainingstorefront/_ui/responsive’
  • ${siteResourcePath} Site resource path (String) Example: ‘/trainingstorefront/_ui/responsive/site-electronics’
  • ${themeResourcePath} Theme resource path (String) Example: ‘/trainingstorefront/_ui/responsive/theme-blue’
  • ${commonResourcePath} Common resource path (String) Example: ‘/trainingstorefront/_ui/responsive/common’
  • ${loginUrl} Login URL (String) Example: ‘/trainingstorefront/electronics/en/login’
  • ${jalosession.attributes[‘user’].anonymousCustomer} Is anonymous customer? (String (Boolean)). Use with caution! Example: ‘TRUE’
  • ${jalosession.attributes[‘user’].uid} Current authenticated User ID (or anonymous, or empty, or null) (String). Use with caution! Example: ‘anonymous’
  • ${jalosession.attributes[‘user’].displayName} User name (first name and last name) (String). Use with caution! Jalo attribute. Example: ‘John Smith’
  • ${jalosession.attributes[‘user’].carts} Cart. The cart entries are also available. (List). Use with caution! Jalo attribute.
  • ${authenticationStatusUrl} Authentication status URL. Response status: 401: unauthenticated . 200: authenticated (String) Example: ‘/trainingstorefront/electronics/en/authentication/status’
  • ${CSRFToken} CSRF Token (String) Example: ‘6e8c4bf2-ef39-4d81-b439-ac25ef4e3e5’
  • ${uiExperienceLevel} UI Experience Level (String) Example: ‘Desktop’
  • ${uiExperienceOverride} Is detected UI level overrided? (String (Boolean)) Example: ‘FALSE’
  • ${detectedUiExperienceCode} Detected UI Experience Code (String) Example: ‘Desktop’
  • ${overrideUiExperienceCode} New UI Experience code from the configuration (String) Example: ‘Desktop’
  • ${detectedDevice} Detected Device (object) (DeviceData) Example: ”
  • ${detectedDeviceUserAgent} Detected Device User Agent (String) Example: ‘Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.101 Safari/537.36’
  • ${language} Current language ISO Code (String) Example: ‘En’
  • ${themeName} Theme code (String) Example: ‘Blue’
  • ${addOnCommonCssPaths} Paths to the CSS of the addon (list) (String) Example: ‘/trainingstorefront/_ui/addons/liveeditaddon/ responsive/common/css/liveeditaddon.css’
  • ${addOnThemeCssPaths} Paths to the theme-related CSS of the addon (list) (String) Example: ‘
  • ${addOnJavaScriptPaths} Paths to javascripts of the addon (list) (String)
  • ${cmsSite} CMS Site Object (CMSSite)
  • ${cmsSite.startPageLabel} Start page Label (just an example of use cmsSite object) (String) Example: ‘homepage’
  • ${cmsSite.startingPage.code} Name of the starting page of the current cms site (just an example of use of cms site object) (String) Example: ‘Homepage’
  • ${currencies} All available currencies (List)
  • ${currentCurrency} Current currency (object) (CurrencyData) Example: 
  • ${currentCurrency.isocode} Iso code of the current currency (String) Example: ‘USD’
  • ${cmsPageRequestContextData.page } Current CMS Page (Model) (CMSPage)
  • ${cmsPageRequestContextData.page.uid} Current CMS Page UID (just as an example of use of cms page object) (String)
  • ${languages} All available languages (List)
  • ${metaRobots} Example: ‘noindex,follow’
  • ${user} Current User (CustomerData)
  • ${user.name} Current User Name (String)
  • ${pageContext} (JSPContextWrapper)
  • ${pageContext.request.remoteAddr} Visitor’s remote address (String) Example: ‘127.0.0.1’

What variables come from the controller?

In order to answer this question you need to iterate through a pageRequest object:
<c:set var='scope' value='${requestScope}'/>
<c:forEach items='${scope}' var='p'>
         <ul>
            <%-- Display the key of the current item, which
                 represents the model atteribute name --%>
            <li>Model Attribute Name: <c:out value='${p.key}'/></li>

            <%-- Display the value of the current item, which
                 represents the model attribute value --%>
            <li>Model Attribute Value: <c:out value='${p.value}'/></li>
         </ul>
</c:forEach>
Totally there are four scopes, and you can see the variables from the any of them using the code above (change the value of the c:set tag ):
  • pageScope,
  • requestScope,
  • sessionScope,
  • applicationScope.
The value of the model attribute is a string representation of the object. If the object is not capable of converting itself to String, you need to request one of the getters manually. For example, cms-jsp-output.png How to understand what attributes are available for the object? The simplest way is to put something that definitely leads to the error, try to reload the page and look at the logs for the class name. Then open the class with this name and look over the getter methods. Let’s try to put the following code into the template (I replaced “uid” to “something”):
${cmsPageRequestContextData.page.something}
2016-09-08_14h28_41.png This piece of the log shows that the property “something” is not found on the type. All we need is a class name, “de.hybris.platform.cms2.model.pages.ContentPageModel. This class and its predecessors have the following getter methods: ContentPageModel extends AbstractPageModel: public List<BannerComponentModel> getBannerComponents() public String getDescription() public List<FlashComponentModel> getFlashComponents() public String getKeywords() public String getLabel() public String getLabelOrId() public List<CMSLinkComponentModel> getLinkComponents() public List<CMSNavigationNodeModel> getNavigationNodes() AbstractPageModel extends CMSItemModel public CmsApprovalStatus getApprovalStatus() public String getAvailableContentSlots() public List<ContentSlotForPageModel> getContentSlots() public Boolean getDefaultPage() public UserModel getLockedBy() public PageTemplateModel getMasterTemplate() public String getMissingContentSlots() public List<CMSNavigationNodeModel> getNavigationNodeList() public MediaModel getPreviewImage() public List<AbstractRestrictionModel> getRestrictions() public String getTitle() public String getType() public String getTypeCode() public String getView() CMSItemModel extends ItemModel: … So as you see the following syntax is possible for this object in the JSP templates:
  • ${cmsPageRequestContextData.page.bannerComponents}
  • ${cmsPageRequestContextData.page.description}
  • ${cmsPageRequestContextData.page.title}
  • ${cmsPageRequestContextData.page.previewImage}
Every ${XXX} in JSP will be compiled to the following java code:
out.write(
  (java.lang.String)
     org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(
       "${XXX}", // expression
        java.lang.String.class, // expectedType
        (javax.servlet.jsp.PageContext) this.getJspContext(),
        null, //functionMap
        false //escape
     )
);

© Rauf Aliev, September 2016

Leave a Reply