Foros de discusión

Link to another portlet on another page

Ralf Haller, modificado hace 8 años.

Link to another portlet on another page

Junior Member Mensajes: 54 Fecha de incorporación: 13/05/13 Mensajes recientes
Hi,

I want to create a link (friendly URL) from one portlet to another portlet on another page. Using portlet:renderURL however only creates a link to the same portlet.

The renderURL contained in the JSF tag lib liferay-portlet can create links to other portlets on other pages. How can I achieve this using liferay faces?

Ralf
thumbnail
Andrew Jardine, modificado hace 8 años.

RE: Link to another portlet on another page

Liferay Legend Mensajes: 2416 Fecha de incorporación: 22/12/10 Mensajes recientes
Hi Ralf,

You should be able to use the <liferay-portlet:renderURL/> taglib in your page, provided you have included the taglib in your page (or inherited pages). I don't typically work with JSF so I am not sure if perhaps this is a nuance that makes it different.

Alternatively, if you have a portlet class that is extending your GenericFacesPortlet you can calculate the URL using the Liferay API -- PortletURLFactoryUtil class I believe is what you are looking for.
thumbnail
Ravi Kumar Gupta, modificado hace 8 años.

RE: Link to another portlet on another page

Liferay Legend Mensajes: 1302 Fecha de incorporación: 24/06/09 Mensajes recientes
All you need to know is portlet id of the portlet you want to call.
long plid = PortalUtil.getPlidFromPortletId(themeDisplay.getScopeGroupId(), true, portletId);
PortletURLFactoryUtil.create(request, portletId, plid, PortletRequest.RENDER_PHASE);

Check my blog post here..
http://techdc.blogspot.in/2015/03/liferay-get-url-using-portlet-id.html
thumbnail
Andrew Jardine, modificado hace 8 años.

RE: Link to another portlet on another page

Liferay Legend Mensajes: 2416 Fecha de incorporación: 22/12/10 Mensajes recientes
And what if you have the same portlet on multiple pages? If you know the page you want to target, doesn't it make more sense to use --


String friendlyURL = "/the/page/you/want/to/hit";
boolean isPrivateLayout = Boolean.FALSE;

Layout plid = LocalServiceUtil.getFriendlyURLLayout(groupId, isPrivateLayout, friendlyURL);


That way you know for sure you get the id for the Layout you want to target no?
thumbnail
Ravi Kumar Gupta, modificado hace 8 años.

RE: Link to another portlet on another page

Liferay Legend Mensajes: 1302 Fecha de incorporación: 24/06/09 Mensajes recientes
True Andrew. I should update myself emoticon

I would like to know if it helps Ralf.
Ralf Haller, modificado hace 8 años.

RE: Link to another portlet on another page

Junior Member Mensajes: 54 Fecha de incorporación: 13/05/13 Mensajes recientes
Andrew Jardine:
And what if you have the same portlet on multiple pages? If you know the page you want to target, doesn't it make more sense to use --


String friendlyURL = "/the/page/you/want/to/hit";
boolean isPrivateLayout = Boolean.FALSE;

Layout plid = LocalServiceUtil.getFriendlyURLLayout(groupId, isPrivateLayout, friendlyURL);


That way you know for sure you get the id for the Layout you want to target no?


That's what I did, however I need a friendly URL with no parameters at all. Here's what I get:

http://localhost:8080/web/guest/teil?p_p_id=_up2datepartshoppartportlet_WAR_up2date4partshopportlet&p_p_lifecycle=0&__up2datepartshoppartportlet_WAR_up2date4partshopportlet_manufacturer=AUDI&__up2datepartshoppartportlet_WAR_up2date4partshopportlet_model=A1&__up2datepartshoppartportlet_WAR_up2date4partshopportlet_node=Anlasser&__up2datepartshoppartportlet_WAR_up2date4partshopportlet_catalog=RECYCLE&__up2datepartshoppartportlet_WAR_up2date4partshopportlet_part=1652

however I want this:

http://localhost:8080/web/guest/teil/-/teil/AUDI/A1/RECYCLE/1652

My code looks like this:

ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);
String pageName = "/teil";
String portletName = "_up2datepartshoppartportlet_WAR_up2date4partshopportlet";
long plid = 0L;
try {
	plid = LayoutLocalServiceUtil.getFriendlyURLLayout(themeDisplay.getScopeGroupId(), false, pageName).getPlid();
} catch (Exception e) {
	e.printStackTrace();
}
PortletURL redirectURL = PortletURLFactoryUtil.create(PortalUtil.getHttpServletRequest(request), portletName, plid, PortletRequest.RENDER_PHASE);
redirectURL.setParameter("manufacturer", "AUDI");
redirectURL.setParameter("model", "A1");
redirectURL.setParameter("node", "Anlasser");
redirectURL.setParameter("catalog", parts.getRowData().getCatalogCode());
redirectURL.setParameter("part", parts.getRowData().getCode());
FacesContext.getCurrentInstance().getExternalContext().redirect(redirectURL.toString());


BTW, using liferay-portlet:renderURL did not work in JSF (I included the tag lib in the page of course).

Ralf
thumbnail
Andrew Jardine, modificado hace 8 años.

RE: Link to another portlet on another page

Liferay Legend Mensajes: 2416 Fecha de incorporación: 22/12/10 Mensajes recientes
Hey Ralf,

Can you share with us also your Friendly URL configuration that you have configured for the portlet?
Ralf Haller, modificado hace 8 años.

RE: Link to another portlet on another page

Junior Member Mensajes: 54 Fecha de incorporación: 13/05/13 Mensajes recientes
Hi Andrew,

my friendly routes look like this:

<routes>
	<route>
		<pattern>/{node}/{manufacturer}/{model}/{catalog}/{part}</pattern>
		<ignored-parameter name="p_auth" />
		<ignored-parameter name="p_p_id" />
		<ignored-parameter name="p_p_col_id" />
		<ignored-parameter name="p_p_col_count" />
		<ignored-parameter name="_facesViewIdRender" />
		<implicit-parameter name="p_p_lifecycle">0</implicit-parameter>
		<implicit-parameter name="p_p_state">normal</implicit-parameter>
		<implicit-parameter name="p_p_mode">view</implicit-parameter>
	</route>
</routes>


in liferay-portlet.xml I have:

...
<friendly-url-mapper-class>com.liferay.portal.kernel.portlet.DefaultFriendlyURLMapper</friendly-url-mapper-class>
<friendly-url-mapping>teil</friendly-url-mapping>
<friendly-url-routes>up2date-partshop-part-friendly-url-routes.xml</friendly-url-routes>
...


What else do you need?

Ralf
thumbnail
Andrew Jardine, modificado hace 8 años.

RE: Link to another portlet on another page

Liferay Legend Mensajes: 2416 Fecha de incorporación: 22/12/10 Mensajes recientes
hey Ralf,

My first question, and it's really just curiosity more than anything -- in your portlet.xml, have you named your portlet _up2date-partshop-part-portlet. I mean, have you prefixed your portlet name with an underscore? and if so, can I ask why? You can name it whatever you like of course I've just never seen that notation so I am curious about it.

As for the friendly URL, everything looks right to me. I have had this problem in the past though and here is the advice I was given by someone at Liferay actually to help solve problems I run into. The process is a little tedious at times but since it has in the end produced, for me anyway, pretty regular results on first pass I am convinced that it is faster than going for gold and spending hours trying to figure out where it is hiding emoticon.

This may or may not help so I'll leave it to you to decide if you want to try it. I'll use your friendly URL routes as an example

1. Always put the patterns in the file ranging from most complicated to least, and build this list bottom up.

2. Most of the uggo parameters that are in the url (p_p_lifecycle, p_p_mode, etc) are taken care of for you so you don't have to worry about them. To make sure that my routes file is picked up -- if it is, then all of them should be taken care of and I should only have to worry about my parameters. (DISCLAIMER: I don't use JSF so I'm going to guess at those). So I would start with this --


<routes>
     <route>
         <pattern>/</pattern>
     </route>
</routes>


3. After deploy you should get a URL that does not include the default parameters. If you don't, then it usually means the routes file is not referenced properly. Next step is to add one more route --


<routes>
     <route>
         <pattern>/{node}</pattern>
     </route>
     <route>
         <pattern>/</pattern>
     </route>	
</routes>


4. Obviously, this time I would expect to see my friendly node name in place of __up2datepartshoppartportlet_WAR_up2date4partshopportlet_node=Anlasser in the URL.

5. Continue this approach, adding more patterns, always injecting them into the first position. So you would end up with something like this --


<routes>
     <route>
         <pattern>/{node}/{manufacturer}/{model}/{catalog}/{part}</pattern>
     </route>
     <route>
         <pattern>/{node}/{manufacturer}/{model}/{catalog}</pattern>
     </route>
     <route>
         <pattern>/{node}/{manufacturer}/{model</pattern>
     </route>
     <route>
         <pattern>/{node}/{manufacturer}</pattern>
     </route>
     <route>
         <pattern>/{node}</pattern>
     </route>
     <route>
         <pattern>/</pattern>
     </route>	
</routes>


And at this stage you would add whatever implicit parameters are required to clean up the stragglers (eg. facesViewIdRender). One other thing I noticed though is that you have, in your route, a pauth parameter listed. pauth tokens are generated during ACTION urls as part of CSRF protection (I believe). Friendly URLs only work with RENDER urls because the parameters have to be in the URL to be matched against the pattern.

As I said, the pain in the ass here is change something, deploy, change something deploy, etc. But it allows you to pinpoint errors much faster and from my experience, you end up doing something like this anyway to try to figure out where it went wrong. I you do decide to give that a shot, let me know if it helps at all. If not, we can try something else.
Ralf Haller, modificado hace 8 años.

RE: Link to another portlet on another page

Junior Member Mensajes: 54 Fecha de incorporación: 13/05/13 Mensajes recientes
Hi Andrew,

you made my day! I found two problems in my code:

1. The underscore in the portlet's name was wrong
2. I changed my friendly routes as you suggested

and everything worked fine.

Thanks for pushing me in the right direction...

Ralf
thumbnail
Andrew Jardine, modificado hace 8 años.

RE: Link to another portlet on another page

Liferay Legend Mensajes: 2416 Fecha de incorporación: 22/12/10 Mensajes recientes
Hi Ralf,

My pleasure. I had the same a few years ago when I first started using friendly URLs and spent a painful couple of days trying to sort it all out. Happy to help others avoid this pain. emoticon