MVCRenderCommand
s are classes that respond to
MVCPortlet render
URLs.
If your render logic is simple and you want to implement all of your render
logic in your portlet class, see Writing MVC Portlet Controller
Code. If your render
logic is complex or you want clean separation between render paths, use
MVCRenderCommand
s. Each render URL in your portlet’s JSPs invokes an
appropriate render command class.
Here are the steps:
-
Configure your JSPs to generate render URLs via
<portlet:renderURL>
tags.For example, this render-command-portlet sample render URL invokes an MVC render command named
/blade/render
.<portlet:renderURL var="bladeRender"> <portlet:param name="mvcRenderCommandName" value="/blade/render" /> </portlet:renderURL>
-
Name the render URL via its
<portlet:param>
namedmvcRenderCommandName
. The render URL and*MVCRenderCommand
class (demonstrated later) map to themvcRenderCommandName
value. -
Assign the
<portlet:renderURL>
’svar
attribute a variable name to pass to a UI component. -
Assign the render URL variable (
var
) to a UI component. When the user triggers the UI component, the*MVCRenderCommand
class that matches the render URL handles the render request.For example, the render URL with the variable
bladeRender
triggers on users clicking this button.<aui:button href="<%= bladeRender %>" value="goto page render" />
-
Create a class that implements the
MVCRenderCommand
interface. -
Annotate the class with an
@Component
annotation, like this one:@Component( property = { "javax.portlet.name=com_liferay_blade_samples_portlet_rendercommand_BladeRenderPortlet", "mvc.command.name=/blade/render" }, service = MVCRenderCommand.class )
-
Set a
javax.portlet.name
property to your portlet’s internal ID. -
Set a
mvc.command.name
property to your<portlet:renderURL>
tagmvcRenderCommandName
portlet parameter value. This maps your class to the render URL. -
Register your class as an
MVCRenderCommand
service by setting theservice
attribute toMVCRenderCommand.class
.Note, you can apply MVC Command classes to multiple portlets by setting a
javax.portlet.name
property for each portlet and apply MVC Command classes to multiple command names by setting anmvc.command.name
property for each command name. For example, this component’sjavax.portlet.name
properties andmvc.command.name
properties apply it to two specific portlets and two specific command names.@Component( immediate = true, property = { "javax.portlet.name=" + HelloWorldPortletKeys.HELLO_MY_WORLD, "javax.portlet.name=" + HelloWorldPortletKeys.HELLO_WORLD, "mvc.command.name=/hello/edit_super_entry", "mvc.command.name=/hello/edit_entry" }, service = MVCRenderCommand.class )
-
Implement your render logic in a method that overrides
MVCRenderCommand
’srender
method. Some*MVCRenderCommand
s, such as the one below, always render the same JSP.public class BlogsViewMVCRenderCommand implements MVCRenderCommand { @Override public String render( RenderRequest renderRequest, RenderResponse renderResponse) { return "/blogs/view.jsp"; } }
As you can see, MVC render commands are easy to implement and can respond to multiple command names for multiple portlets.