Portal events or sometimes referred as action in Liferay are of different types.
They executes on different time and a developer can choose relevant action to perform.
You can find these entries in portal.properties-
1) Servlet service event
The pre-service events process before Struts processes the request.
The post-service events process after Struts processes the request.
servlet.service.events.pre
servlet.service.events.post
2) Login Events process after successful login only.
login.events.pre
login.events.post
3) Logout events called after successful logout.
logout.events.pre
logout.events.post
In this post we will be setting landing page in Liferay 7 OSGI way with post login action.
But at the end you will explore that all of the actions can be implemented by a single change of property.
I will be adding class only in the post, you can choose build type from - Gradle/Maven
To add events you need to -
1) Implement com.liferay.portal.kernel.events.LifecycleAction
2) Add annotation
@Component(
immediate = true, property = {"key=login.events.pre"},
service = LifecycleAction.class
)
that's all, you need to do :)
Below is the code you can refer -
package com.sample; import javax.servlet.http.HttpSession; import org.osgi.service.component.annotations.Component; import com.liferay.portal.kernel.events.ActionException; import com.liferay.portal.kernel.events.LifecycleAction; import com.liferay.portal.kernel.events.LifecycleEvent; import com.liferay.portal.kernel.log.Log; import com.liferay.portal.kernel.log.LogFactoryUtil; import com.liferay.portal.kernel.struts.LastPath; import com.liferay.portal.kernel.util.StringPool; @Component( immediate = true, property = {"key=login.events.post"}, service = LifecycleAction.class ) public class LandingRedirectAction implements LifecycleAction { public static final Log LOGGER = LogFactoryUtil.getLog(LandingRedirectAction.class); @Override public void processLifecycleEvent(LifecycleEvent lifecycleEvent) throws ActionException { LOGGER.info("PostLogin called"); HttpSession session = lifecycleEvent.getRequest().getSession(); session.setAttribute("LAST_PATH", new LastPath(StringPool.BLANK, "/web/guest/landMeOnMoon")); } }
Now, in here if you want to change this action/event to be called on each page just change the property "key" to "servlet.service.events.pre" and add your code accordingly.
You are just done, Try & Enjoy the function.............:)