How to call a specific method of the portlet.java class and not override the serveResource method?

I need help in liferay with ajax. Right now I'm calling the ajax method from the view.jsp page to send some data.

Here is an example of the code that I use 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 portlet.java is one method that 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, now it just shakes the default @override method from any ajax class. But how can I call a specific method of the portlet.java class to call ajax?

I am a new bee in ajax. So please guide me anyway, you can .....

I got the following error when calling ajax with the URL of the 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 is as follows

  @ProcessAction(name = "ajax_AddAdvertise") public void ajax_AddAdvertise(ResourceRequest request,ResourceResponse response) { } 
+4
source share
1 answer

how can i call a specific method of the portlet.java class when calling ajax?

I think that we cannot have two different versions of the serveResource methods that we do for action methods, at least not with the default implementation.

If you need different methods, you will need Spring MVC ( @ResourceMapping ) to have this.

However, you can define different logic in your serveResource using resourceId as follows ( full example ):

In JSP:

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

In the portlet class, the serveResource 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 the <html> , <head> , <body> tags, since portlets generate a fragment of the page, not the entire page. Even if this is allowed, your resulting page will not be well formed and will behave differently in different browsers. And besides, javascript that modifies the DOM element will be completely useless.

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

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

To do this, the actionURL generated a little differently 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 } 
+10
source

Source: https://habr.com/ru/post/1444278/


All Articles