Today we will discuss how to redirect from a portlet to another portlet which are placed on some other page. For this we need two things:-
1)Portlet Layout Id(plid)
We need the plid of the page where we want to redirect.We can get the plid by the page name as:-
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
long plid = LayoutLocalServiceUtil.getFriendlyURLLayout(themeDisplay.getScopeGroupId(), false, pageName).getPlid(); |
2)Portlet Name
We can get the Portlet name (on which we have to redirect) by clicking on wheel icon of portlet than go to look and feel and than click on advance Styling.
So lets start this step by step:-
Step 1:-Create Liferay Project and Portlet
Inside eclipse IDE Click on File->New->Liferay plugin project and give the project name as send-redirect and click finish. After that Right Click on your project ie send-redirect and click on New than click on Liferay Portlet. Provide class name as Demo and select MVCPortlet and click finish.
Note:-You can check the snapshots in my blog on Portlet Configuration Page in Liferay.
Step 2:-Change view.jsp
Open view.jsp and paste this content:-
Explanation:-
Here we create a simple form on submit of form sendAction method is called inside controller.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %> | |
<portlet:defineObjects /> | |
<portlet:actionURL name="sendAction" var="actionUrl"></portlet:actionURL> | |
<form action="<%=actionUrl %>" method="POST"> | |
<input type="submit" name="Submit" value="Submit"> | |
</form> |
Explanation:-
Here we create a simple form on submit of form sendAction method is called inside controller.
Step 3:-Create Second Portlet
Right Click on project and click on New than click on Liferay Portlet.Give Portlet Name as Result and also change jsp name to result.jsp.
Step 4:-Deploy Result Portlet
Deploy the Result portlet and add to any page in my case i am adding this to a public page named about. Now get the Portlet name by going to look and Feel as discussed above. In my case the portlet name is result_WAR_sendredirectportlet.
Step 5:-Change Controller of Demo Portlet
Open Demo.java and Paste this Content:-
Explanation:-
Now deploy both Demo and Result portlet and add Demo Portlet to welcome page and Result Portlet to about page .
Project Structure
Hope this will Help....
You can Download Source code from Send Redirect in Liferay.
Related Post:-
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.demo; | |
import java.io.IOException; | |
import javax.portlet.ActionRequest; | |
import javax.portlet.ActionResponse; | |
import javax.portlet.PortletException; | |
import javax.portlet.PortletRequest; | |
import javax.portlet.PortletURL; | |
import com.liferay.portal.kernel.util.WebKeys; | |
import com.liferay.portal.service.LayoutLocalServiceUtil; | |
import com.liferay.portal.theme.ThemeDisplay; | |
import com.liferay.portal.util.PortalUtil; | |
import com.liferay.portlet.PortletURLFactoryUtil; | |
import com.liferay.util.bridges.mvc.MVCPortlet; | |
public class Demo extends MVCPortlet { | |
public void sendAction(ActionRequest request, ActionResponse response) throws PortletException, IOException { | |
ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY); | |
String pageName="/about"; | |
String portletName = "result_WAR_sendredirectportlet"; | |
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("data", "This Value Comes From Welcome Page"); // set required parameter that you need in doView of another Portlet | |
response.sendRedirect(redirectURL.toString()); | |
} | |
} |
Explanation:-
- Here first we get the plid and than create a URL and send redirect to another page using plid.
- we also send a parameter so that this can be used in another portlet.
- For more detail on LayoutLocalServiceUtil you may refer my blog add Portlet and Change Layout Programmatically.
Step 6:-Change result.jsp
Open result.jsp and paste this:-
Explanation:-
Here we just print the value that is set in Demo.java .
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %> | |
<portlet:defineObjects /> | |
<h3><b><%=renderRequest.getParameter("data") %></b></h3> | |
Explanation:-
For Output
Now deploy both Demo and Result portlet and add Demo Portlet to welcome page and Result Portlet to about page .
When you click on submit it will redirect to Result Portlet which is placed on about page.
Hope this will Help....
You can Download Source code from Send Redirect in Liferay.
Related Post:-