liferay portlet 中的 Ajax

发布于 2024-12-02 05:31:33 字数 3578 浏览 0 评论 0原文

我在 liferay 中创建了一个 portlet(这是我的第一个 portlet)。我遵循了liferay的mvc结构。 Java 文件如下:-

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

    }

}

但是当我使用 view.jsp 文件中的 ajax 调用 fetchdataAction() 时,它什么也不返回。 view.jsp 文件如下:-

<%@ 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>

如果您能够帮助我,那将对我有很大帮助。

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

    }

}

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

活雷疯 2024-12-09 05:31:33

您不需要创建 fetchDataAction( ActionRequest request, ...) 方法,而是需要重写 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);
    }
}

华泰

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);
    }
}

HTH

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文