如何在 Java 中创建 Liferay 网页内容?

How to create Liferay web content in Java?

通过 UI 创建 Web 内容很容易。

但是如何在 Java 中以编程方式添加新的 Web 内容?

我必须将数据从遗留系统迁移到 Liferay 7,因此我正在编写一个 Java OSGI 包来执行此操作。没有用户界面。

尼古拉斯

我在 Liferay 6.2 中有一个类似的问题需要解决,但我相信您可以使用相同的方法解决您的问题。

我们构建了一个 "integration interface"(一个简单的 Java 批处理项目来触发整个事情),它与遗留系统和 Liferay REST 服务(使用 Liferay Service Builder 创建)进行通信。

Liferay 为您提供了一项服务 API,您可以在其中处理它的一些资源。要创建期刊文章(Web 内容),您必须调用 Class JournalArticleLocalServiceUtil

这是创建期刊文章的示例代码:

public static JournalArticle addJournalArticle(
        long userId, long groupId, String title, String contentEn)
        throws Exception {

    ServiceContext serviceContext = new ServiceContext();

    serviceContext.setAddGroupPermissions(true);
    serviceContext.setAddGuestPermissions(true);
    serviceContext.setScopeGroupId(groupId);
    serviceContext.setWorkflowAction(WorkflowConstants.ACTION_PUBLISH);
    Map<Locale, String> titleMap = new HashMap<Locale, String>();
    Map<Locale, String> descriptionMap = new HashMap<Locale, String>();

    titleMap.put(Locale.US, title);
    descriptionMap.put(Locale.US, title);


    try {
        JournalArticleLocalServiceUtil.deleteArticle(groupId, title, serviceContext);
    } catch (Exception ex) {
        System.out.println("Ignoring " + ex.getMessage());
    }

    String xmlContent = "<?xml version='1.0' encoding='UTF-8'?>" +
            "<root default-locale=\"en_US\" available-locales=\"en_US\">" +
                "<static-content language-id=\"en_US\">" +
                    "<![CDATA[" + contentEn + "]]>" +
                "</static-content>" +
            "</root>";

    JournalArticle article = JournalArticleLocalServiceUtil.addArticle(
            userId, groupId, 0, 
            0, 0, title, true, 
            JournalArticleConstants.VERSION_DEFAULT, titleMap, 
            descriptionMap, xmlContent, 
            "general", null, null, null, 
            1, 1, 2014, 0, 0,
            0, 0, 0, 0, 0, true, 
            0, 0, 0, 0, 0, true, 
            true, false, null, null, 
            null, null, serviceContext);

    return article;
}

但你必须改进它以放置正确的用户权限、类别、标签等。

在这种情况下,查看 source code 会有所帮助。

也可以考虑使用 Upgrade Process。虽然您的情况并不是真正的更新,但听起来像是一次操作,您最好在启动时执行。