Obtice
Obtice

Reputation: 1269

How to define an action runs on deploy in liferay portlet?

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?

Upvotes: 4

Views: 2235

Answers (3)

Sumukh
Sumukh

Reputation: 749

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) {
...
}
}

Upvotes: -1

user1357289
user1357289

Reputation:

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

Upvotes: 1

Obtice
Obtice

Reputation: 1269

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.

Upvotes: 6

Related Questions