Wednesday, October 5, 2016

Developing a MVCPortlet with Multiple Render Methods in Liferay


In the previous blog Developing a MVCPortlet with Multiple Actions in Liferay  we see how we can handle multiple action methods in a single portlet. Today we will discuss how to develop a MVCPortlet with multiple render methods in Liferay. Here we create a portlet that contain 4 render methods that will redirect to different jsps that contain simple messages.So lets start this step by step:-


Step 1:-Create Liferay Project and Portlet 
Create a Liferay plugin project and than create a portlet in it. You can check snapshot in my previous blog Developing a MVCPortlet with Multiple Actions in Liferay.


Step 2:-Change view.jsp
Open view.jsp and paste this content:-

view.jsp
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<portlet:defineObjects />
<portlet:renderURL var="firstRenderURL">
<portlet:param name="mvcPath" value="/html/demo/first.jsp" />
</portlet:renderURL>
<portlet:renderURL var="secondRenderURL">
<portlet:param name="mvcPath" value="/html/demo/second.jsp" />
</portlet:renderURL>
<!-- ActionURLs but in Controller i use for rendering-->
<portlet:actionURL var="thirdRenderURL" name="CustomRenderMethod">
<portlet:param name="nameofjsp" value="third" />
</portlet:actionURL>
<portlet:actionURL var="fourthRenderURL" name="CustomRenderMethod">
<portlet:param name="nameofjsp" value="fourth" />
</portlet:actionURL>
<h4>First Form</h4>
<a href="${firstRenderURL}" >Go to First JSP</a><br>
<a href="${secondRenderURL}" >Go to Second JSP</a><br>
<h4>Second Form</h4>
<a href="${thirdRenderURL}" >Go to Third JSP</a><br>
<a href="${fourthRenderURL}" >Go to Fourth JSP</a>
view raw view.jsp hosted with ❤ by GitHub

Explanation:-
Here we simply 4 URLs 2 renderURLs that use param mvcPath that will redirect to corresponding jsps. And 2 actionURLs that go to controller where we redirect to different jsps.



Step 3:-Change the Controller
Open your java class and paste this:-

Demo.java
package com.demo;
import java.io.IOException;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.util.bridges.mvc.MVCPortlet;
public class Demo extends MVCPortlet {
public void CustomRenderMethod(ActionRequest actionRequest,ActionResponse actionResponse) throws IOException, PortletException {
String nameofjsp = ParamUtil.getString(actionRequest, "nameofjsp");
if(nameofjsp.equalsIgnoreCase("third")){
actionResponse.setRenderParameter("mvcPath","/html/demo/third.jsp");
}
else{
actionResponse.setRenderParameter("mvcPath","/html/demo/fourth.jsp");
}
}
}
view raw Demo.java hosted with ❤ by GitHub

Explanation:-
Here we create a  method customRenderMethod that check the value of parameter nameofjsp and then set the parameter mvcPath.





Step 4:-Check the Output
Deploy the portlet and add this to a page :-

No comments:

Post a Comment

Total Pageviews

1039172

Number Of Unique Visitor

Free counters!