0

Hi all i'm new Adobe CQ5, i'm getting values from JCR in Adobe CQ5 component, value is rendering fine but now want to check null, i'm doing like this:

  <% if(<%= properties.get("videoImage") %> != null)
     {
       <img src=<%= properties.get("videoImage") %> />
     }
  %> 

but it generate an error, can any one suggest what i'm doing wrong.

Amit Sharma
  • 1,088
  • 1
  • 19
  • 44

3 Answers3

6

Use JSTL:

<c:if test="${not empty properties.videoImage}">
    <img src="${properties.videoImage}" />
</c:if>

or switch from JSP to Sightly (a recommended option):

<img data-sly-test="${properties.videoImage}" src="${properties.videoImage}" />
Tomek Rękawek
  • 9,204
  • 2
  • 27
  • 43
1

I guess the problem is you are not terminating the scriptlets properly ,

  <% if(properties.get("videoImage") != null)
     { %> 
       <img src=<%= properties.get("videoImage") %> />
   <%}
  %> 

Dont mix html and Java codes in your jsp.

Read How to avoid Java code in JSP files?

Community
  • 1
  • 1
Santhosh
  • 8,181
  • 4
  • 29
  • 56
1

This is better way to achieve it.

<% pageContext.setAttribute("videoImage", properties.get("videoImage", ""));%>
    <c:if test="${not empty videoImage}">
        <img src="${videoImage}" />
    </c:if>

Ideally set all of your variables to session, request, page context attributes at one place in top and you may use it throughout component jsp, alternatively much cleaner approach will be to declare all of them in a jsp and include them using <cq:include script"path/to/jsp">

yash ahuja
  • 470
  • 1
  • 8
  • 23
  • 1
    `properties` object is already set as the page attribute, so the first line is redundant if we use `properties.videoImage` in JSTL directives. See my response for more details. – Tomek Rękawek Feb 09 '15 at 12:57