Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call specific method of portlet.java class rather then overide serveResource method?

I want some help in liferay with ajax. Right now I am calling the ajax method from my view.jsp page to submit some data.

Here is sample code I am using in view.jsp:

<%@ include file="/init.jsp"%>

<portlet:actionURL name="AddTest" var="add1" />
<portlet:resourceURL id="AddTest" var="AddTest"></portlet:resourceURL>

<html>
<head>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>

    <script type="text/javascript">
    function addToDo(addToDo){
        var todo =document.getElementById('toDo').value;
        $.ajax({
            url :addToDo,            
              data: {"todo":todo,"CMD":"addToDo"},
              type: "GET",
              dataType: "text",
            success: function(data) {              
                  $("#toDoList").html(data);
            }
        });
    }
    </script>
</head>

<body>

    <portlet:resourceURL var="addToDo" id="addToDo"></portlet:resourceURL>

    <form>
        <input type="text" name="toDo" id="toDo">
        <button name="Add" type="button" onclick="addToDo('<%=addToDo%>')">Add</button>
        <div id="toDoList">

        </div>
    </form>
</body>
</html> 

and in my portlet.java class there is one method which is called by this ajax call:

@Override
public void serveResource(ResourceRequest request, ResourceResponse response){
    if(request.getParameter("CMD").equals("addToDo")) {

        System.out.println("came here for add");

        mediatype userToDo = new mediatypeImpl();
        //userToDo.setMediaId(12345);
        try {
            userToDo.setPrimaryKey((CounterLocalServiceUtil.increment()));
            userToDo.setMedianame(request.getParameter("todo"));
            mediatypeLocalServiceUtil.addmediatype(userToDo);
        }
        catch (SystemException e) {
            e.printStackTrace();
        }
    }
}

So my question is that right now it is just caling by default @override method from any ajax class. But how can I call specific method of portlet.java class on ajax call?

I am new bee in ajax. So please guide me anyways u can.....

I got following error when calling ajax with url of following

<portlet:actionURL name="ajax_AddAdvertise" var="addToDo" windowState="<%= LiferayWindowState.EXCLUSIVE.toString()%>"> </portlet:actionURL>



06:47:03,705 ERROR [http-bio-8080-exec-23][render_portlet_jsp:154] java.lang.NoSuchMethodException: emenu.advertise.portlet.RestaurantPortlet.ajax_AddAdvertise(javax.portlet.ActionRequest, javax.portlet.ActionResponse)
    at java.lang.Class.getMethod(Class.java:1605)

my process action method as follows

 @ProcessAction(name = "ajax_AddAdvertise")
    public void ajax_AddAdvertise(ResourceRequest request,ResourceResponse response) {

}
like image 516
User 1531343 Avatar asked Dec 27 '22 15:12

User 1531343


1 Answers

how can I call specific method of portlet.java class on ajax call?

I think we can't have two different versions of serveResource methods like we do for action methods atleast not with the default implementation.

If you want different methods you would have to go the Spring MVC (@ResourceMapping) way to have that.

Still, you can define different logic in your serveResource method using the resourceId as follows (a full example):

In the JSP:

<portlet:resourceURL var="myResourceURL" id="myResourceID01" />

In the portlet class the serveResource method will contain the following code:

String resourceID = request.getResourceID();

if(resoureID.equals("myResourceID01")) {
   // do myResourceID01 specific logic
} else {
   // else do whatever you want
}

Please keep in mind [Important]
In a portlet you should not use <html>, <head>, <body> tags since portlets generate fragment of the page and not the whole page. Even if it is allowed your resultant page will not be well-formed and will behave differently on different browsers. And moreover the javascript which modifies DOM element will be totally useless.

Edit after this comment:
You can also use ajax with action methods:

People use <portlet:actionURL> with ajax generally for <form>-POST.

For this the actionURL is generated in a slightly different way in your jsp like this:

<portlet:actionURL name="ajax_AddAdvertise" var="addToDo" windowState="<%= LiferayWindowState.EXCLUSIVE.toString()%>">
</portlet:actionURL>

And in your portlet you can have (as in the question):

@ProcessAction(name = "ajax_AddAdvertise")
public void ajax_AddAdvertise(ActionRequest request, ActionResponse response) {
// ... your code
}
like image 139
Prakash K Avatar answered May 09 '23 19:05

Prakash K