Liferay <portlet:actionURL>
Asked Answered
B

6

7

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?

Brattice answered 11/3, 2013 at 6:45 Comment(0)
D
14

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) {
    // ...
}
Dockery answered 11/3, 2013 at 6:51 Comment(2)
perp: so in my actionURL above: <portlet:actionURL name="addDetails" var="addDetailsURL" /> name is addDetails so my function name should be addDetails, right?Brattice
you can also use @ProcessAction annotaion.Subjection
H
9

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) {
    // ...
}
Hedge answered 12/3, 2013 at 11:37 Comment(0)
P
6

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!')
});
Phyfe answered 11/3, 2013 at 6:50 Comment(1)
What if I have many functions in the java class? then how will it know which function it should go to?Brattice
H
3

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);

        } 
Hellgrammite answered 12/6, 2013 at 17:50 Comment(0)
P
2

Paste this in your controller class

public void addDetails(ActionRequest actionRequest, ActionResponse actionResponse) {
    //this method will be called on submitting the form
    System.out.println("addDetails");
}

or you can use it as

@ProcessAction(name="addDetails")
public void myaddDetails(ActionRequest actionRequest, ActionResponse actionResponse) {
    //this method will be called on submitting the form
    System.out.println("addDetails");
}
Poet answered 1/11, 2015 at 23:13 Comment(0)
M
1

If your java class is extending MVCPortlet then just the method name should match actionURL name i.e. addDetails . If the class is extending GenericPortlet you will have to specify annotaion for the same on top of your method like @ProcessAction(name="addDetails").IOn this case the name of your method can be different.

Mauricio answered 13/7, 2014 at 13:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.