سؤال

I have made an JSR 168 portlet as follows:

public class GetTest extends GenericPortlet {
    @Override
    public void doView(RenderRequest request, RenderResponse response) 
             throws PortletException, IOException {
        PortletRequestDispatcher rd = 
                      getPortletContext().getRequestDispatcher("/getTest.jsp");
        rd.include(request, response);
    }
}

The portlet for this is named as getTest.portlet and is at WebContent folder. The jsp page for this:

<%
    String params = request.getParameter("params");
    out.print("Params: " + params);
%>

Now I want to make an Ajax get request to this portlet using DISC framework of Weblogic. How can I do this?

I searched on net regarding this but didnt any useful example which I can use. What I have tried is as follows:

in some other.jsp:

.....
<script type="text/javascript">
    var dataUrl = "/getTest.portlet?params=hi";
    var xmlhttp = new bea.wlp.disc.io.XMLHttpRequest();
    xmlhttp.onreadystatechange = function() {
        if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) {
            alert(xmlhttp.responseText);
        }
    }
    xmlhttp.open('GET', dataUrl, true);
    xmlhttp.send(null);
</script>
....

In alert I get blank. I should get "Params: hi" as it is in jsp page of this portlet. How can I achieve this?

I read following articles but did not find anything useful or may be I missed something.

I have also enabled disc for the desktop portal in which this portlet is attached.

هل كانت مفيدة؟

المحلول

Finally, I found it myself. I need to use Resource Serving feature of JSR 286 portlet to get data through Ajax request. What you need to do is:

  1. Build a resource url of your portlet:<portlet:resourceURL var="homeURL" id="home" escapeXml="false" />
  2. Use this url to make an Ajax request to your portlet.

    var path="<%=homeURL.toString()%>";
    request.onreadystatechange=function () {
        if (request.readyState == 4) {
            if (request.status == 200) {
                alert(request.responseText);
            } else {
                alert("Problem retrieving data from server.");
            }
        }
    };
    request.open("GET", path, true);
    request.send(null); 
    
  3. In portlet write your logic in serveResource(ResourceRequest request, ResourceResponse response) method. Set data values as attributes to request and then forward request, response to JSP page using request dispathcer. Or you can directly write data values in JSON format to response writer.(response.getWriter())
  4. If you choose to pass data to a JSP page then on JSP page get data from request attributes and display them as needed.
  5. If you choose to pass data as JSON format using response writer then you need to parse that data in JavaScritp and then display them in appropriate format.
  6. And if you pass data to portlet you can easily pass it in the same way you pass it to a servlet using an Ajax request. In this line request.send(null) instead of null pass your data.

Hope this helps future visitors. :-)

Still I am not sure if this is the best way to do this. Any other solutions are always welcome.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top