Skip to content
OpenCms documentation
OpenCms documentation

Besides splitting large JSP code blocks into smaller parts by means of the <cms:include> tag, there is the possibility of encapsulating code redundancies into custom reusable JSP tag files.

If there appear many <cms:container> tags in a template JSP, for example, it may be useful to define all the container related markup in a JSP tag file and then call the <example:container> tag in the template JSP.

JSP

<%@page pageEncoding="UTF-8" buffer="none" session="false"%>
<%@ taglib prefix="example" tagdir="/WEB-INF/tags/example" %>
<example:container name="main" type="element"/>

Tag file

<%@tag pageEncoding="UTF-8"
    display-name="container"
    body-content="empty"
    trimDirectiveWhitespaces="true"
    description="Generates a container." %>

<%@attribute name="name" type="java.lang.String" required="true"
        description="A unique name for the container." %>

<%@attribute name="type" type="java.lang.String" required="true"
        description="The type assigned to the container, e.g. 'element',
            used to define which content formatters may occupy the container." %>

<%@ taglib prefix="cms" uri="http://www.opencms.org/taglib/cms"%>

<cms:container name="${name}" type="${type}" maxElements="100">
  <div class="notification is-info is-light">
    <h3>Container <small>for ${type}</small></h3>
    <p>Drag and drop new elements here.</p>
  </div>
</cms:container>

JSP tag files by convention are stored with the *.tag suffix in a module's tags/ directory, e.g., at /system/modules/org.opencms.examples/tags/container.tag. In order to make the tag files of a module work, a module's tags/ folder has to be exported to a subfolder of Tomcat's WEB-INF/tags/ directory on the server's file system. Use the Modules app to configure a new export folder like this:

  • Source: /system/modules/org.opencms.examples/tags/
  • Target: WEB-INF/tags/examples/
Make sure to use the resource type JSP and not Plain text when creating a new tag file. Changes to tag files are available only after publication since only the publication causes the tag file to be exported to Tomcat's WEB-INF/tags/ directory.