如何在 Java 中为 Liferay Web 内容设置类别?

How to set a category to a Liferay Web Content in Java?

在 Liferay 7 中,我有一个 Web 内容、一个词汇表和一个类别。
如何设置网页内容的类别?

我写了这段代码:

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

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

执行时没有错误,但创建的Web内容的编辑页面上没有显示类别:

已成功创建类别,但 Web 内容未分配该类别。

我做错了什么?

我也试过addAssetEntryAssetCategoriesaddAssetEntryAssetCategoryaddAssetCategoryAssetEntry:同样的问题。

尝试使用这两个函数中的任何一个来添加类别:

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 class。 访问这个和相关的 class 首先我添加了依赖到我的 build.gradle 文件

仅编译组:“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 对象 这对我来说很好