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?
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) {
// ...
}
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) {
// ...
}
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!')
});
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With