You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Liferay 7.4:自定义导航面包屑ADT模板适配问题求助

Liferay 7.4 自定义导航适配面包屑ADT解决方案

问题核心

默认面包屑组件的entries对象基于页面的站点层级路径生成,而非自定义导航菜单的结构,所以无法匹配你通过ADT实现的带分组的导航路径。要解决这个问题,需要从自定义导航菜单的条目集合中,找到当前页面对应的节点,再回溯父级节点来构建面包屑。

实现步骤

1. 修改面包屑ADT模板

替换原有模板代码为以下内容,该代码会从指定的自定义导航菜单中匹配当前页面,并递归构建完整的自定义导航路径:

<#assign navMenuId = 12345>  <!-- 替换为你的自定义导航菜单ID -->
<#assign navigationMenuDisplayContext = request.getAttribute("com.liferay.navigation.menu.web.internal.display.context.NavigationMenuDisplayContext")>
<#assign navMenuItems = navigationMenuDisplayContext.getNavigationMenuItemTree(navMenuId).getChildren()>

<#-- 递归查找当前页面对应的导航条目 -->
<#function findCurrentItem items currentURL>
  <#list items as item>
    <#assign itemURL = item.getURL()!""?trim>
    <#if itemURL == currentURL || (item.getLayout()?exists && item.getLayout().getFriendlyURL() == currentURL)>
      <#return item>
    <#elseif item.getChildren()?has_content>
      <#assign found = findCurrentItem(item.getChildren(), currentURL)>
      <#if found?exists>
        <#return found>
      </#if>
    </#if>
  </#list>
  <#return null>
</#function>

<#-- 获取当前页面的友好URL -->
<#assign currentLayout = themeDisplay.getLayout()>
<#assign currentURL = currentLayout.getFriendlyURL()!>

<#assign currentNavItem = findCurrentItem(navMenuItems, currentURL)>

<#-- 构建面包屑路径(从根到当前节点) -->
<#function buildBreadcrumbPath item>
  <#assign path = []>
  <#assign current = item>
  <#while current?exists>
    <#assign path = path + [current]>
    <#assign current = current.getParentNavigationMenuItem()!>
  </#while>
  <#return path?reverse>
</#function>

<#assign breadcrumbEntries = buildBreadcrumbPath(currentNavItem)>

<#if breadcrumbEntries?has_content>
  <ol class="breadcrumb">
    <#list breadcrumbEntries as entry>
      <li class="breadcrumb-item">
        <#if entry_has_next>
          <a class="breadcrumb-link"
            <#if entry.isBrowsable()>
              href="${htmlUtil.escapeAttribute(entry.getURL()!"")}"
            </#if>
          >
            <span class="breadcrumb-text-truncate">${htmlUtil.escape(entry.getTitle())}</span>
          </a>
        <#else>
          <span class="active breadcrumb-text-truncate">${htmlUtil.escape(entry.getTitle())}</span>
        </#if>
      </li>
    </#list>
  </ol>
</#if>

2. 关键说明

  • 替换导航菜单ID:将代码中的navMenuId值替换为你的自定义导航菜单的ID(可从导航菜单的编辑页面URL中获取,比如URL参数_com_liferay_navigation_menu_web_portlet_NavigationMenuPortlet_menuId=12345中的12345)。
  • 递归查找逻辑:findCurrentItem函数会遍历自定义导航的所有节点,匹配当前页面的友好URL,找到对应的导航条目。
  • 构建路径:buildBreadcrumbPath函数从当前条目向上回溯父节点,反转后得到从根到当前页面的自定义导航路径。
  • 适配无链接分组:保留了原模板中isBrowsable()的判断,无链接的分组项会显示为纯文本(如果是中间节点,也不会添加链接)。

3. ADT配置

在面包屑ADT的配置界面中,确保选择正确的作用范围;如果需要动态选择菜单,可将navMenuId改为从ADT配置参数中获取,需要在ADT的配置选项里添加菜单选择器。

内容的提问来源于stack exchange,提问作者Syrcom

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 06:55:22