【问题标题】:how to check null from JCR value in Adobe CQ5..?如何从 Adob​​e CQ5 中的 JCR 值中检查 null ..?
【发布时间】:2015-02-09 12:32:19
【问题描述】:

大家好,我是新的 Adob​​e CQ5,我正在从 Adob​​e CQ5 组件中的 JCR 获取值,值呈现正常但现在想检查 null,我正在这样做:

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

但它会产生一个错误,任何人都可以建议我做错了什么。

【问题讨论】:

  • 如何初始化properties 对象?
  • 我在 CRX 中将此名称声明为属性,我很好,但在检查 null 时遇到问题

标签: jsp adobe components aem jcr


【解决方案1】:

使用JSTL:

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

或从 JSP 切换到 Sightly(推荐选项):

<img data-sly-test="${properties.videoImage}" src="${properties.videoImage}" />

【讨论】:

  • 你能建议我如何检查 Anchor href 的相同空条件
【解决方案2】:

这是实现它的更好方法。

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

理想情况下,将所有变量设置为会话、请求、页面上下文属性在顶部的一个位置,您可以在整个组件 jsp 中使用它,或者更简洁的方法是在 jsp 中声明所有变量并使用 @ 包含它们987654322@

【讨论】:

  • properties 对象已经设置为页面属性,所以如果我们在 JSTL 指令中使用properties.videoImage,第一行是多余的。详情请参阅我的回复。
【解决方案3】:

我想问题是你没有正确终止scriptlets

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

不要在你的 jsp 中混用 htmlJava 代码。

阅读How to avoid Java code in JSP files?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多