Ajax
Ajax

Reputation: 148

AEM: How to dynamically get the current page path and pass it to the request parameter for drop down

options=/bin/services/myservlet.GET_DROPDOWN_VALUES.json?locale='some_locale'

if I put locale=en-us it works fine,

How can I dynamically get the current page locale and pass it to the request parameter ?

Upvotes: 0

Views: 9394

Answers (2)

SubSul
SubSul

Reputation: 2563

So, your servlet requires locale as one of the parameters to retrieve dropdown values. I can think of 2 options to do this.

  1. Invoke the servlet with $Path as one of the parameters. $Path will give you the complete page path, all the way till jcr:content. options=/bin/services/myservlet.GET_DROPDOWN_VALUES.json?compNode=$PATH.

In your servlet, construct page object using $PATH, then as @jwepurchase mentioned, get the locale using page.getLanguage(false).

String compNodePath = (String) request.getParameter("compNode");  
String pagePath = StringUtils.substringBefore(compNodePath, "jcr:content");  
PageManager pageMgr = request.getResourceResolver().adaptTo(PageManager.class);  
Page page = pageMgr.getContainingPage(pagePath);  
Locale pageLocale = page.getLanguage(false);

getLanguage will look for jcr:language property in currentpage or its ancestors. This property gets set when you set the Language field in page properties (advanced tab) of your page(usually set in the root locale page) to an appropriate value. eg: If you set the language to english(us), jcr:language will be en_us.

  1. Similar steps as option 1, if value in page property is not set, you can use currentPage.getAbsoluteParent(DEPTH_VAL) to retrieve the locale value. Not a recommended soln though.

Upvotes: 1

jwepurchase
jwepurchase

Reputation: 743

com.day.cq.wcm.api.Page.getLanguage(false) will return the value of the jcr:language property on the page or the first parent page where it is sent. Generally this isn't set on every page.

If you have included Adobe's global.jsp or used the <cq:defineObjects/> tag in your JSP, you should find that "currentPage" is already in scope, providing access to a Page object.

I generally have a custom taglib function to make it easier to access via EL. But as a scriptlet I expect the following would work:

<%= ((Page)getPageContext().getAttribute("currentPage")).getLanguage(false)%>

Upvotes: 0

Related Questions