2017-08-11 81 views
0

我需要在成功方法上返回一個字符串。 這是我在jsp頁面中的AJAX代碼。如何在liferay portlet中使用ajax返回對象?

<portlet:actionURL name="filterDocuments" var="filterDocuments"> 
    <portlet:param name="action" value="filterDocuments"/> 
</portlet:actionURL> 

<script type="application/javascript"> 
    var filterDto = { 
     page: 0, 
     tags: {}, 
     search: null 
    }; 
    $.ajax({ 
     type: "POST", 
     url: "<%=filterDocuments%>", 
     data: filterDto, 
     success: function (msg) { 
      alert(msg); 
     } 
    }); 
</script> 

這裏是我在後端的處理程序。

@ActionMapping(params = "action=filterDocuments") 
    public void filterDocuments(ActionRequest actionRequest, ActionResponse actionResponse) throws IOException { 
     PrintWriter writer = PortalUtil.getHttpServletResponse(actionResponse).getWriter(); 
     writer.write("text"); 
     writer.flush(); 
     writer.close(); 
    } 

結果它顯示了警告消息中的空字符串。 如何處理它?

UPDATE 我改變控制器方法

@ResourceMapping(value = "filterDocuments") 
public void filterDocuments(ActionRequest actionRequest, ActionResponse actionResponse) throws IOException { 
    PrintWriter writer = PortalUtil.getHttpServletResponse(actionResponse).getWriter(); 
    writer.write("text"); 
    writer.flush(); 
    writer.close(); 
} 

和URL在我的JSP是

<portlet:resourceURL var="ajaxResourceURL"/> 

<script type="application/javascript"> 
    var filterDto = { 
     page: 0, 
     tags: {}, 
     search: null 
    }; 
    $.ajax({ 
     type: "POST", 
     url: "<%=ajaxResourceURL%>", 
     data: filterDto, 
     success: function (msg) { 
      alert(msg); 
     } 
    }); 
</script> 

而現在它給了我一個異常

org.springframework。 web.portlet.NoHandlerFoundException:找不到處理程序門戶請求:模式 '觀看',相位 'RESOURCE_PHASE',參數映射[ '搜索' - >陣列[ ''], '頁' - >陣列[ '0']]

全控制器類

@Controller 
@RequestMapping("VIEW") 
@Log4j 
public class SpringMVCController extends MVCPortlet { 




    @RenderMapping 
    public String view(RenderRequest request, RenderResponse response) { 


     String view = PropertiesConfig.getAsString("views", request); 

     return view; 
    } 


    @Override 
    public void serveResource(ResourceRequest resourceRequest, 
           ResourceResponse resourceResponse) 
      throws IOException, PortletException { 
     try { 
      PrintWriter writer = resourceResponse.getWriter(); 
      writer.write("text"); 
      writer.flush(); 
      writer.close(); 
      CaptchaUtil.serveImage(resourceRequest, resourceResponse); 
     } catch (Exception e) { 
      log.error(e.getMessage()); 
     } 
    } 


    @ResourceMapping(value = "action=filterDocuments") 
    public void filterDocuments(ActionRequest actionRequest, ActionResponse actionResponse) throws IOException { 
     PrintWriter writer = PortalUtil.getHttpServletResponse(actionResponse).getWriter(); 
     writer.write("text"); 
     writer.flush(); 
     writer.close(); 
    } 


} 
+0

與您的代碼的問題是,你調用'使用AJAX,這不是一個有效的模式actionURL'。 –

+0

將'portlet:actionURL'改爲'portlet:resourceURL'和'@ ActionMapping'改爲'@ResourceMapping'。 –

+0

[在portlet中使用json對象響應http請求]的可能重複(https://stackoverflow.com/questions/17592144/respond-to-http-request-with-json-object-in-portlet) –

回答

2

與更新的代碼/是問題(一個或多個)以下:

我。您尚未根據您的portlet:actionURL指定portlet:resourceURL上的id屬性(具有所需的值)。將其更改爲:

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

ii。映射方法在行動類簽名不一樣的UI的portlet:resourceURL id屬性的值,將其更改爲:

@ResourceMapping("filterDocuments") 
public void filterDocuments(ResourceRequest request, ResourceResponse response) 
throws IOException, SystemException { 
    PrintWriter writer = response.getWriter(); 
    writer.write("text"); 
    writer.flush(); 
    writer.close(); 
} 

III。從操作類中刪除另一個serveResource方法,因爲這僅適用於通用(默認)Portlet類型。

參考: Liferay Spring Portlet Tutorial

+0

thanx。它有幫助。這liferay讓我發瘋 –

+0

@SergShapoval,不客氣,快樂編碼! –