簡體   English   中英

如何在 Java 中為 Liferay Web 內容設置類別?

[英]How to set a category to a Liferay Web Content in Java?

在 Liferay 7 中,我有一個 Web 內容、一個詞匯表和一個類別。
如何將類別設置為 Web 內容?

我寫了這段代碼:

article = JournalArticleLocalServiceUtil.addArticle(...);
category = AssetCategoryLocalServiceUtil.addCategory(...);

AssetCategoryLocalServiceUtil.setAssetEntryAssetCategories(
    article.getPrimaryKey(), new long[]{category.getPrimaryKey()});

執行時沒有錯誤,但該類別沒有顯示在創建的 Web 內容的編輯頁面上:

在此處輸入圖片說明

類別已成功創建,但 Web 內容未分配該類別。

我究竟做錯了什么?

我也試過addAssetEntryAssetCategoriesaddAssetEntryAssetCategoryaddAssetCategoryAssetEntry :同樣的問題。

嘗試使用以下 2 個函數中的任何一個來添加類別:

addAssetEntryAssetCategory(long entryId, long categoryId);
addAssetEntryAssetCategories(long entryId, long[] categoryIds);

在您的代碼中,您使用的是primary_key,但是,根據文檔,您應該使用條目ID 和類別ID。 所以你的函數調用應該是這樣的:

AssetEntry entry = AssetEntryLocalServiceUtil.fetchEntry(JournalArticle.class.getName(),  article.getResourcePrimKey());

AssetCategoryLocalServiceUtil.addAssetEntryAssetCategory(
    entry.getEntryId(), category.getCategoryId());

從 7.0 開始,他們從 JournalArticle 中刪除了 getEntryId 方法,您需要額外調用才能獲取它。 有一種update方法,您也可以考慮在單次調用中完成此操作。 我仍在使用 6.2 並趕上 7 :)。

請注意,類別是為管理員而非普通用戶設計的。

我正在使用 liferay 7.1 dxp

就我而言,我必須使用程序更新期刊文章或網絡內容的類別。 為了實現這一點,我必須使用 assetEntryAssetCategoryRel 類。 為了首先訪問這個和相關的類,我在 build.gradle 文件中添加了依賴項

compileOnly 組:“com.liferay”,名稱:“com.liferay.asset.entry.rel.api”,版本:“1.1.0”

List<AssetEntryAssetCategoryRel> assetEntryAssetCategoryRelsByAssetEntryId = AssetEntryAssetCategoryRelLocalServiceUtil.
                    getAssetEntryAssetCategoryRelsByAssetEntryId(assetEntry.getEntryId());
if(assetEntryAssetCategoryRelsByAssetEntryId!=null && !assetEntryAssetCategoryRelsByAssetEntryId.isEmpty()){
                    AssetEntryAssetCategoryRel assetEntryAssetCategoryRel = assetEntryAssetCategoryRelsByAssetEntryId.get(0);
                    assetEntryAssetCategoryRel.setAssetCategoryId(assetCategory.getCategoryId());
                    assetEntryAssetCategoryRel = AssetEntryAssetCategoryRelLocalServiceUtil.updateAssetEntryAssetCategoryRel(assetEntryAssetCategoryRel);
                    
                }

我有 assetentry 和 assetcategory 對象這對我來說很好用

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM