rendering Liferay page URLs inside of portlets Liferay 6.1

[亡魂溺海] 提交于 2019-12-09 13:28:03

问题


I'm new to liferay and I'm almost positive this is blazingly simple to do: Using velocity markup, I want to be able to generate links to pages within my Liferay website and embed them inside of my portlets on different pages.

I have a vague idea of how it might be done so I searched around figuring it would be posted somewhere, but I can't find anything on it. Incidentally, I want to put whatever code I come up with inside the view.jsp of the portlet. I would use velocity markup here but I don't think (don't know for sure) if that is allowed inside of a jsp.

Please let me know if you need more information to respond.


回答1:


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:

  • You will need a Layout object which you can get with the help of static methods in LayoutLocalServiceUtil.
  • After you get the Layouts, you can use the static methods of com.liferay.portal.util.PortalUtil like getLayoutFriendlyURL or getLayoutFullURL etc to build the URL.

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.



回答2:


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!



来源:https://stackoverflow.com/questions/12045055/rendering-liferay-page-urls-inside-of-portlets-liferay-6-1

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