Portlet can send dynamic content to client in serve resource phase.
Portlets are used to generate aggregate response. Each portlet will generate small part of whole page. So far we seen, render phase is used to generate the content of portlet. Render and action are two phases originally defined by portlet specification 1.0 (JSR-168).
But then later on, portlets are expected to serve content other than html. For example sending stream of bytes directly to client ( file download of image, pdf, xls, JSON, XML etc) or sending dynamic output without refreshing page(through ajax). There was no direct provision to serve resource dynamically in portlet specification 1.0 (JSR-168).
To over come this, portlet specification was updated (new portlet specification is called 2.0 or JSR-286) to include additional functionality. One of the main functionality added in portlet specification 2.0 (JSR-286) is resource serving capability.
Prior to JSR-286 specification, servlet inside portlet was used to server content dynamically.
Portlet Serve Resource phase / Lifecycle
In this article, we will talk about portlet serve resource phase / Lifecycle.
In serve resource phase, portlet can send dynamic content without calling render phase. Unlike action method, Liferay portal will not call render method after completion of serve resource phase. The requested resource will be sent directly to client in serve resource phase.
How to implement portlet serve resource phase ?
serveResource() method represents serve resource phase of portlet. serveResource() method is defined in ResourceServingPortlet interface. If we talk about liferay MVC portlet, it has following class hierarchy.
- GenericPortlet implements following 4 interfaces
- Portlet – This interface defines init, destroy, render and processAction methods which represent init, destroy, render and action phases respectively. It was introduced in JSR-168 (Portlet specificaiton 1.0)
- EventPortlet – This interface defines processEvent method. This method represent event phase of portlet and was added in JSR-286 (Portlet Specification 2.0)
- PortletConfig – This interface defines various methods to get portlet context, read portlet init parameters, get portlet name, get resource bundle etc.
- ResourceServingPortlet – This interface defines serveResource method. This method represent serve resource phase of portlet and was added in JSR-286 (Portlet Specification 2.0)
- GenericPortlet provides default implementation of these interfaces.
- serveResource() method is defined in ResourceServingPortlet interface.
- GenericPortlet provides default implementation of serveResource() method.
- LiferayPortlet extends GenericPortlet and overrides serveResource() method.
- MVCPortlet extends LiferayPortlet and overrides serveResource() method.
If you want to implement serve resource phase, you need to override serveResource() method in your custom portlet in case if you are using MVCPortlet as your parent portlet class.
How serveResource() method is called ?
serveResource() method is called when resource URL of that portlet is clicked. resource URL points to the portlet for which it’s created. It may points to same portlet or other based on the parameters set in resource URL.
Execution flow of serveResource method
below sequence diagram shows the execution flow of serveResource() method.
Flow of execution
- When user clicks on resource URL, it goes to Liferay portal.
- Portal will take this request and forward to portlet container.
- Portlet container will find the appropriate portlet and call it’s serveResource() method.
- Portlet container then pass the output of serveResource() method to Liferay portal and Liferay portal eventually pass that content directly to client.
- You can see that portlet container will not make call of any other phase after completion of serve resource phase( completion of serveResource() method )
- It’s more or less work just like a servlet which will send output directly to client.
- In case of render method, after completion of render phase, Liferay portal will aggregate the output of all portlets on page and send to client.
- But in case of serve resource phase, render phase is not called so Liferay portal will not generate portlet content. It simply send the output from serveResource() method to client. Because of this behavior, you need to always call resource URL in ajax.
- serve resource phase was introduced in portlet specification 2.0 (JSR-286).
- In serve resource phase, portlet can send dynamic content directly to client. It can be plain html, json, xml or stream of bytes i.e. sending a file.
- serveResource() method represent serve resource phase of portlet. You need to override this method and place your logic in it to serve dynamic content directly to client.
- After completion of serveResource method, Liferay portal will send the output of serveResource method directly to client. It will not create whole portal page content. Due to this behavior, you should call serveResource method in ajax.
- You can refer this link to know more about serve resource phase.