Some Liferay DXP core content, such as tag library tags, can only be overridden
using JSPs ending in .readme
. The suffix .readme
facilitates finding them.
The code from these JSPs is now inlined (brought into Liferay DXP Java source
files) to improve performance. Liferay DXP ignores JSP files with the .readme
suffix. If you add code to a JSP .readme
file and remove the .readme
suffix,
Liferay DXP uses that JSP instead of the core inline content. This tutorial shows
you how to make these customizations.
Here’s how to override inline content using JSPs:
-
Create a Custom JSP Bag for deploying your JSP. Note the module folder you’re storing the JSPs in: the default folder is
[your module]/src/main/resources/META-INF/jsps/
-
Download the Liferay DXP source code or browse the source code on GitHub (Liferay Portal CE).
-
Search the source code for a
.jsp.readme
file that overrides the tag you’re customizing. -
Copy the
.jsp.readme
file into your project and drop the.readme
suffix. Use the same relative file path Liferay DXP uses for the.jsp.readme
file. For example, if the file in Liferay DXP isportal-web/docroot/html/taglib/aui/fieldset/start.jsp.readme
use file path
[your module]/src/main/resources/META-INF/jsps/html/taglib/aui/fieldset/start.jsp
-
Familiarize yourself with the current UI content and logic, so you can override it appropriately. Tag library tag content logic, for example, is in the respective
*Tag.java
file underutil-taglib/src/com/liferay/taglib/[tag library]/
. -
Develop your new logic, keeping in mind the current inline logic you’re replacing.
-
Deploy your JSP.
Liferay DXP uses your JSP in place of the current inline logic. If you want
to walk through an example override, continue with this tutorial. Otherwise,
congratulations on a modified .jsp.readme
file to override core inline
content!
Example: Overriding the fieldset Taglib Tag
This example demonstrates changing the liferay:aui
tag library’s fieldset
tag. Browsing the Liferay DXP web application or the source code at
portal-web/docroot/html/taglib/aui/fieldset
reveals these files:
start.jsp.readme
end.jsp.readme
They can override the logic that creates the start and end of the fieldset
tag. The FieldsetTag.java
class’s processStart
and processEnd
methods
implement the current inline content. Here’s the
processStart
method:
@Override
protected int processStartTag() throws Exception {
JspWriter jspWriter = pageContext.getOut();
jspWriter.write("<fieldset class=\"fieldset ");
jspWriter.write(GetterUtil.getString(getCssClass()));
jspWriter.write("\" ");
String id = getId();
if (id != null) {
jspWriter.write("id=\"");
jspWriter.write(id);
jspWriter.write("\" ");
}
jspWriter.write(
InlineUtil.buildDynamicAttributes(getDynamicAttributes()));
jspWriter.write(StringPool.GREATER_THAN);
String lable = getLabel();
if (lable != null) {
jspWriter.write(
"<legend class=\"fieldset-legend\"><span class=\"legend\">");
MessageTag messageTag = new MessageTag();
messageTag.setKey(lable);
messageTag.setLocalizeKey(getLocalizeLabel());
messageTag.doTag(pageContext);
String helpMessage = getHelpMessage();
if (helpMessage != null) {
IconHelpTag iconHelpTag = new IconHelpTag();
iconHelpTag.setMessage(helpMessage);
iconHelpTag.doTag(pageContext);
}
jspWriter.write("</span></legend>");
}
if (getColumn()) {
jspWriter.write("<div class=\"row\">");
}
else {
jspWriter.write("<div class=\"\">");
}
return EVAL_BODY_INCLUDE;
}
The code above does this:
-
Write
<fieldset class=\"fieldset
starting tag. -
Write the CSS class name attribute.
-
If the tag has an ID, add the
id
as an attribute. -
Write the tag’s dynamic attribute (map).
-
Close the starting
fieldset
tag. -
Get the tag’s
label
attribute. -
Write the starting
legend
element. -
Use
getLocalizeLabel()
to add the localized label in thelegend
. -
If there’s a help message (retrieved from
getHelpMessage()
), write it in anicon-help-tag
. -
Write the closing
legend
tag. -
If there’s a column attribute, write
<div class=\"row\">
; else write<div class=\"\">
.
Replicating the current logic in your custom JSP helps you set up the tag
properly for customizing. The init.jsp
for fieldset
initializes all the
variables required to create the starting tag. You can use the variables in the
start.jsp
. The logic from FieldsetTag
’s processStart
method converted to
JSP code for start.jsp
(renamed from start.jsp.readme
) would look like this:
<%@ include file="/html/taglib/aui/fieldset/init.jsp" %>
<fieldset class="fieldset <%= cssClass %>" <%= Validator.isNotNull(id) ? "id=\"" + id + "\"" : StringPool.BLANK %> <%= InlineUtil.buildDynamicAttributes(dynamicAttributes) %>>
<c:if test="<%= Validator.isNotNull(label) %>">
<legend class="fieldset-legend">
<span class="legend">
<liferay-ui:message key="<%= label %>" localizeKey="<%= localizeLabel %>" />
<c:if test="<%= Validator.isNotNull(helpMessage) %>">
<liferay-ui:icon-help message="<%= helpMessage %>" />
</c:if>
</span>
</legend>
</c:if>
<div class="<%= column ? "row" : StringPool.BLANK %>">
On deploying the start.jsp
, the fieldset
tags render the same as they did
before. This is expected because it uses the same logic as FieldsetTag
’s
processStart
method.
The fieldset
starting logic is ready for customization. To test that this
works, you’ll print the word test surrounded by asterisks before the end of
the fieldset
tag’s starting logic. Insert this line before the start.jsp
’s
last div
tag:
<c:out value="**********test**********"/>
Redeploy the JSP and refresh the page to see the text printed above the
fieldset
’s fields.
You know how to override specific Liferay DXP core inline content using Liferay’s
.jsp.readme
files.