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.
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 :-
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
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
Explanation:-Open view.jsp and paste this content:-
view.jsp
This file contains 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: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> |
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
This file contains 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 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"); | |
} | |
} | |
} |
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 :-
Project Structure:-
Hope this will Help....
You can Download Source code from Multiple Render Method in MVCPortlet.
Related Post:-
You can Download Source code from Multiple Render Method in MVCPortlet.
Related Post:-
Developing a MVCPortlet with Multiple Actions in Liferay
Categorization in web content
Fetch Web Contents Programmatically
Get Journal Article Fields Programmatically
Embedding a Portlet in Web Content
Creating tabs using Web Content
Embedding a Web Content/Journal Article in a Portlet
Index Post Processor Hook
Categorization in web content
Fetch Web Contents Programmatically
Get Journal Article Fields Programmatically
Embedding a Portlet in Web Content
Creating tabs using Web Content
Embedding a Web Content/Journal Article in a Portlet
Index Post Processor Hook
No comments:
Post a Comment