Customizing JSPs with Dynamic Includes

The liferay-util:dynamic-include tag is an extension point for inserting content (e.g., JavaScript code, HTML, and more). To do this, create a module that has content you want to insert, register that content with the dynamic include tag, and deploy your module.

We’ll demonstrate how dynamic includes work using the Blogs entries. For reference, you can download the example module.

  1. Find the liferay-util:dynamic-include tag where you want to insert content and note the tag’s key.

    The Blogs app’s view_entry.jsp has a dynamic include tag at the top and another at the very bottom.

    <%@ include file="/blogs/init.jsp" %>
    
    <liferay-util:dynamic-include key="com.liferay.blogs.web#/blogs/view_entry.jsp#pre" />
    
        ... JSP content is here
    
    <liferay-util:dynamic-include key="com.liferay.blogs.web#/blogs/view_entry.jsp#post" />
    

    Here are the Blogs view entry dynamic include keys:

    • key="com.liferay.blogs.web#/blogs/view_entry.jsp#pre"
    • key="com.liferay.blogs.web#/blogs/view_entry.jsp#post"
  2. Create a module (e.g., blade create my-dynamic-include). The module will hold your dynamic include implementation.

  3. Specify compile-only dependencies, like these Gradle dependencies, in your module build file:

    dependencies {
    	compileOnly group: "javax.portlet", name: "portlet-api", version: "2.0"
    	compileOnly group: "javax.servlet", name: "javax.servlet-api", version: "3.0.1"
    	compileOnly group: "com.liferay", name: "com.liferay.petra.string", version: "1.0.0"
    	compileOnly group: "com.liferay.portal", name: "com.liferay.portal.kernel", version: "2.0.0"
    	compileOnly group: "org.osgi", name: "osgi.cmpn", version: "6.0.0"
    }
    
  4. Create an OSGi component class that implements the DynamicInclude interface.

    Here’s an example dynamic include implementation for Blogs:

    import java.io.IOException;
    import java.io.PrintWriter;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.osgi.service.component.annotations.Component;
    
    import com.liferay.portal.kernel.servlet.taglib.DynamicInclude;
    
    @Component(
    	immediate = true,
    	service = DynamicInclude.class
    )
    public class BlogsDynamicInclude implements DynamicInclude {
    
    	@Override
    	public void include(
    			HttpServletRequest request, HttpServletResponse response,
    			String key)
    		throws IOException {
    
    		PrintWriter printWriter = response.getWriter();
    
    		printWriter.println(
    			"<h2>Added by Blogs Dynamic Include!</h2><br />");
    	}
    
    	@Override
    	public void register(DynamicIncludeRegistry dynamicIncludeRegistry) {
    		dynamicIncludeRegistry.register(
    			"com.liferay.blogs.web#/blogs/view_entry.jsp#pre");
    	}
    
    }
    

    Giving the class a @Component annotation that has the service attribute service = DynamicInclude.class makes the class a DynamicInclude service component.

    @Component(
        immediate = true,
        service = DynamicInclude.class
    )
    

    In the include method, add your content. The example include method writes a heading.

    @Override
    public void include(
            HttpServletRequest request, HttpServletResponse response,
            String key)
        throws IOException {
    
        PrintWriter printWriter = response.getWriter();
    
        printWriter.println(
        "<h2>Added by Blogs Dynamic Include!</h2><br />");
    }
    

    In the register method, specify the dynamic include tag you want to use. The example register method targets the dynamic include at the top of the Blogs view_entry.jsp.

    @Override
    public void register(DynamicIncludeRegistry dynamicIncludeRegistry) {
    	dynamicIncludeRegistry.register(
    		"com.liferay.blogs.web#/blogs/view_entry.jsp#pre");
    }
    

Once you’ve deployed your module, the overridden JSP dynamically includes your content. Congratulations on injecting dynamic content into a JSP!

« Customizing JSPsJSP Overrides Using Portlet Filters »
Este artigo foi útil?
Utilizadores que acharam útil: 0 de 0