Jahia get live workspace JCR data from edit mode

427 Views Asked by At

I'm using Jahia Digital Experience Manager 7.1.2.

How can I display data from live workspace in edit mode (in JSTL from a JSP)?


Details :

I need to display some data in a page in edit mode. Problem is that data is user submitted content stored only in JCR live workspace (not stored in the default workspace used by edit mode).

Usually I display data like this (works great for displaying default workspace data in edit mode and live data in online mode) :

<%@ taglib prefix="jcr" uri="http://www.jahia.org/tags/jcr" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--@elvariable id="currentNode" type="org.jahia.services.content.JCRNodeWrapper"--%>

<c:forEach items="${jcr:getChildrenOfType(currentNode,'unt:formResponse')}" var="resp">
     ${resp.name}
</c:forEach>

But of course as my data exist only in live workspace, nothing is displayed in edit mode.

Using a scriptlet I am able to get the live content :

<c:set var="currentNodeId" value="${currentNode.identifier}"/>
<c:set var="locale" value="${renderContext.mainResourceLocale}"/>
<%
     JCRNodeWrapper responsesNode = JCRSessionFactory.getInstance().getCurrentUserSession("live", locale).getNodeByIdentifier(currentNodeId);
     for (JCRNodeWrapper resp : responsesNode.getNodes()) {
          log.info(resp.getName());
     }
     pageContext.setAttribute("responsesNode", responsesNode);
%>
<c:forEach items="${jcr:getChildrenOfType(responsesNode,'unt:formResponse')}" var="resp">
     ${resp.name}
</c:forEach>

I can also do it in full java scriptlet (or better with a taglib) or in groovy, but is there any way to do it in pure JSTL ?

1

There are 1 best solutions below

1
On

You'r right, in Jahia you don't have the control on the rendering servlet before the jsp component view.

Moreover, it's not easy to switch the mode in default JSP tags (because, on normal use, you render the content only from the current workspace).

But using a filter is not a good idea either in this case, because a filter could cause performance or refresh issues (break the default cache mechanism in JSP Jahia view component), and will be difficult to reuse in a different context in your code.

However, there is a possible solution to keep your JSP code clean :

  • Create your own JSP tag with your scriptlet code inside it, then call inside it the method JCRContentUtils.getChildrenOfType(responsesNode, type); (with type = 'unt:formResponse' in parameter for your example)
  • You can name this tag getChildrenOfTypeInLive(...) for example, with a tag lib prefix = myjcr.

Documentation:

I hope this will help you,

Regards,

Cédric