資料 リソース
Liferayは、コミュニティにてテクノロジーをより良く使うために役立つ豊富なリソースと知識を提供しています。
Invoking the API locally
Each service provides a local interface to clients running in the same JVM as the portal. There are two ways to invoke the methods of a service API:
By using Spring injection, if your app is using Spring and has access to the portal context.
By using
-ServiceUtilclasses. These classes hide complexity of the service implementations and may be a good option if you are not familiar with Spring.
We’ll demonstrate invoking a service via its -ServiceUtil. But first, how do we find services? … By looking them up in the Liferay Portal Javadocs.
For example, here is how you look up the Organization services:
In your browser, open up the Javadocs at http://docs.liferay.com/portal/6.1/javadocs/.
Click on the link for the
com.liferay.portal.servicepackage in the Packages frame, since the services for the Organization entity belong to the portal.Find and click on the
-ServiceUtilclass (in this caseOrganizationLocalServiceUtil) in the class summary table or the list of classes.
It’s just that easy!
Similarly, if you want to search for one of Liferay’s built-in portlet services, no problem. But, when looking up the package, instead of clicking on the link for the service package of the portal, click on the link for the service package of the portlet. The portlet service packages use the naming convention com.liferay.portlet.[portlet-name].service, where [portlet-name] is replaced with the actual name of the portlet.
For example, here is how you look up services for user blogs statistics:
In your browser, open up the Javadocs at http://docs.liferay.com/portal/6.1/javadocs/.
Click on the link for the
com.liferay.portlet.blogs.servicepackage in the Packages frame, since the services are a part of the blogs portlet.Find and click on the
-ServiceUtilclass (in this caseBlogsStatsUserLocalServiceUtil) in the class summary table or the list of classes.
So, now that you know how to look up the service classes, let’s look at the following JSP code snippet that demonstrates how to get a list of the most recent bloggers from an organization.
<%@ page import="com.liferay.portlet.blogs.service.BlogsStatsUserLocalServiceUtil" %>
<%@ page import="com.liferay.portlet.blogs.util.comparator.StatsUserLastPostDateComparator" %>
...
<%@
List statsUsers = BlogsStatsUserLocalServiceUtil.getOrganizationStatsUsers(
organizationId, 0, max, new StatsUserLastPostDateComparator());
%>
This JSP code invokes static method getOrganizationStatsUsers() on the -LocalServiceUtil class BlogsStatsUserLocalServiceUtil.
Note: Invoking the services in this way avoids permission checks. So, if you want to ensure permission checks are performed, even from a local context, then you should use the remote variant of the API.
We’ll look at invoking services remotely, next.