我想避免激活一些网页,如果它的内容是空的。我使用一些servlet来完成这个任务,如下所示:
@SlingServlet(paths = "/bin/servlet", methods = "GET", resourceTypes = "sling/servlet/default")
public class ValidatorServlet extends SlingAllMethodsServlet {
@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) {
String page = "pathToPage";
PageManager pageManager = request.adaptTo(PageManager.class);
Page currentPage = pageManager.getPage(page);
boolean result = pageHasContent(currentPage);
}现在如何检查,currentPage是否有内容?
发布于 2013-11-11 12:17:08
请注意,以下答案是在2013年CQ/AEM与当前版本有很大不同时发布的。如果使用,下面的操作可能会不一致。有关此问题的更多信息,请参阅塔迪娅·马利奇的回答如下。
Page类的hasContent()方法可用于检查页面是否有内容。如果页面有jcr:content节点,则返回true,否则返回false。
boolean result = currentPage != null ? currentPage.hasContent() : false;
如果您想检查尚未创作的页面,一种可能的方法是检查jcr:content下是否存在其他节点。
Node contentNode = currentPage.getContentResource().adaptTo(Node.class);
boolean result = contentNode.hasNodes();发布于 2013-11-12 09:20:47
我将创建一个OSGi服务,它接受一个页面,并根据您设置的规则遍历它的内容树,以确定页面是否有意义的内容。
页面是否有实际内容是特定于应用程序的,因此创建自己的服务将使您完全控制该决定。
发布于 2021-02-18 12:31:10
一种方法是进行使用相同的模板创建一个新页面,然后遍历节点列表并计算组件的散列(或它们的内容取决于您想要比较的内容)。一旦获得了空页模板的散列,那么您就可以将任何其他页面哈希与该.进行比较。
注意:这个解决方案需要适应您自己的用例。也许检查页面上有哪些组件以及它们的顺序就足够了,也许您也想比较它们的配置。
private boolean areHashesEqual(final Resource copiedPageRes, final Resource currentPageRes) {
final Resource currentRes = currentPageRes.getChild(com.day.cq.commons.jcr.JcrConstants.JCR_CONTENT);
return currentRes != null && ModelUtils.getPageHash(copiedPageRes).equals(ModelUtils.getPageHash(currentRes));
}模型Utils:
public static String getPageHash(final Resource res) {
long pageHash = 0;
final Queue<Resource> components = new ArrayDeque<>();
components.add(res);
while (!components.isEmpty()) {
final Resource currentRes = components.poll();
final Iterable<Resource> children = currentRes.getChildren();
for (final Resource child : children) {
components.add(child);
}
pageHash = ModelUtils.getHash(pageHash, currentRes.getResourceType());
}
return String.valueOf(pageHash);
}
/**
* This method returns product of hashes of all parameters
* @param args
* @return int hash
*/
public static long getHash(final Object... args) {
int result = 0;
for (final Object arg : args) {
if (arg != null) {
result += arg.hashCode();
}
}
return result;
}注意:使用队列也会考虑组件的顺序。
这是我的方法,但我有一个非常具体的用例。通常,如果您真的想要计算要发布的每个页面上的每个组件的散列,您可能会想一想,因为这会减慢发布过程。您还可以在每次迭代中比较散列,并在第一个差异上中断计算。
https://stackoverflow.com/questions/19905514
复制相似问题