Tips & Tricks: include jsp into vm template

This tip&trick is dedicated to all developers who work with themes and vm’s. Probably you know that you have only little set of services from VelocityVariables class and some variables from init.vm. If you want call normal service method this is a huge problem. My question was: how can I include a jps file to vm file? Liferay has a lot of scriptlets and calls services from jsp. Why I can’t call it from VM? Many of you tell me: “I know how can you do it. Put this line of code and it’s ready”

#set ($categoriesService = $portal.getClass()
.forName("com.liferay.portlet.asset.service.AssetCategoryLocalServiceUtil")
.getMethod("getService", null).invoke(null, null))

My answer is: Yessss, it works. But I want use a jstl lib (e.g. <aui> or <ui>) as well. In my opinion the simpliest way to do it is include jsp file to themeServletContext. Put your file.jsp into your theme:
your-theme/docroot/path/to/your/jsp/file.jsp

and paste this piece of code into your vm (for example portal_normal.vm):

$theme.include($themeServletContext, "/path/to/your/jsp/file.jsp")

And enjoy all Liferay’s functionalities!

Advertisement

4 Responses to Tips & Tricks: include jsp into vm template

  1. Pingback: How to include JSP in your Liferay theme « mygeekjourney

  2. Pingback: How to include JSP in your Liferay theme | My Geek Journey

  3. lrkwz says:

    When I use

    $theme.include($themeServletContext, ‘/html/portlet/login/login.jsp’)

    the log states

    15:33:23,146 ERROR [http-bio-80-exec-210][LiferayMethodExceptionEventHandler:34] javax.servlet.ServletException: File "/html/portlet/login/login.jsp" not found
    javax.servlet.ServletException: File "/html/portlet/login/login.jsp" not found

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: