• Action URL Render URL And Resource URL In Liferay 7 DXP

    Action URL Render URL And Resource URL In Liferay 7 DXP

    Action URL Render URL and the Resource URL are the most widely used words by the liferay developers as far as i know, lets see them one by one in detail in this blog.

    Environment
    Liferay IDE 3.1.1 GA2
    Liferay CE Portal Tomcat 7 GA4
    JDK 8
    MySql 5.7

    For creating Action or Render or resource url below portlet taglib should be defined in the JSP page.
    <%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
    <portlet:defineObjects />

    Action URL

    As we discussed in our earlier blog on portlet phases, using action url we can perform the operations such as add, update and delete the entities (Basically CRUD operations) or form submissions where the user is performing any kind of actions.

    <portlet:actionURL var="doActionVariable" name="doActionMethod"/>

    • var="doActionVariable" : Variable name of the action URL
    • name="doActionMethod" : action method name in the portlet controller class

    For this action URL to work properly there should an action method in the controller as shown below.

    public void doActionMethod(ActionRequest actionRequest, ActionResponse actionResponse) throws IOException, PortletException { System.out.println("doActionMethod Triggered"); }


    Render URL

    Render URL is used to render the jsp inside the portlet, when there are use cases where you just have to render a view or jsp in such use cases you can use the render method

    <portlet:renderURL var="renderJSPVariable">
    <portlet:param name="jspPage" value="/META-INF/resources/second.jsp"/>
    </portlet:renderURL>
    •  var="renderJSPVariable" : Variable name of the render URL
    •  name="jspPage": This parameter is used to specify the name of the jsp to render
    • value="/META-INF/resources/second.jsp" : the path of the jsp to render

    Resource URL

    Resource URL is used when you want perform any validation or Ajax request to accomplish any task on the server without reloading the page context.

    <portlet:resourceURL var="resourceVaraible" id="resourceId"/>
    •   var="resourceVaraible" : Variable name of the resource URL
    •   id="resourceId" : unique id for the serveresource mthod
    For this resource URL to work properly there should an action method in the controller as shown below.

    @Override public void serveResource(ResourceRequest resourceRequest, ResourceResponse resourceResponse) throws IOException, PortletException { String title = ParamUtil.getString(resourceRequest,"title"); if(title.equalsIgnoreCase("Liferaystack")){ System.out.println("Enetered Title is LiferayStack "); resourceResponse.getWriter().print("true"); }else{ System.out.println("Enetered Title is NOT LiferayStack "); resourceResponse.getWriter().print("false"); } }

    Lets Bind the view and controller code into a portlet, i have created a portlet with a Portlet Controller Class and Two JSPs, view.jsp and second.jsp for the demo of Action URL, Render URL and Resource URL, Just Download, deploy and test the URLs.

    Source Code




  • 0 comments:

    Post a Comment