When using Liferay’s MVCPortlet
framework, you can
create resource URLs in your JSPs to retrieve images, XML, or any other kind of
resource from a Liferay DXP instance. The resource URL then invokes the
corresponding MVC resource command class (*MVCResourceCommand
) that processes
the resource request and response.
Here how to create your own MVC Resource Command:
-
Configure your JSPs to generate resource URLs via
<portlet:resourceURL>
tags.For example, this resource-command-portlet sample resource URL invokes an MVC resource command named
/blade/captcha
.<portlet:resourceURL id="/blade/captcha" var="captchaURL" />
-
Name the resource URL via its
id
attribute. -
Assign the resource URL’s
var
attribute a variable name to pass to a UI component. -
Assign the resource URL variable (
var
) to a UI component, such as a button or icon. When the user triggers the UI component, the*MVCResourceCommand
class that matches the resource URL handles the resource request.For example, the sample’s resource URL is triggered when the user clicks on this
liferay-captcha
component:<liferay-captcha:captcha url="<%= captchaURL %>" />
-
Create a class that implements the
MVCResourceCommand
interface, or that extends theBaseMVCResourceCommand
class. The latter may save you time, since it already implementsMVCResourceCommand
. -
Annotate your class with an
@Component
annotation, like this one:@Component( property = { "javax.portlet.name=your_portlet_name_YourPortlet", "mvc.command.name=/your/jsp/resource/url" }, service = MVCResourceCommand.class ) public class YourMVCResourceCommand extends BaseMVCResourceCommand { // your resource handling code }
-
Set a
javax.portlet.name
property to your portlet’s internal ID. -
Set the
mvc.command.name
property to your<portlet:resourceURL>
tag’sid
. This maps your class to the resource URL of the same name. -
Register your class as an
MVCResourceCommand
service by setting theservice
attribute toMVCResourceCommand.class
.
-
-
Implement your resource logic by overriding the appropriate method of the class you’re implementing or extending.
-
MVCResourceCommand
implementations override theserveResource
method. -
BaseMVCResourceCommand
extensions override thedoServeResource
method.
-
For example, the
resource-command-portlet’s
CaptchaMVCResourceCommand
class implements the MVCResourceCommand
interface
with only a single method: serveResource
.
@Component(
immediate = true,
property = {
"javax.portlet.name=com_liferay_blade_samples_portlet_resourcecommand_CaptchaPortlet",
"mvc.command.name=/blade/captcha"
},
service = MVCResourceCommand.class
)
public class CaptchaMVCResourceCommand implements MVCResourceCommand {
@Override
public boolean serveResource(
ResourceRequest resourceRequest, ResourceResponse resourceResponse)
throws PortletException {
if (_log.isInfoEnabled()) {
_log.info("get captcha resource ");
}
try {
CaptchaUtil.serveImage(resourceRequest, resourceResponse);
return false;
}
catch (Exception e) {
_log.error(e.getMessage(), e);
return true;
}
}
private static final Log _log = LogFactoryUtil.getLog(
CaptchaMVCResourceCommand.class);
}
This serveResource
method processes the resource request and response via the
javax.portlet.ResourceRequest
and javax.portlet.ResourceResponse
parameters,
respectively. Note that the try
block uses the helper class
CaptchaUtil
to serve the CAPTCHA image. Though you don’t have to create such a helper class,
doing so often simplifies your code.
Great! Now you know how to use MVCResourceCommand
to process resources in your
Liferay MVC Portlets.