How to replace a function with a native Liferay portlet

I am making changes to my own Liferay portlet, which comes with Liferay Intallation. How to change a function to my own implementation using a hook or similar approach?

I read how to make pre and post conditions and how to make a new implementation of the interface, but I donโ€™t know how to simply replace the random function inside the random class in the portlet that I want to save otherwise, since this is the original.

+7
source share
1 answer

There are several ways to change the functionality of your own Liferay portlet. Here is a quick overview.

Most binding functions are described through the liferay-hook.xml file located in the / docroot / WEB -INF directory. Here is the most common method.

Custom jsp

In liferay-hook.xml, add the following child to <hook/>

 <custom-jsp-dir>/META-INF/custom_jsps</custom-jsp-dir> 

This element defines the location of the JSP overwriting location. For example, you can rewrite view.jsp for a document library portlet at:

 [custom-jsp-dir]/html/portlet/document_library/view.jsp 

Model listeners

To do this, you will need to define the portal.property file, which is usually stored in

 /docroot/WEB-INF/src/portal.property 

And tell liferay-hook.xml its location. Below is an example of the above,

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

if you want to listen to changes in User, for example, you should write in the property

 value.object.listener.com.liferay.portal.model.User=com.my.example.UserListener; 

What is in the following format,

 value.object.listener.[class-to-listen]=[my-listener-class] 

And your class should implement com.liferay.portal.model.BaseModelListener .

Here you can listen to events such as Add, Update, Delete, and several others.

Extend \ Overwrite Service

A similar story is here in liferay-hook.xml in the <hook /> add element

 <service> <service-type>com.liferay.portal.service.UserService</service-type> <service-impl>my.example.service.UserServiceImpl</service-impl> </service> 

Here, your implementation should extend the correct wrapper class for a particular service. For example, for the example above,

 com.liferay.portal.service.UserServiceWrapper; 

Now you can overwrite all public UserService methods, for example updateUser(..) .

Customizing Struts Actions

(available for Liferay 6.1 only)

Similarly, as extension services, define elements for <hook />

 <struts-action> <struts-action-path>/message_boards/view</struts-action-path> <struts-action-impl>my.example.action.SampleViewAction</struts-action-impl> </struts-action> 

You need to expand

 com.liferay.portal.kernel.struts.BaseStrutsAction 

and you will get access to the request and be able to perform a custom action. It is very powerful when combined with custom JSPs.

Good luck

Be sure to check compatibility with the version of Liferay that you are using.

If you need even more control, you will need to use an ext-plugin.

+15
source

All Articles