This section briefly covers how to get your view layer working, from organizing your imports in one JSP file, to creating URLs that direct processing to methods in your portlet class.
Using the init.jsp
Liferay’s practice puts all Java imports, tag library declarations, and
variable initializations into a JSP called init.jsp
. If you use
Blade CLI or
Liferay Dev Studio DXP
to create a module based on the mvc-portlet
project template, these taglib
declarations and initializations are added automatically to your init.jsp
:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%@ taglib uri="http://liferay.com/tld/aui" prefix="aui" %>
<%@ taglib uri="http://liferay.com/tld/portlet" prefix="liferay-portlet" %>
<%@ taglib uri="http://liferay.com/tld/theme" prefix="liferay-theme" %>
<%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui" %>
<liferay-theme:defineObjects />
<portlet:defineObjects />
Here are the tag libraries it gives you:
c
: JSTL core tags.portlet
: Standard portlet component tags.aui
: AlloyUI component tags.liferay-portlet
: Liferay portlet component tags.liferay-theme
: Liferay theme component tags.liferay-ui
: Liferay UI component tags.
These tags make portlet and Liferay objects available:
-
<portlet:defineObjects />
: Implicit Java variables that reference Portlet API objects. The objects available are limited to those available in the current portlet request. For details, see thedefineObjects
tag in JSR-286. -
<liferay-theme:defineObjects />
: Implicit Java variables that reference Liferay objects.
To use all that the init.jsp
has, include it in your other JSPs:
<%@include file="/html/init.jsp"%>
A JSP uses render URLs to display other pages and action URLs to invoke controller methods.
Using Render URLs
A render URL attached to a UI component action displays another page. For
example, this render URL displays the JSP /path/to/foo.jsp
.
<portlet:renderURL var="adminURL">
<portlet:param name="mvcPath" value="/path/to/foo.jsp" />
</portlet:renderURL>
Here’s how to use a render URL:
-
Add a
<portlet:renderURL>
to your JSP. -
Name the render URL via a
var
attribute in the<portlet:renderURL>
tag. The<portlet:renderURL>
tag constructs the URL and assigns it to the variable. For example, this render URL is assigned to the variable namedadminURL
:<portlet:renderURL var="adminURL"> ... </portlet:renderURL>
-
As sub-element to the
<portlet:renderURL>
tag, add a<portlet:param>
tag with the following attributes:name="mvcPath"
: Your controller’srender
method forwards processing to the JSP at the path specified in thevalue
.value="/path/to/foo.jsp"
: The path to the JSP to render. Replace the value/path/to/foo.jsp
with your JSP path.<portlet:renderURL var="adminURL"> <portlet:param name="mvcPath" value="/path/to/foo.jsp" /> </portlet:renderURL>
-
To invoke the render URL, assign its variable (
var
) to a UI component action, such as a button or navigation bar item action.
Invoking the UI component causes the controller’s render method to display the
mvcPath
parameter’s JSP.
Using Action URLs
Action methods are different because they invoke an action (i.e., code), rather
than link to another page. For example, this action URL invokes a
controller method called doSomething
and passes a parameter called redirect
.
The redirect
parameter contains the path of the JSP to render after invoking
the action:
<portlet:actionURL name="doSomething" var="doSomethingURL">
<portlet:param name="redirect" value="<%= redirect %>" />
</portlet:actionURL>
Here’s how to use an action URL:
-
Add a
<portlet:actionURL>
to your JSP. -
Add a
name
andvar
attribute to the<portlet:actionURL>
. The<portlet:actionURL>
tag constructs the URL and assigns it to thevar
variable.name
: Controller action to invoke.var
: Variable to assign the action URL to.<portlet:actionURL name="doSomething" var="doSomethingURL"> ... </portlet:actionURL>
-
As sub-element to the
<portlet:actionURL>
tag, add a<portlet:param>
tag that has the following attributes:name="redirect"
: Tells the portlet to redirect to the JSP associated with this parameter.value="/path/to/foo.jsp"
: Redirects the user to this JSP path after invoking the action. Replace the value/path/to/bar.jsp
with your JSP path.<portlet:actionURL name="doSomething" var="doSomethingURL"> <portlet:param name="redirect" value="/path/to/bar.jsp" /> </portlet:actionURL>
-
To invoke the action URL, assign its variable (
var
) to a UI component action, such as a button or navigation bar item action.
Congratulations! Your portlet is ready for action.
These simple examples demonstrate how Liferay MVC Portlet facilitates communication between a smaller application’s view layer and controller.