Tuesday 30 August 2016

How to create Portlet Filters in Liferay 7 DXP

How to write a Portlet Filter in Liferay 7 DXP?
In this tutorial we are going to see how we can write a portlet filter in liferay 7. Portlet Filter can generally be implemented in a class using any of the portlet. In short you can write a portlet filter for any portlet in any portlet project. It is not necessary that the portlet filter will needs to be implemented in its own portlet project.
To write a portlet filter we only need to know the name of the portlet that we need to write the filter for.
The portlet filters can be of following type -:
  • Render Filter
  • Resource Filter
  • Action Filter
So whenever any of the above type of Url is called from the controller one of the above filter is called depending upon the type of the filter. i.e if a URL of action type is called the Action Filter will be called and if Resource URL is called then the Resource filter will be called.
The Render Filter will implement the javax.portlet.filter.RenderFilter class and will implement the doFilter(RenderRequest request, RenderResponse response, FilterChain chain) method.
The ActionFilter will implement the javax.portlet.filter. ActionFilter class and will implement the doFilter(ActionRequest request, ActionResponse response, FilterChain chain) method.
The ResourceFilter will implement the javax.portlet.filter. ResourceFilter class and will implement the doFilter(ResourceRequest request, ResourceResponse response, FilterChain chain) method.


So the first step will be to define the class as a component whose service class will be PortetFilter.class, Next we need to provide the name of the portlet that can be done in the property. Define the name and component in the following manner.


@Component(
immediate = true,
property = {
"javax.portlet.name=<NAME_OF_THE_PORTLET>",
},
service = PortletFilter.class
)


So the complete implementation for the Render Method could be as follows.

And once you drag the portlet on to a page the following can be seen in the console log.





The code for action portlet filter will be


j\

2 comments:

  1. If we want to add multiple portlets for this filter can we add the portlet's name as comma-separated way in "javax.portlet.name=com_liferayrevisited_portet_FilterPortlet,another-portlet,more-portletname", }

    ReplyDelete
  2. How to execute a actionfilter for particular mvcactioncommad?

    ReplyDelete

 

Copyright @ 2016 LiferayRevisited.