How to link my page created programatically to its own module

João Machado, modified 6 Years ago. New Member Posts: 4 Join Date: 10/31/18 Recent Posts
Hello, everyone, i am using Liferay 7.1 and trying to implement a page creation programatically, and i managed that already. I have this difficulty, i dont understand how to associate my content of the module (a simple Hello from "xpto") to the newly created page where i have the code. When i access the page via friendly URL, the page is blank. Please help me. Kind regards.

I really need your help, friends, please advise.
thumbnail
Minhchau Dang, modified 6 Years ago. Liferay Master Posts: 598 Join Date: 10/22/07 Recent Posts
João Machado

I have this difficulty, i dont understand how to associate my content of the module (a simple Hello from "xpto") to the newly created page where i have the code.

Liferay has a utility method, DefaultLayoutPrototypesUtil.addPortletId, that it uses when it creates page templates. I don't see any code that looks specific to page templates (so in theory, it can be used for any page), so you should be able to use it in your own custom code in order to add portlets to pages.
João Machado, modified 6 Years ago. New Member Posts: 4 Join Date: 10/31/18 Recent Posts
Minhchau Dang
Liferay has a utility method, DefaultLayoutPrototypesUtil.addPortletId, that it uses when it creates page templates. I don't see any code that looks specific to page templates (so in theory, it can be used for any page), so you should be able to use it in your own custom code in order to add portlets to pages.

Thank you, Minhchau,

how would i use this method in my created page? As where in the code that generates the page? The portlet is already in the module that calls the programatically generated page. Its unclear to me.

ThemeDisplay themeDisplay =(ThemeDisplay)request.getAttribute(WebKeys.THEME_DISPLAY);
String layoutPrototypeId = DefaultLayoutPrototypesUtil.addPortletId(themeDisplay.getLayout(), themeDisplay.getPortletDisplay().getId(), themeDisplay.getPortletDisplay().getColumnId());
System.out.println(layoutPrototypeId);

This generates a java.lang.NullPointerException

And when i try with a hard coded layoutprototypeID i get this stacktrace...

2018-11-05 10:16:57.801 ERROR [http-nio-8081-exec-1][VirtualHostFilter:375] java.lang.IllegalArgumentException: Path [web/guest/home] does not start with a "/" character
java.lang.IllegalArgumentException: Path [web/guest/home] does not start with a "/" character
    at org.apache.catalina.core.ApplicationContext.getRequestDispatcher(ApplicationContext.java:404)
    at org.apache.catalina.core.ApplicationContextFacade.getRequestDispatcher(ApplicationContextFacade.java:222)
    at com.liferay.portal.servlet.filters.virtualhost.VirtualHostFilter.processFilter(VirtualHostFilter.java:370)
    at com.liferay.portal.kernel.servlet.BaseFilter.doFilter(BaseFilter.java:49)
    at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.processDoFilter(InvokerFilterChain.java:207)
    at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.doFilter(InvokerFilterChain.java:112)
    at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.processDirectCallFilter(InvokerFilterChain.java:188)
    at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.doFilter(InvokerFilterChain.java:96)
    at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.processDirectCallFilter(InvokerFilterChain.java:188)
    at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.doFilter(InvokerFilterChain.java:96)
    at org.tuckey.web.filters.urlrewrite.RuleChain.handleRewrite(RuleChain.java:176)
    at org.tuckey.web.filters.urlrewrite.RuleChain.doRules(RuleChain.java:145)
    at org.tuckey.web.filters.urlrewrite.UrlRewriter.processRequest(UrlRewriter.java:92)
    at org.tuckey.web.filters.urlrewrite.UrlRewriteFilter.doFilter(UrlRewriteFilter.java:389)
    at com.liferay.portal.servlet.filters.urlrewrite.UrlRewriteFilter.processFilter(UrlRewriteFilter.java:65)
    at com.liferay.portal.kernel.servlet.BaseFilter.doFilter(BaseFilter.java:49)
    at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.processDoFilter(InvokerFilterChain.java:207)
    at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.doFilter(InvokerFilterChain.java:112)
    at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.processDirectCallFilter(InvokerFilterChain.java:168)
    at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.doFilter(InvokerFilterChain.java:96)
    at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.processDirectCallFilter(InvokerFilterChain.java:168)
    at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.doFilter(InvokerFilterChain.java:96)
    at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.processDirectCallFilter(InvokerFilterChain.java:188)
    at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.doFilter(InvokerFilterChain.java:96)
    at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilter.doFilter(InvokerFilter.java:100)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:494)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:651)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:407)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:754)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1376)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:748)

I have made available my snippet of code here, please share your insights.
https://github.com/jmrmm-eurohelp/liferay

Would this be an option also?
https://community.liferay.com/forums/-/message_boards/message/106173

Kind regards.
João Machado, modified 6 Years ago. New Member Posts: 4 Join Date: 10/31/18 Recent Posts

Hello to all,

 

could anyone assist me in this topic, please?

 

I am trying to use portal startup events to deploy my pages. (following: https://github.com/jmrmm-eurohelp/startupLiferay)

How can i achieve this? Per documentation as follows:

 

Portal Context Initialization Phase

The Portal Context Initialization phase runs first with these tasks:

  1. Set up low level utilities such as logging and those in PortalUtil and InitUtil.

  2. OSGi framework is initialized.

  3. Spring Phase 1: INFRASTRUCTURE beans specified by the Spring context files listed in Portal property spring.infrastructure.configs are loaded.

  4. INFRASTRUCTURE beans are published as OSGi services.

  5. OSGi framework starts.

    1. Static bundles are installed and started.
    2. Dynamic bundles are started.
  6. OSGi framework starts the runtime.

  7. Spring Phase 2: MAIN

    1. Load Spring beans specified by the Spring context files listed in Portal property spring.configs.
    2. ModuleServiceLifecycle event service with a service property module.service.lifecycle value spring.initialized (i.e., SPRING_INITIALIZED) registers.
  8. MAIN Spring beans are published as OSGi services.

thumbnail
David Truong, modified 6 Years ago. Expert Posts: 322 Join Date: 3/24/05 Recent Posts
Here is a sample project that adds a portlet to a page: https://github.com/david-truong/joao
João Machado, modified 6 Years ago. New Member Posts: 4 Join Date: 10/31/18 Recent Posts

Thank you, David.

But i needed to create the layout for the newly created page also, so i can add pages with portlets without using the portal interface.

For that purpose i needed to have the ThemeDisplay available, how would i use this to be able to access it when loading the pre event?

 

I tried to use com.liferay.portal.instance.lifecycle, but on runtime i get an exception.

 

2018-11-08 11:44:24.433 ERROR [Framework Event Dispatcher: Equinox Container: 50154a96-4be3-0018-13d4-a86dbed45c57][com_startup:97] FrameworkEvent ERROR 
org.osgi.framework.BundleException: Could not resolve module: com.startup [1051]_  Unresolved requirement: Import-Package: com.liferay.portal.instance.lifecycle; version="[2.0.0,3.0.0)"_ [Sanitized]

 

On compilation, the jar is there as you can see, and it is both present on bnd.bnd and build.gradle.