4

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

}
BhavikKama
  • 8,566
  • 12
  • 94
  • 164
  • 2
    You have asked many questions and have also got relevant answers. So may I request you to atleast acknowledge the answers to your questions by marking them as "accepted"? so that it will help the community know that these questions are answered and will help someone looking for the same thing. Moreover it would not waste time of those members who want to help with other questions which are truly unanswered. Thanks – Prakash K Nov 07 '12 at 05:58

1 Answers1

10

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
}
Community
  • 1
  • 1
Prakash K
  • 11,669
  • 6
  • 51
  • 109
  • What am asking is in my above exmaple if i want to use action method??then shall i use action method as i use reource method??and how? – BhavikKama Nov 06 '12 at 13:58
  • How can i use process action method rather then serveResource method? – BhavikKama Nov 07 '12 at 05:58
  • You want to use multiple `processAction` methods with ajax?? – Prakash K Nov 07 '12 at 05:59
  • ya..like if i can give to url in ajax and it can execute my ajax_Addadvertise method of my protlet.java class – BhavikKama Nov 07 '12 at 06:06
  • I have seen people use `` with ajax but only for `
    ` `POST` and not otherwise. The `actionURL` is generated in slightly different way like this: ` `. Hope this helps. Let me know after you have tried this.
    – Prakash K Nov 07 '12 at 06:40
  • :-) Kindly double check the **parameters** of the method you have written, if you read the error carefully you would know. Since you are using `actionURL` the method should have `actionRequest` and not `resourceRequest` – Prakash K Nov 07 '12 at 06:53
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/19194/discussion-between-prakash-k-and-bhavik-kama) if required. – Prakash K Nov 07 '12 at 06:55
  • can u please see this link http://stackoverflow.com/questions/13263384/how-to-get-file-name-and-other-requested-parameter-in-to-resourceserver-method-w have two doubt more in this Ajax call with upload button...thanks – BhavikKama Nov 07 '12 at 06:58
  • Please don't hijack questions, will take that question separately. Or if you want we can have a chat on that question rather than continuing on this question. If this question is solved kindly mark it as accepted and then we can move on to the next question – Prakash K Nov 07 '12 at 07:03