问题
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
orgetLayoutFullURL
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):
- 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
. - docroot/html/themes/_unstyled/templates/portlet.vm, this file is a template to display the individual portlets.
- docroot/html/themes/_unstyled/templates/navigation.vm, contains code for displaying the navigation menu with page links.
- 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:
- 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.
- 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