Thursday, November 10, 2016

Creation of Portlet URL in Java

We  already know how to create Liferay URL in jsp file.Today we will discuss how to Create Portlet URLs in Java file or in we can say in Controller . Here we create a simple actionURL in our doview method and than call processAction by using it.So lets start this step by step:-


Step 1:-Create Liferay Project and Portlet 
Just create a project and then create a portlet in it. 



Step 2:-Change your Controller 
Just open your java file and paste this:-

Demo.java
package com.demo;
import java.io.IOException;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.GenericPortlet;
import javax.portlet.PortletException;
import javax.portlet.PortletRequestDispatcher;
import javax.portlet.PortletURL;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
public class Demo extends GenericPortlet {
public void init() {
viewTemplate = getInitParameter("view-template");
}
public void doView(RenderRequest renderRequest, RenderResponse renderResponse)throws IOException, PortletException {
PortletURL myActionURL = renderResponse.createActionURL();
myActionURL.setParameter("name", "Liferay is Easy");
renderRequest.setAttribute("myActionURL", myActionURL.toString());
include(viewTemplate, renderRequest, renderResponse);
}
@Override
public void processAction(ActionRequest request, ActionResponse response)throws PortletException, IOException {
String name = request.getParameter("name");
System.out.println("Name we set in URL=>"+name);
}
protected void include(String path, RenderRequest renderRequest,RenderResponse renderResponse)throws IOException, PortletException {
PortletRequestDispatcher portletRequestDispatcher =getPortletContext().getRequestDispatcher(path);
if (portletRequestDispatcher == null) {
_log.error(path + " is not a valid include");
}
else {
portletRequestDispatcher.include(renderRequest, renderResponse);
}
}
protected String viewTemplate;
private static Log _log = LogFactoryUtil.getLog(Demo.class);
}
view raw Demo.java hosted with ❤ by GitHub

Explanation:-
Focus on line 22 - 24 here we create a ActionURL and set a parameter with name as key and Liferay is Easy as value. Finally we put this URL in request.

Step 3:-Change your view.jsp
Now open your view.jsp and paste this:-

view.jsp
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<jsp:useBean class="java.lang.String" id="myActionURL" scope="request"></jsp:useBean>
<portlet:defineObjects />
<h2>Test for Action URL</h2>
<br>
<a href="<%= myActionURL%>">Click Here For ActionURL</a>
view raw view.jsp hosted with ❤ by GitHub

Explanation:-
1)Here we use useBean tag to retrieve the ActionURL from request and place in variable called myActionURL.
2)Then we create a link ,on click of this link processAction method is Called.



Step 4:-Check the Output
Now deploy the portlet and click on  Click Here For ActionURL:-


when we click the link , processAction method inside the controller is called where we fetch the value of parameter which we set in our actionURL (see Demo.java in step 2).




You can Download Source code from  Creation of PortletURL in java. 


Hope this will Help....

Related Post:-



No comments:

Post a Comment

Total Pageviews

1042196

Number Of Unique Visitor

Free counters!