rendering Liferay page URLs inside of portlets Liferay 6.1

岁酱吖の 提交于 2019-12-03 16:39:19
Prakash K

I would use velocity markup here but I don't think (don't know for sure) if that is allowed inside of a jsp.

Why would you want to use Velocity mark-up inside a JSP (view.jsp)? I don't see any advantages in doing that apart from the argument that you are really great at velocity.

Though here is a link that would help you embed velocity inside of JSP.

Note: In my opinion it is not a good practice to embed velocity within JSP in a portlet

In JSP:

In VM (these would be *.vm files in themes):
You can follow all the same steps as mentioned in JSP. The things you would need to do that are:

  • Instance of LayoutLocalService, can be found out by using the following code (taken from this answer):

    #set($layoutLocalService = $serviceLocator.findService("com.liferay.portal.service.LayoutLocalService"))
    

    now you can use the velocity variable $layoutLocalService to make calls to service methods for getting the layouts.

  • Then you can call methods of PortalUtil class by using the variable $portalUtil available for *.vm files in themes.

You can check-out the following files for more details (if you are interested):

  1. docroot/html/themes/_unstyled/templates/init.vm, this contains all the velocity variables available in themes. Variables of interest might be $theme, $theme_display, $layout, $navItems.
  2. docroot/html/themes/_unstyled/templates/portlet.vm, this file is a template to display the individual portlets.
  3. docroot/html/themes/_unstyled/templates/navigation.vm, contains code for displaying the navigation menu with page links.
  4. docroot/html/themes/_unstyled/templates/portal_normal.vm, this file represents a page-template in liferay and this contains the other files like navigation.vm & portlet.vm.

For Velocity:

Okay so for generating the links for Liferay pages in velocity take a look at the following file in the Liferay source code:

/portal-web/docroot/html/themes/_unstyled/templates/navigation.vm

In there you'll see how the default Liferay theme generates the navigation structure for your site. To make life easier for you here it is:

<nav class="$nav_css_class" id="navigation">
<h1>
    <span>#language("navigation")</span>
</h1>

<ul>
    #foreach ($nav_item in $nav_items)
        #if ($nav_item.isSelected())
            <li class="selected">
        #else
            <li>
        #end
            <a href="$nav_item.getURL()" $nav_item.getTarget()><span>$nav_item.icon() $nav_item.getName()</span></a>

            #if ($nav_item.hasChildren())
                <ul class="child-menu">
                    #foreach ($nav_child in $nav_item.getChildren())
                        #if ($nav_child.isSelected())
                            <li class="selected">
                        #else
                            <li>
                        #end
                            <a href="$nav_child.getURL()" $nav_child.getTarget()>$nav_child.getName()</a>
                        </li>
                    #end
                </ul>
            #end
        </li>
    #end
</ul>

So the Velocity is looking through a collection called $nav_items, and then calls the getURL() method on each item to generate the link.

For JSP:

  1. You'll need to make use of the LayoutLocalServiceUtil class, and in particular one of the getLayouts() methods.You'll have to pick the one that best suits your need.
  2. This will return the list of Layouts (your pages), and then you can call getFriendlyURL() on each of these layouts to return it's url This will be a relative url to your site, so something like /my-site-home-page.

Let me know if you have any more questions!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!