【问题标题】:How to create a Liferay 7 Web Content template programmatically?如何以编程方式创建 Liferay 7 Web 内容模板?
【发布时间】:2017-11-07 02:44:30
【问题描述】:

在 Liferay 7 中,我有一个结构,一些 FTL 代码,我想在 Java Liferay 模块(无 portlet)中创建一个模板。就像人类对下面的 UI 所做的那样,但以编程方式:

注意:How to create Structure & Template programmatically in Liferay 6 提出的代码不起作用,它会导致Someone may be trying to circumvent the permission checker 异常。

另见:How to create a Liferay 7 structure programmatically?

【问题讨论】:

    标签: liferay freemarker liferay-7


    【解决方案1】:

    此代码采用structure (DDMStructure) 并成功创建模板:

    ServiceContext serviceContext = new ServiceContext();
    serviceContext.setScopeGroupId(group.getGroupId());
    serviceContext.setAddGroupPermissions(true);
    serviceContext.setAddGuestPermissions(true);
    serviceContext.setWorkflowAction(WorkflowConstants.ACTION_PUBLISH);
    
    String templateKey = "my template"; // Any name you want.
    
    long classNameId = PortalUtil.getPortal().getClassNameId(DDMStructure.class);
    long classPK = structure.getPrimaryKey();
    long resourceClassNameId = PortalUtil.getPortal().getClassNameId(JournalArticle.class);
    nameMap = Utils.createLocalesMap(templateKey);
    descriptionMap = nameMap; // Use the same.
    String type = "display";
    String mode = "create";
    String language = "ftl";
    String script = null;
    try {
        script = new String(Files.readAllBytes(Paths.get("/path/to/my/template.ftl")));
    } catch (IOException e) {
        log.error("Exception when reading template: " + templateDefinitionFilePath, e);
    }
    
    try {
        DDMTemplate template = DDMTemplateLocalServiceUtil.addTemplate(
            user.getUserId(), group.getGroupId(), classNameId, classPK,
            resourceClassNameId, nameMap, descriptionMap,
            type, mode, language, script, serviceContext);
    } catch (PortalException e) {
        log.error("Exception when creating template: " + templateDefinitionFilePath, e);
        return false;
    }
    

    【讨论】:

    • “有人可能试图绕过权限检查器”是权限检查器抛出的异常。可能是您或某些服务在内部使用安全 API ...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-17
    • 2011-09-16
    • 2011-12-28
    • 2019-07-20
    • 1970-01-01
    • 1970-01-01
    • 2015-12-25
    相关资源
    最近更新 更多