concept portlet in category liferay

This is an excerpt from Manning's book Liferay in Action: The Official Guide to Liferay Portal Development.
Other portlets in Liferay are instanceable. This means you can place as many of them as you like on the same pages in any community or organization, and by default they all have their own configurations. For example, the RSS portlet is designed to show RSS feeds. You can add as many RSS portlets as you want to any page and configure each portlet to display different feeds, because this portlet is instanceable. The Web Content Display portlet is the same way: you can place as many Web Content Display portlets on a page as you wish, and each portlet can display a different piece of web content. When you choose portlets from Liferay’s Add > More window, the interface indicates which portlets are instanceable and which are non-instanceable, as shown in figure 1.13.
Figure 1.13. Instanceable and non-instanceable portlets in Liferay’s Add window. Liferay’s UI clearly shows you which portlets can be added multiple times to the same page (instanceable) and which can’t (non-instanceable).
![]()
You can tell which portlet is which in the user interface by looking at the icons in the Add > More window. If there’s a green icon with two windows, the portlet is instanceable. If there’s a purple icon with one window, the portlet is non-instanceable.
To deploy the portlet, run the deploy Ant task. This can be done from the command line (on any OS) by entering the project folder and typing the following command:
If Liferay is running, it will automatically deploy the portlet. If Liferay isn’t yet running, start it; when it has finished starting, the portlet will be deployed.
To test the portlets, log into Liferay with the default administrative credentials:
Figure C.3. Portlet events are fired by the portlet that is configured to send the event. Only the portlets that are configured to listen for the event receive it. In this example, portlet 2 and portlet 3 would receive the event, but portlet 4 wouldn’t.
![]()
You made CatcherPortlet work in a meaningful manner by providing a default message in case there was no event. This is the kind of thing you should do whenever you’re writing portlets based on events.
Events are like broadcasts. They go out, but they’re only heard if another portlet is listening (again, see figure C.3). The portlet spec says sure, you can use the setEvent() method programmatically without declaring the event in the deployment descriptor. But the individual portlet container may decide not to deliver the event—this is left up to the individual portal vendor to decide. In Liferay’s case, Liferay will deliver all events that are listened for. You definitely have to declare ahead of time that a portlet is listening for an event, but you don’t necessarily have to declare that a portlet is going to send a particular event. It’s still a good practice to declare what events your portlets will send, as it prevents someone else who might have to maintain your code from having to search the code for events. If they’re declared, all that person has to do is look at your portlet.xml file.
The spec also says that portlet events can be heard by portlets that aren’t on the same page as the portlet that sent the event. Liferay implements this in a couple of different ways. Remember that portal-ext.properties file you used in chapter 2 to connect Liferay to a database? There’s a property you can use in that file to define how you want Liferay to handle events:
You can set this property to either layout (the default) or to layout-set. If set to layout, portlets have to be on the same page as the sender in order to receive any of its events. If it’s set to layout-set, portlets in the same group of pages—either the public or private pages of a community or organization—will be able to receive events sent by any portlet within the same layout set.
Events can also be sent between portlets residing in different .war files. This is one of the features of the design. Just because you put both portlets in one project for this example doesn’t mean you have to. In fact, if you wanted, you could turn Hello You into a portlet that responded to the Pitch event in some way (maybe by yelling, “He’s no batter! Swing!”).