javajavascriptservletsportletwebsphere-portal

Getting response form custom @proceesAction in WebSphere Portal Portlet


I am developing a Portlet in WebSphere Portal 8 and I am having problems getting the response from a custom @processAction method, the method is called and executed, but in the jsp I can't get the data returned.

I have a jsp file which has:

-definition of portlet actionURL...

<portlet:defineObjects/>
<portlet:actionURL var="cargarListadoConcursosURL">
       <portlet:param name="<%=ActionRequest.ACTION_NAME%>" value="cargarListadoConcursos" />
    </portlet:actionURL>

-JavaScript method with ajax post method:

<script type="text/javascript">
    $(document).ready(function() {
        cargarListadoConcursos();
    });
    
    function cargarListadoConcursos() {
        $.ajax({
            url : '<%=cargarListadoConcursosURL%>',
            type : 'POST',
            dataType : 'json',
            success : function(data) {
                alert(data);
                //do something!!!
            }
        });
    }

and my portlet class looks like:

public class ListadoConcursosPortlet extends GenericPortlet
{
   //more methods...

    @ProcessAction(name="cargarListadoConcursos")
    public void cargarListadoConcursos(ActionRequest request, ActionResponse response) throws PortletException, IOException {
        HttpServletResponse resp = PortletUtils.getHttpServletResponse(response);
        resp.setContentType("application/json");
        resp.setCharacterEncoding("UTF-8");
        PrintWriter writer = resp.getWriter();
        writer.append(gson.toJson(new ArrayList<Concurso>()));
        writer.flush();
        resp.flushBuffer();
        System.out.println("Paso por cargarListadoConcursos");
    }
}

I think that the portlet.xml its fine because the jsp call the portlet controller (the syso appears at console)...

Well in conclusion, the problems is that I can't get the JSON object in my JavaScript called, and the alert(data) is never executed...


Solution

  • The issue is that the page is refreshed when you it the actionURL. You need to use a Resource Serving Portlet, the serveResource method and an resourceURL which doesn't refresh the page when called.