• Action URL in Liferay

    Posted on July 16, 2015 by Hamidul Islam in Liferay.

    action-url-in-liferay


    aimWithout action URL we can't imagine portlet development. In this article we will discuss about action URL in Liferay. In simple by action URL we invoke process action of portlet's life cycle. By the end of this article we will know the various way  to create action URL in Liferay. This article is very basic which will be helpful for the beginners. 


    1. What's Action URL

    To be simple its just a URL in UI. But the main aim of the action URL is to invoke process action like

    	@ProcessAction(name = "addBook")
    	public void addBook(ActionRequest actionRequest,ActionResponse actionResponse){
    		//Some logic here
    	}
    

    So in UI we create Action URL and once Action URL is invoked it executes some java code (which is called nothing but process action) in the server side.
    2. Create Action URL by RenderResponse object
    So how to get renderResponse object in our JSP? Its quite simple. By adding below code in our JSP renderResponse object will be ready to use

    <%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
    <portlet:defineObjects />
    

    Lets see how to create actionURL from renderResponse object

    <%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
    <%@page import="javax.portlet.PortletURL"%>
    <portlet:defineObjects />
    <%
    	PortletURL actionURL = renderResponse.createActionURL();
    	actionURL.setParameter("javax.portlet.action", "someProcessAction");
    %>
    
    <a href="<%=actionURL%>">Call Process Action</a>
    

    And corresponding process action

    	@ProcessAction(name="someProcessAction")
    	public void someProcessAction(ActionRequest actionRequest, ActionResponse actionResponse){
    		System.out.println("#######Calling some process action##############");
    	}
    

    3. Send some parameter while invoking actionURL

    <%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
    <%@page import="javax.portlet.PortletURL"%>
    <portlet:defineObjects />
    <%
    	PortletURL actionURL = renderResponse.createActionURL();
    	actionURL.setParameter("javax.portlet.action", "someProcessAction");
    	actionURL.setParameter("some-parameter", "some-value");
    %>
    
    <a href="<%=actionURL%>">Call Process Action</a>
    

    Retrieve Value in process action

    	@ProcessAction(name="someProcessAction")
    	public void someProcessAction(ActionRequest actionRequest, ActionResponse actionResponse){
    		String value = ParamUtil.getString(actionRequest, "some-parameter");
    		System.out.println("#######Value##############"+value);
    		System.out.println("#######Calling some process action##############");
    	}
    

    4. Set window state in action URL

    <%@page import="com.liferay.portal.kernel.portlet.LiferayWindowState"%>
    <%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
    <%@page import="javax.portlet.PortletURL"%>
    <portlet:defineObjects />
    <%
    	PortletURL actionURL = renderResponse.createActionURL();
    	actionURL.setParameter("javax.portlet.action", "someProcessAction");
    	actionURL.setWindowState(LiferayWindowState.MAXIMIZED);
    %>
    
    <a href="<%=actionURL%>">Call Process Action</a>
    

    We have set window state to MAXIMIZED. That means when we invoke the action URL the size of the portlet will be maximized.


    NoteNote:

    i) When  window state is MAXIMIZED the portlet occupy the whole page of the portal page.

    ii) Only one portlet can be in MAXIMIZED state for a given page. For example if there are 5 portlets in a page and if any portlet goes to MAXIMIZED state then other portlets will be hidden

    iii) We can use different window states by using the class com.liferay.portal.kernel.portlet.LiferayWindowState.


    5. Create action URL by tag portlet:actionURL

    This is the easiest way to create action URL. For example

    <%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
    <portlet:defineObjects />
    <portlet:actionURL var="actionURL" name="someProcessAction"/>
     <a href="<%=actionURL%>">Call Process Action</a>
    

    Various attributes of portlet:actionURL Tag

    windowState (Type: String,non-required)indicates the window state that the portlet should have when the link is executed. These are the window states are predefined: minimized, normal, and maximized. If the specified window state is illegal for the current request, a JspException is thrown. Reasons for a  window state being illegal may include that the portal does not support this state, the portlet has not declared in its deployment descriptor that it supports this state, or the current user is not allowed to switch to  this state. If a window state is not set for a URL, it should stay the same as the window state of the current request.The window state attribute is not case sensitive.

    portletMode (Type: String, non-required) – indicates the portlet mode that the portlet must have when this link is executed, if no error condition ocurred. These are the portlet modes are predefined: edit, help, and view. If the specified portlet mode is illegal for the current request, a JspException is thrown. Reasons for a portlet mode being illegal may include that the portal does not support this mode, the portlet has not declared in its deployment descriptor that it supports this mode for the current markup, or the current user is not allowed to switch to this mode. If a portlet mode is not set for a URL, it must stay the same as the mode of the current request. The portlet mode attribute is not case sensitive.

    var (Type: String, non-required) – name of the exported scoped variable for the action URL. The exported scoped variable must be a String. By default, the result of the URL processing is written to the current JspWriter. If the result is exported as a JSP scoped variable, defined via the var attributes, nothing is written to the current JspWriter.

    After the URL is created it is not possible to extend the URL or add any further parameter using the variable and String concatenation. If the given
    variable name already exists in the scope of the page or it is used within an iteration loop, the new value overwrites the old one.

    secure (Type: String, non-required) – indicates if the resulting URL should be a secure connection (secure="true") or an insecure one (secure="false"). If the specified security setting is not supported by the run-time environment, a JspException is thrown.If the security is not set for a URL, it must stay the same as the security setting of the current request.

    copyCurrentRenderParameters (Type: boolean, non-required) – if set to true requests that the private render parameters of the portlet of the current request is attached to this URL. It is equivalent to setting each of the current private render parameters via the <portlet:param> tag. If additional <portlet:param> tags are specified parameters with the same name as an existing render parameter will get merged and the value defined in additional <portlet:param> tags must be pre-pended. The default for this attribute is false.

    escapeXml (Type: boolean, non-required) – determines whether characters  <,>,&,'," in the resulting output should be converted to their corresponding character entity codes. For example

    < gets converted to &lt;

     > gets converted to &gt;

     & gets converted to &amp;

    ' gets converted to &#039;

    " gets converted to &#034;

    Default value is true.

    name (Type: String, non-required) – specifies the name of the action that can be used by GenericPortlet to dispatch to methods annotated with ProcessAction. Setting this name will result in adding a parameter to this action URL with the name javax.portlet.action.


    Post Tagged with , ,

2 Responses so far.

  1. sgvan says:

    Nice explanation. Could you please give sample example of portlet which consists of four button new, edit, delete, select action on one form.

    Many thanks !!

Top
%d bloggers like this:

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close