4

I have developed a portlet in liferay I have a table(entity) that I want to fill it with data when portlet is deploying. How can I call a method of a class during deploy operation?

Obtice
  • 1,205
  • 3
  • 18
  • 44
  • see http://stackoverflow.com/questions/10353881/overriding-liferay-startup-events/10356947 – Mark Jan 20 '14 at 10:17

3 Answers3

6

Finally I solved it.

I have to create my action class somewhere in src folder.

package com.example.portal.events;

import java.util.Arrays;

import com.liferay.portal.kernel.events.SimpleAction;

public class ExampleStartupAction extends SimpleAction {

    public void run(String[] ids) {

    System.out.println("############################ This is a Startup Action ##########################"+ ids.length+" "+Arrays.toString(ids));

    }

}

Then you have to create a file named portal.properties inside src folder in WEB-INF and add this line to it:

application.startup.events=com.example.portal.events.ExampleStartupAction

Finally you must edit liferay-hook.xml file and add this line above :

<portal-properties>portal.properties</portal-properties>

method run, will run during deply of portlet.

Mark
  • 17,887
  • 13
  • 66
  • 93
Obtice
  • 1,205
  • 3
  • 18
  • 44
1

You could also override and use the init() method of your portlet class.

-1

By using servlet context initializer you can perform actions during context initialization and destruction. In the web.xml add this..

    <?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>


    <listener>
        <listener-class>MyListenerClass</listener-class>
    </listener>

</web-app>

and Implement the listener class

   import javax.servlet.*;
    import javax.servlet.http.*;

    public class MyListenerClass implements ServletContextListener {

      public void contextInitialized(ServletContextEvent e) {
...
}

public void contextDestroyed(ServletContextEvent e) {
...
}
}
Sumukh
  • 660
  • 7
  • 11
  • Listening to the initialization of your web application doesn't mean that your portlet was deployed successfully. Just imagine that the web application of Liferay is initialized after your web application. In that case you will have no access to any Liferay or portlet specific functionality in your listener. – Tobias Liefke Aug 31 '15 at 07:57
  • IMHO, The question is not to access any portlet specific functionality here. So the order of initialization does not matter and this solution fits the question asked – Sumukh Sep 18 '15 at 07:02
  • He has asked for _when portlet is deploying_. And even if he asks for a web application I would prefer the modern `@WebListener` instead. – Tobias Liefke Sep 18 '15 at 09:47