尝试在freemarker中访问我的自定义util-Liferay



我有一个实用程序来访问我的自定义Liferay配置(而不是默认的Liferay(,我可以在java代码中做我想做的事情,但当涉及到freemarker时,我陷入了困境。

这是我想在Liferay主题免费标记模板中访问的类:

类名:CommonServicesUtil


package mycompany.liferay.modules.common_services.util;
public class CommonServicesUtil {
public static CommonServicesGroupConfiguration getCommonServicesGroupConfiguration(final long groupId) {
try {
return ConfigurationProviderUtil.getConfiguration(CommonServicesGroupConfiguration.class, new GroupServiceSettingsLocator(groupId, CommonServicesConstants.COMMON_SERVICES_API_BUNDLE_NAME));
} catch (ConfigurationException ex) {
throw new ArenaRuntimeException(ex.getMessage(), ex);
}
}
public static CommonServicesSystemConfiguration getCommonServicesSystemConfiguration() {
try {
return ConfigurationProviderUtil.getSystemConfiguration(CommonServicesSystemConfiguration.class);
} catch (ConfigurationException ex) {
throw new ArenaRuntimeException(ex.getMessage(), ex);
}
}
}

在freemarker中,我尝试了以下操作:

<#assign configuration = serviceLocator.findService("mycompany.liferay.modules.common_services.util.CommonServicesUtil")>
<#assign ConfigurationProvider = objectUtil("mycompany.liferay.modules.common_services.util.CommonServicesUtil")  />
<#assign ConfigurationProvider = staticUtil["mycompany.liferay.modules.common_services.util.CommonServicesUtil"]  />

我的救生筏版本是7.4

我正在尝试访问已添加到Liferay配置的配置

我一直认为serviceLocator适用于Liferay的Service Builder类。如果您需要任意类,我不知道它会如何在类路径上找到这些类(它可能来自许多OSGi捆绑包(至少,您需要导出类或接口。

我建议实现TemplateContextContributor,这样可以使模板更加干净,并且不需要模拟识别特定的实现对象。

有一种很好的方法可以通过使用Template Context Contributor来获得您想要的内容请查看救生筏网站

这里有的样品

通过运行Template Context Contributor的blade命令或使用maven check liferay网站创建一个新模块,然后您将得到一个新的模块,该模块具有以下类:

package com.liferay.blade.samples.theme.contributor;
import com.liferay.portal.kernel.template.TemplateContextContributor;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.osgi.service.component.annotations.Component;
@Component(
immediate = true,
property = "type=" + TemplateContextContributor.TYPE_GLOBAL,
service = TemplateContextContributor.class
)
public class BladeTemplateContextContributor
implements TemplateContextContributor {
/**
* Injects a new string variable into the map of provided variables. The map
* is made available to non-JSP templates (FreeMarker, Velocity, etc.) that
* do not have access to the contextual objects native to the platform, like
* the request and session.
*
* <p>
* The <code>sample_text</code> variable can be used in any theme file.
* For example, you could add it to the <code>portal_normal.ftl</code> file
* in your theme as <code>${sample_text}</code>.
* </p>
*
* @param contextObjects the variables available in the context
* @param httpServletRequest the HTTP servlet request
*/
@Override
public void prepare(
Map<String, Object> contextObjects,
HttpServletRequest httpServletRequest) {
contextObjects.put("sample_text", "This is some sample text");
}
}

打包并部署到救生舱tomcat后,您将能够访问freemarker、速度、主题的值

在这样的自由标记中:${sample_text}

此外,您可以看到这里的范围是GLOBAL属性=";type="+"TemplateContextContributor.TYPE_GLOBAL

如果你想获得主题中的值,你可以将其更改为主题

最新更新