[英]Liferay 7.4 - access structure data in ftl template
在以前的 Liferay 版本中,這是訪問結構數據字段(標題、副標題)的方法,但最近 liferay 從 xml 遷移到 json,所以這不起作用
journalArticle = assetRenderer.getArticle()
docXml = saxReaderUtil.read(journalArticle.getContent())
articleTitle = docXml.valueOf("//dynamic-element[@name='title']/dynamic-content/text()")
articleSubTitle = docXml.valueOf("//dynamic-element[@name='subtitle']/dynamic-content/text()")
我試圖訪問 json
docJSON = jsonFactoryUtil.convertXMLtoJSONMLArray(journalArticle.getContent())
但它不起作用。 現在如何訪問這些字段?
這就是 json 數組的樣子
讓我們考慮一個包含兩個文本字段的 web 內容結構:joboffering 和 workplace(最后一個是可重復的)。 請注意,在 Liferay 7.4 中,您分配給字段的名稱存儲為“field-reference”屬性; 在以前的版本中,您分配給字段的名稱存儲為“名稱”屬性。
然后,我為資產發布者創建了以下小部件模板(為簡單起見,我只呈現了第一個條目),它按預期工作:
<#if entries?has_content>
<#assign entry = entries[0]
assetRenderer = entry.getAssetRenderer()
journalArticle = assetRenderer.getArticle()
docXML = saxReaderUtil.read(journalArticle.getDocument().asXML())
docJSON = jsonFactoryUtil.convertXMLtoJSONMLObject(journalArticle.getDocument().asXML())
joboffering = docXML.selectSingleNode("/root/dynamic-element[@field-reference='joboffering']/dynamic-content").getText()
workplaces = docXML.selectNodes("/root/dynamic-element[@field-reference='workplace']/dynamic-content")
/>
<h1>
${joboffering}
</h1>
<ul>
<#list workplaces as workplace>
<li>${workplace.text}</li>
</#list>
</ul>
<code>${docJSON}</code>
</#if>
然后是 XPath 查詢
/root/dynamic-element[@field-reference='joboffering']/dynamic-content
對於第一個字段(請注意,我使用了field-reference
屬性),以及
/root/dynamic-element[@field-reference='workplace']/dynamic-content
第二個。 這第二個 XPath 在selectNodes()
方法中執行,並通過循環打印結果。
我還打印了json版的內容。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.