Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Liferay <portlet:actionURL>

In my jsp I have the following code:

<portlet:actionURL name="addDetails" var="addDetailsURL" />
<aui:form name="addDetails" action="<%=addDetailsURL.toString() %>" method="post" >
        <aui:input type="text" label="name:" name="name" value="" />
        <aui:input type="text" label="surname:" name="surname" value="" />
        <aui:input type="text" label="age:" name="age" value="" />
        <aui:button type="submit" value="addDetails" />
</aui:form>

I am using liferay. I want to submit this data which will be processed in a java class. my java class has few functions. how should I specify in the above jsp that it should access the particular function in java after submitting the form?

like image 363
Seeya K Avatar asked Mar 11 '13 06:03

Seeya K


4 Answers

If your portlet inherits MVCPortlet, simply create a public method with the same "name" as your actionURL, that takes an ActionRequest and ActionResponse argument:

public void addDetails(ActionRequest req, ActionResponse rsp) {
    // ...
}
like image 76
perp Avatar answered Oct 26 '22 23:10

perp


Just for the record, you can also use annotations. For example, you have this jsp:

<portlet:actionURL var="urlAction">
    <portlet:param name="javax.portlet.action" value="myAction"/>
</portlet:actionURL>

<aui:form action="${urlAction}" method="post">
    <aui:input type="text" label="name:" name="name" value="" />
    <aui:button type="submit" value="Submit" />
</aui:form>

And this Java method in your MVCPortlet:

@ProcessAction(name = "myAction")
public void method(ActionRequest actionRequest, ActionResponse actionResponse) {
    // ...
}
like image 22
Dani Avatar answered Oct 27 '22 00:10

Dani


If you want to handle only one action:

Portlet

@Override
public void processAction(ActionRequest actionRequest, ActionResponse actionResponse) throws IOException, PortletException
{
    // TODO Auto-generated method stub
    super.processAction(actionRequest, actionResponse);
}

JSP

<form action="<portlet:actionURL />" method="post">
    <aui:button type="submit" value="addDetails" />
</form>

If you want more than one action method:

public void myAction(ActionRequest request, ActionResponse response)
{
     Long id = ParamUtil.getLong(request, "myParam");
     // TODO
}

JSP

<portlet:actionURL name="myAction" var="myActionVar">
    <portlet:param name="myParam" value="${currentElement.id}"></portlet:param>
</portlet:actionURL>

<a href="${myActionVar}">Click Me!</a>

But you probably would do:

Portlet

@Override
public void serveResource(ResourceRequest request, ResourceResponse response) throws IOException
{
    String action = request.getParameter("action");

    if(action.equalsIgnoreCase("myAction")){
        // handle AJAX call
    }
}

JSP

<portlet:resourceURL var="resourceUrl" />
<input id="resourceURL" type="hidden" value="${resourceUrl}" />

JavaScript

$.post($('#resourceURL').val(),{
    action : 'myAction'
}).done(function(result){
    alert('Action completed successfully!')
});
like image 25
kayz1 Avatar answered Oct 27 '22 01:10

kayz1


If you are not using MVCPortlet but rather something like GenericPortlet, add a param to the actionURL like this:

<portlet:actionURL name="addDetails" var="addDetailsURL" >
<portlet:param name="method" value="addDetails"/>
</portlet:actionURL>

Then in your processAction method, handle checking the method type this way:

public void processAction(ActionRequest aReq, ActionResponse aResp) throws IOException,
            PortletException {

        final String method = aReq.getParameter("method");

        if ( method != null && method.equals("addDetails")) 
        {   
            addDetails(aReq, aResp);

        } 
like image 34
Sean Gildea Avatar answered Oct 26 '22 23:10

Sean Gildea