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

Liferay 7.4如何从ddmfieldattribute表取smallAttributeValue及ADT获取结构字段

解决方案

前置注意事项

  • 若你使用serviceLocator调用服务,需要先在控制面板 -> 系统设置 -> 模板引擎 -> 自由标记中开启「允许在模板中使用服务定位器」配置,否则服务调用会失败。
  • 以下方案默认你使用的是资产发布器ADT模板,遍历的entries对象为AssetEntry实例。

方案1:通过DDMFormValues获取字段(适配所有自定义结构字段)

该方案兼容任意自定义结构的字段读取,直接读取底层ddmfieldattribute关联的结构数据:

<#-- 引入所需服务,你原来的dlService如果没有用到可以删除 -->
<#assign journalArticleService = serviceLocator.findService("com.liferay.journal.service.JournalArticleLocalService")>

<#if entries?has_content>
    <#list entries as entry>
        <#if entry.getClassName() == "com.liferay.journal.model.JournalArticle">
            <#-- 获取最新版本的文章实例 -->
            <#assign article = journalArticleService.fetchLatestArticle(entry.getClassPK(), 0)>
            <#-- 取当前站点的显示语言,也可固定指定如"zh_CN" -->
            <#assign currentLocale = themeDisplay.getLocale() />
            
            <#if article.getDDMFormValues()??>
                <#assign formValues = article.getDDMFormValues() />
                <#-- 读取title字段值,字段名必须和结构定义的名称完全一致,区分大小写 -->
                <#assign titleField = formValues.getDDMFormFieldValue("title") />
                <#assign titleValue = titleField.getValue(currentLocale) />
                <#-- 读取content字段值 -->
                <#assign contentField = formValues.getDDMFormFieldValue("content") />
                <#assign contentValue = contentField.getValue(currentLocale) />
                
                <#-- 字段使用示例,富文本字段可以加?no_escape取消转义输出原始HTML -->
                <div class="journal-item">
                    <h2>${titleValue}</h2>
                    <div class="journal-content">${contentValue?no_escape}</div>
                </div>
            </#if>
        </#if>
    </#list>
</#if>

方案2:无服务调用简化实现(无需开启serviceLocator权限)

如果不想修改系统配置开启服务定位器,也可以通过资产渲染器直接获取字段,代码更简洁:

<#if entries?has_content>
    <#list entries as entry>
        <#if entry.getClassName() == "com.liferay.journal.model.JournalArticle">
            <#assign assetRenderer = entry.getAssetRenderer() />
            <#assign article = assetRenderer.getAssetObject() />
            <#assign currentLocale = themeDisplay.getLocale() />
            
            <#-- 直接读取字段值 -->
            <#assign titleValue = article.getTitle(currentLocale) />
            <#assign contentValue = article.getContentByLocale(currentLocale.toString()) />
            
            <div class="journal-item">
                <h2>${titleValue}</h2>
                <div class="journal-content">${contentValue?no_escape}</div>
            </div>
        </#if>
    </#list>
</#if>

补充说明

如果你的结构中存在其他自定义字段,替换方案1中getDDMFormFieldValue的参数为对应字段名即可,不需要修改其他逻辑。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.27 16:06:04