Issue
- Is there an option/API available to call OSGI Services directly in fragments like it's possible using serviceUtil and staticUtil?
Environment
- DXP 7.4
Resolution
-
Yes, you can obtain this by using
serviceLocator
inside the HTML code of your fragment, same as it is used inside free-markers.
The only thing is changing inside fragments is that<
changes for[
and>
for]
, like this:
[#assign exampleServiceUtil = serviceLocator.findService("com.liferay.example.service.ExampleService") /]
Keep in mind thatserviceLocator
is restricted by default for security reasons. In order to enable it, you need to follow this steps: Changes in Enabling serviceLocator Calls in Liferay DXP 7.0
It is important to understand that activatingserviceLocator
from a security point of view, is considered a bad idea because it opens the access to all of your services from freemarker. That means that you explicitly trust anybody who's allowed to write freemarker templates to run server-side code (and have access to the LocalService APIs)
Because of that, we recommend usingTemplateContextContributors
that helps you make available only what you need. See Injecting Additional Context Variables and Functionality into Your Theme Templates
The code is very simple, and you can inject whatever you need, see this example: Template Context Contributor
Other article that might be of your interest: Non service builder generated services are not retrieved by ServiceLocator in freemarker template
Conteúdo Excluesivo para Assinantes
Uma Subscrição do Liferay Enterprise fornece acesso a mais de 1.500 artigos que incluem práticas recomendadas, solução de problemas e outras soluções valiosas. Faça login para obter acesso completo.
Entrar