简体   繁体   English

如何在Adobe CQ5中设置默认图像

[英]how to set a default image in adobe CQ5

I am creating some pages in which image component is being used. 我正在创建一些正在使用图像组件的页面。 I want to put mysitelogo.jpg(as default image), if image is not uploaded by user or image is not present. 如果用户未上传图片或图片不存在,我想放置mysitelogo.jpg(作为默认图片)。

below is exsiting code 下面是现有代码

String imgPath = properties.get(ybUrl, String.class);
if (imgPath != null && !imgPath.equals("")) {
    %><div class="image"><img src="<%= imgPath %>" alt="" ></div><%
}

@Mayank, @Mayank,

The Sling ValueMap allows for specifying either a class to cast the returned value or a default value when retrieving a property. Sling ValueMap允许在检索属性时指定一个类来强制转换返回值或默认值。 Replace your existing code with: 将现有代码替换为:

String imgPath = properties.get(ybUrl, "/some/path/mysitelogo.jpg");
%><div class="image"><img src="<%= imgPath %>" alt="" ></div><%

This will return the default value to your image (after you correct the path). 这将为图像返回默认值(在更正路径之后)。 Since the ValueMap will automatically return the default value if the retrieved value is null, you no longer have to do the null check. 由于如果检索到的值为null,ValueMap将自动返回默认值,因此您不再需要进行null检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM