2013-06-20 37 views
5

我有liferay portlet,我需要嚴重依賴AJAX調用。所以我需要多次調用serveResource方法。一種方法是,我可以通過URL傳遞參數,然後根據該參數區分請求。多個Ajax調用liferay portlets

但在我的情況下,我不得不調用serveResource很多次,因此方法將難以維護。 有沒有這樣的框架?使用它可以維護代碼。

+3

你可以使用Spring MVC。 –

回答

2

使用Spring MVC框架和調用不同的方法,根據在控制你的業務邏輯/用戶操作,

試試下面的代碼 在JSP中的JSP

AUI().ready(
     function(A) {    
      A.use('aui-io-request', 
        function(aui) { 
        A.io.request("<%=loadContents%>", { 
         autoLoad : false, 
         cache : false, 
         dataType : 'json', 
         data:{}, 
         method:'POST', 
         on : { 
          success : function(event, id, xhr) { 
           var response = this.get('responseData'); 
           // add logic here after response 
          } 
         } 
        }).start(); 
       }); 
     }); 

<portlet:resourceURL var="loadContents" id="loadContents"></portlet:resourceURL> 
<portlet:resourceURL var="loadCategories" id="loadCategories"></portlet:resourceURL> 

Ajax調用

in controller/java class

@ResourceMapping("loadCategories") 
    public void loadCategories(final ResourceRequest resourceRequest, final ResourceResponse resourceResponse) 
    { 
     // your business logic goes here 
    } 

    @ResourceMapping("loadContents") 
    public void loadContents(final ResourceRequest resourceRequest, final ResourceResponse resourceResponse) 
    { 
     // your business logic goes here 
    } 

希望以上代碼片段將幫助你,你會得到你正在尋找的東西!

+0

感謝您的回答。我們只會繼續這樣做。儘管已經採用了Spring MVC的決定,但同時也非常感謝您的投入。 :-) – Danish

2

增加更多的在this.We不能使用serveResource方法等processAction。還有可在單一的liferay的portlet多個的processAction(未Spring MVC中的portlet),而在serveReource情況下,將單。

serveResource主要用於ajax調用。我們可以在serveResource方法中通過區分使用資源ID來調用多個ajax請求。

resourceRequest.getResourceID()將返回我們在jsp中使用下面的代碼定義的Id。

<portlet:resourceURL var="demoUrl" id="demoUrl"></portlet:resourceURL>