天天看點

liferay中使用Jquery傳回json對象處理

1.  引入js。

參見liferay-portlet.xml

       在配置<portlet>時,其中有一項<header-portlet-javascript>

       如

       <header-portlet-javascript>/html/portlet/mail/packed.js</header-portlet-javascript>

       在dtd中這樣解釋

Set the path of JavaScript that will be referenced in the page's header relative to the portlet's context path.

具體的裝載過程在PortletLocalServiceImpl.java中的_readLiferayPortletXML方法中可以看到

2.  Ajax方法

var xHR = jQuery.ajax (

                                    {   //定義通路url,對應struts的配置檔案

                                           url: themeDisplay.getPathMain() + "/mail/action",

                                           //參數設定

                                           data: {cmd: "getRecipients", data: data.value},

                                           //傳回類型

                                           dataType: 'json',

                                           async: false

                                    }

        );

3.  Action傳回josn

在action中通過JSONArray建構json對象傳回,具體可參見MailAction.java

Action必須繼承JSONAction,覆寫父類方法getJSON()

4.  用戶端擷取并處理

擷取傳回内容,轉化為josn格式

var jsonObj = Liferay.Util.toJSONObject(xmlHttpReq.responseText);