阿贾克斯在Liferay中的Portlet阿贾克斯、Liferay、Portlet

2023-09-10 14:03:02 作者: ̄ェ ̄

我已经创建了Liferay的一个portlet(这是我的第一个Portlet)。在我遵循的Liferay MVC结构。 它的Java文件,如下所示: -

I have created one portlet in liferay(This is my first portlet). In that i have followed mvc structure of liferay. Java file for it as follows:-

package com.liferay.samples;

import java.io.IOException;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;
import javax.portlet.PortletPreferences;
//import javax.portlet.GenericPortlet;
import com.liferay.util.bridges.mvc.MVCPortlet;

public class MyGreetingPortlet extends MVCPortlet {

    @Override
    public void processAction(
            ActionRequest actionRequest, ActionResponse actionResponse)
        throws IOException, PortletException {

       PortletPreferences prefs = actionRequest.getPreferences();

        String greetingname = actionRequest.getParameter("greetingname");
        String greeting     = actionRequest.getParameter("greeting");

        if (greeting != null && greetingname != null) 
        {    

            prefs.setValue(greetingname, greeting);
            prefs.store();
         }
        //System.out.println("In doView code");
        super.processAction(actionRequest, actionResponse);
    }
     public void fetchdataAction( ActionRequest actionRequest, ActionResponse actionResponse)
        throws IOException, PortletException {  
        System.out.println("In doView code");
        //super.fetchdataAction(actionRequest, actionResponse);

    }

}

但是,当我打电话fetchdataAction()用ajax从view.jsp的文件,它没有返回值。 view.jsp的文件,如下所示: -

But when i call fetchdataAction() using ajax from view.jsp file it returns nothing. view.jsp file as follows :-

<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%@ taglib uri="http://liferay.com/tld/aui" prefix="aui" %>
<%@ page import="java.util.*" %>
<%@ page import="javax.portlet.PortletPreferences" %>
<portlet:defineObjects />
<div id="ContentGreeting">
This is the <b>Thired Test</b> portlet.
<%


    PortletPreferences prefs = renderRequest.getPreferences();
//forEachPreference
    //String em[]            = String[2];
    //em                     = prefs.getValues();
    Enumeration em          =   prefs.getNames();
    //ArrayList aList = Collections.list(em);
    //out.println("value :-"+ aList.get(1));
    String[] greeting           =   new String[3];
    int i=0;
    while(em.hasMoreElements())
    {
        String key = (String)em.nextElement();
        greeting[i] = (String)prefs.getValue(

         key, "Hello! Welcome to our portal.");
        //out.println("<br> value :"+greeting);


%>
<p id='id<%= i %>'  onclick="ajaxcallTofetchpage();"><%= greeting[i] %></p>
<portlet:renderURL var="editGreetingURL">

    <portlet:param name="jspPage" value="/edit.jsp" />

</portlet:renderURL>

<a href="<%= editGreetingURL %>&greetingname=<%= key %>">Edit greeting</a>

<%
    i++;
    }
%> 
</div>
<portlet:renderURL var="addGreetingURL">

    <portlet:param name="jspPage" value="/add.jsp" />

</portlet:renderURL>

<p><a href="<%= addGreetingURL %>">Add greeting</a></p>
<portlet:resourceURL var="fetchdataAction">

    <portlet:param name="fetchdataAction" value="/view.jsp" />

</portlet:resourceURL>

<script>
    //$("#id1").hide("slow");
    function ajaxcallTofetchpage()
    {
        $.ajax({
           type: "POST",
           url: "<%= fetchdataAction %>",
           data: "name=John",
           success: function(msg){


            alert(msg);

           }
         });
    }
</script>

如果你能帮助我,这将是对我很大的帮助。

If you are able to help me that will be great help for me.

推荐答案

而不是创建一个 fetchDataAction的(从ActionRequest要求,...)方法,你需要重写的serveResource(ResourceRequest要求,...)

Instead of creating a fetchDataAction( ActionRequest request, ...) method, you need to override serveResource( ResourceRequest request, ...).

public void serveResource( ResourceRequest request, ResourceResponse response)
    throws IOException, PortletException {

    String jspPage = resourceRequest.getParameter("fetchDataAction");
            if (jspPage != null) {
        include(jspPage, request, response, PortletRequest.RESOURCE_PHASE);
    }
    else {
        super.serveResource(request, response);
    }
}

心连心