gpt4 book ai didi

liferay portlet 中的 Ajax

转载 作者:行者123 更新时间:2023-12-02 07:09:43 25 4
gpt4 key购买 nike

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

}

}

但是当我使用 ajax 从 view.jsp 文件调用 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>

如果你能帮助我,那对我来说将是很大的帮助。

最佳答案

无需创建 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);
}
}

HTH

关于liferay portlet 中的 Ajax,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7241861/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com