2015-06-12 17 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

相關問題