2015-06-12 15 views
1

我试图将一个AJAX'GET'请求中的值传递给我的Portlet类,但我无法完全想出一种方法来访问此类变量的值。我的AJAX代码是:如何通过AJAX调用将数据从页面传递到Portlet类?

function loadXMLDoc() 
{   
    var nocache = new Date().getTime(); 
    var xmlhttp=new XMLHttpRequest(); 
    var url = "${mURL}"; 
    url += '?cache=' + nocache + '&nRows=' + numberOfRows; // Generate unique request URL 
    xmlhttp.open("GET", url,true); 
    xmlhttp.onreadystatechange = function() 
    {   
     if (xmlhttp.readyState==4 && xmlhttp.status==200) 
     { 
      document.getElementById("contentContainer").innerHTML= xmlhttp.responseText; 
     } 
    }  
    xmlhttp.send(); 
} 

其中'nRows'是我想要传递给我的Portlet类的变量。 我的portlet类,我试图取回变量的值的部分是:

@Override 
public void serveResource(ResourceRequest request, ResourceResponse response) throws PortletException,IOException 
{      
    /* Returns null */ 
    String nRows = request.getParameter("nRows"); 

    response.setContentType("text");   
    response.resetBuffer(); 
    String htmlContent = htmlInterpreter.getParsedHTML(); 
    response.getWriter().print(htmlContent); 
    response.flushBuffer();         
} 

约的替代品来访问serveResource(...)方法“NROWS”的价值任何想法?提前致谢!

+2

您必须使用* portlet命名空间*作为URL中参数名称的前缀,否则它们将被忽略。看看这里:https://www.liferay.com/web/meera.success/blog/-/blogs/liferay-requires-name-spaced-parameters –

+0

@Heitor卡斯特罗,http:// stackoverflow可能的副本。 com/questions/17592144/response-to-http-request-with-json-object-in-portlet/17593935#17593935 –

+0

@ParkashKumar在这种情况下,我相信名称空间标签也是一个问题,但我很欣赏替代方案资源!谢谢安德烈,你的回答也是如此! –

回答

1

我推荐使用liferay的<portlet:resourceURL var="resourceURL"> </portlet:resourceURL>AlloyUI而不是使用简单的xmlhttp函数。在的Liferay-portlet.xml中

变化所需的命名空间参数

<requires-namespaced-parameters>false</requires-namespaced-parameters> 

添加以下导入

<%@ taglib uri="http://java.sun.com/portlet" prefix="portlet" %> 
<portlet:defineObjects /> 

更改URL下面

url += '?cache=' + nocache + '&<portlet:namespace/>nRows=' + numberOfRows; 

请找到下面的代码来获得使用ParamUtil参数:

@Override 
public void serveResource(ResourceRequest request, ResourceResponse response) throws PortletException,IOException 
{      
    /* Returns null */ 
    String nRows = ParamUtil.getString(resourceRequest, "nRows"); 

    response.setContentType("text");   
    response.resetBuffer(); 
    String htmlContent = htmlInterpreter.getParsedHTML(); 
    response.getWriter().print(htmlContent); 
    response.flushBuffer();         
} 

欲了解更多详细信息,把你的眼睛此链接:。

http://liferayway.blogspot.in/2013/08/ajaxjsonliferay-example.html

希望它可以帮助你!

+1

非常感谢!你的回答完美解决它! –

+0

很高兴听到..! – Ranjitsinh

相关问题