In the previous example, the portlet doesn't have a view, so if you install it, you will see nothing in the page of the portal. In this recipe, you will see the complete configuration and creation of the views for the portlet.
Add a new view for the portlet using Groovy, WebUI's preferred scripting tool. With Groovy, it is very simple to handle the WebUI components. Therefore, we need to add a new component to handle it in the view.
Carry out the following steps:
MyPortlet.gtmpl
inside the path MyApplication.war/groovy/MyApplication/webui/component
:<% def rcontext = _ctx.getRequestContext() ; %> <div id="$uicomponent.id" class="MyPortlet"> Started!!! </div>
configuration.xml
: <ui-component-config>
<type>
my.sample.MyPortlet
</type>
<lifecycle>
org.exoplatform.webui.core.lifecycle.UIApplicationLifecycle
</lifecycle>
<template>
app:/groovy/MyApplication/webui/component/MyPortlet.gtmpl
</template>
</ui-component-config>
Or in the annotation:
@ComponentConfig( type = MyPortlet.class, lifecycle = UIApplicationLifecycle.class, template = "app:/groovy/MyApplication/webui/component/MyPortlet.gtmpl") public class MyPortlet extends UIPortletApplication { ... }
The template tag sets the default view for the portlet. If you install the portlet in the main page under the HomePortlet
, you will see something similar to the following screen:
Component
as annotation:@ComponentConfigs({
@ComponentConfig(
lifecycle = UIApplicationLifecycle.class,
template = " app:/groovy/MyApplication/webui/component/MyPortlet.gtmpl "
),
@ComponentConfig(
type = UIPortalNavigation.class,
id = "MyUISiteMap")
})
public class MyPortlet extends UIPortletApplication {
...
}
The same thing can be done through XML:
<ui-component-config> <type> my.samples.MyPortlet </type> <lifecycle> org.exoplatform.webui.core.lifecycle.UIApplicationLifecycle </lifecycle> <template> app:/groovy/MyApplication/webui/component/MyPortlet.gtmpl </template> </ui-component-config> <ui-component-config id="MyUISiteMap"> <type> org.exoplatform.portal.webui.navigation.UIPortalNavigation </type> </ui-component-config>
In this example, you have two configurations: one is the example showed before, and for the second, we declare a new component, MyPortlet
, through a WebUI component, the UIPortalNavigation
, the component used for the navigation of the portal.
UIPortalNavigation
component as a child inside the application:public MyPortlet() throws Exception { // Take the current PortletRequest PortletRequestContext context = (PortletRequestContext) WebuiRequestContext .getCurrentInstance(); PortletRequest prequest = context.getRequest(); /* Take the portlet preferences and find a preference called template. If it doesn't exist add the default template UISitemapTree.gtmpl. It implements a navigable tree structure */ PortletPreferences prefers = prequest.getPreferences(); String template = prefers.getValue("template", "system:/groovy/webui/core/UISitemapTree.gtmpl"); /* Add the UIPortalNavigation as a child of the application and set two properties */ UIPortalNavigation uiPortalNavigation = addChild( UIPortalNavigation.class, "MyUISiteMap", null); uiPortalNavigation.setTemplate(template); uiPortalNavigation.setUseAjax(false); /* Set in memory the informations present in the tree. In this code are memorized the first 2 levels of the tree */ uiPortalNavigation.setScope(GenericScope.treeShape(2)); }
MyPortlet.gtmpl
so that we can call the new inserted component:<%
import org.exoplatform.portal.webui.navigation.UIPortalNavigation;
def uiPortalNavigation = uicomponent.getChild(UIPortalNavigation.class);
uiPortalNavigation.loadTreeNodes();
uicomponent.renderChildren();
%>
Through the
renderChildren()
method, the component will be rendered for all its children. Each component will query their template and will provide their own graphic part to the page. Here is the result:
Of course, it is still a crude window. You will see how to decorate it better in the Handling different skins in a portlet recipe.
The Groovy scripts are elaborated by a service, the Template Service. This service is loaded in the file $PORTAL_ROOT/WEB-INF/conf/common/common-configuration.xml
:
<component> <type> org.exoplatform.groovyscript.text.TemplateService </type> </component>
It is based on Groovy, a highly efficient scripting language used for the view.
As with most components, the Template Service starts during the loading of the page that uses Groovy scripts. It manages an internal cache where it preserves the bytes and compiles the pages.
The Groovy scripts can be declared with different schemas. Each schema responds to a different resource resolver. The resource resolver searches and loads the resources. Here are the available schemas:
org.exoplatform.resolver.ServletResourceResolver
. It finds the resources in the ServletContext
.org.exoplatform.resolver.ClasspathResourceResolver
.org.exoplatform.resolver.PortletResourceResolver
.org.exoplatform.resolver.PortletResourceResolver
. It finds the resources in the PortletContext
.org.exoplatform.resolver.ServletResourceResolver.
org.exoplatform.resolver.FileResourceResolver
.org.exoplatform.resolver.FileResourceResolver
.The Template service doesn't need configuration, unless you use different Groovy compiler settings, which you can change only by directly working on the class.
Notate the
uicomponent
key in the Groovy script. It allows loading the current UI component application that you have declared in the configuration.xml
:
<application> <ui-component-root> my.sample.MyPortlet </ui-component-root> ...
The other keys available for the Groovy scripts can be found in:
uicomponent
represented as org.exoplatform.web.url.navigation.NodeURL
instance.UIPortletLifecycle
. It contains the text inside the portlet. It is represented by the org.exoplatform.commons.utils.Text
instance, and is very useful for AJAX or REST calls.These variables are present in the processRender
and
renderTemplate
methods seen in the WebUI lifecycle that you have configured in the portlet. The processRender
method adds these variables in memory (except for the locale) so that they can be used in the page. The renderTemplate
adds the locale to the GroovyContext
and passes it to the Template Service.
In this recipe, we introduced the concept of a service. You have seen the first service in the previous chapter with the OrganizationService
. All services in GateIn can be easily identified/recognized as they all end with the name Service
.
All the services are declared in the portal root application, but they can be added in other portal applications. The convention for a configuration is to have the name of the service followed by –configuration.xml
; for example, common-configuration.xml
.
The configuration system is based on Pico Container, a framework similar to Spring used to instantiate objects through the XML configuration. Details for Pico Container can be seen here: http://picocontainer.org.
A service can be loaded inside any UIComponent
. For example, in MyPortlet
, we can call the Template Service as follows:
PortletRequestContext context = (PortletRequestContext) WebuiRequestContext
.getCurrentInstance();
ExoContainer pcontainer = context.getApplication().getApplicationServiceContainer();
TemplateService service = (TemplateService)pcontainer.getComponentInstanceOfType(TemplateService.class);
Or simply:
TemplateService templateService = this.getApplicationComponent(TemplateService.class)
Or by Groovy:
def templateService = uicomponent.getApplicationComponent(TemplateService.class);
GateIn provides many services so it is not possible to write about all of them. We will explain the main services in the next recipes for dialed argument.
Another service tied to TemplateService
is the TemplateStatisticService
. It is used to monitor the execution time of a Groovy script. Here are the signatures of the methods:
getTemplateList()
getMaxTime(String id_template)
getMinTime(String id_template)
getExecutionCount(String id_template)
getAverageTime(String id_template)
getSlowestTemplates()
getMostExecutedTemplates()
getFastestTemplates()
18.117.232.239