2011-02-07 105 views
1

我試圖在liferay上添加一個新的IFrame portlet,點擊鏈接。我已經在一些問題上實現了它。在Liferay頁面重新加載期間添加Portlet實例

首先,我使用Liferay的SDK創建一個portlet和定製的默認view.jsp

<portlet:renderURL var="pageDisp"> 
    <portlet:param name="jspPage" value="/view.jsp" /> 
</portlet:renderURL> 
<a href="<%=pageDisp%>&clink=g">Google</a> <br /> 
<a href="<%=pageDisp%>&clink=y">Yahoo</a> <br /> 
<a href="<%=pageDisp%>&clink=d">Daily Tech</a> <br /> 
<a href="<%=pageDisp%>&clink=w">Wired</a> <br /> 

改變portlet.xml

<portlet-class>com.liferay.util.bridges.mvc.MVCPortlet</portlet-class> 

<portlet-class>test.LinksPortlet</portlet-class> 

我已經實現後續在LinksPortlet荷蘭國際集團代碼:

public void render(RenderRequest request, RenderResponse response) throws PortletException, java.io.IOException { 
     log("LinksPortlet:render"); 
     try { 
      ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(com.liferay.portal.kernel.util.WebKeys.THEME_DISPLAY); 
      LayoutTypePortlet layoutTypePortlet = themeDisplay.getLayoutTypePortlet(); 
      List<Portlet> portletList = layoutTypePortlet.getPortlets(); 
      for(Portlet p : portletList) { 
       log(p.getPortletId()); 
       log(p); 
       log(p.getDisplayName()); 
       log(p.getInitParams()); 
      } 
      String c = request.getParameter("clink"); 
      log(c); 
      if(c != null && (c.equals("g") || c.equals("y") || c.equals("d") || c.equals("w"))) { 
       String portletId = layoutTypePortlet.addPortletId(Long.parseLong(request.getRemoteUser()), PortletKeys.IFRAME, "column-2", -1); 
       log("portletId: "+portletId); 
       long companyId = themeDisplay.getCompanyId(); 
       long ownerId = PortletKeys.PREFS_OWNER_ID_DEFAULT; 
       int ownerType = PortletKeys.PREFS_OWNER_TYPE_LAYOUT; 
       Layout layout = themeDisplay.getLayout(); 
       PortletPreferences prefs = PortletPreferencesLocalServiceUtil.getPreferences(companyId, ownerId, ownerType, layout.getPlid(), portletId); 
       Locale locale = new Locale("en", "US"); 
       prefs.setValue("portlet-setup-use-custom-title", "true"); 

       if(c.equals("g")) { 
        prefs.setValue("src","http://www.google.com"); 
        prefs.setValue("portlet-setup-title-" + LocaleUtil.toLanguageId(locale), "Google"); 
       } 
       else if(c.equals("y")) { 
        prefs.setValue("src","http://www.yahoo.com"); 
        prefs.setValue("portlet-setup-title-" + LocaleUtil.toLanguageId(locale), "Yahoo"); 
       } 
       else if(c.equals("d")) { 
        prefs.setValue("src", "http://www.dailytech.com"); 
        prefs.setValue("portlet-setup-title-" + LocaleUtil.toLanguageId(locale), "Daily Tech"); 
       } 
       else if(c.equals("w")) { 
        prefs.setValue("src", "http://www.wired.com"); 
        prefs.setValue("portlet-setup-title-" + LocaleUtil.toLanguageId(locale), "Wired"); 
       } 

       PortletPreferencesLocalServiceUtil.updatePreferences(ownerId, ownerType, layout.getPlid(), portletId, prefs); 

       portletList = layoutTypePortlet.getPortlets(); 
       for(Portlet p : portletList) { 
        if(p.getPortletId().equals(portletId)) { 
         break; 
        } 
       } 

       LayoutLocalServiceUtil.updateLayout(layout.getGroupId(), layout.isPrivateLayout(), layout.getLayoutId(), layout.getTypeSettings()); 
      } 
     } 
     catch(Exception e) { 
      log("LinksPortlet:doView:Exception"); 
      e.printStackTrace(); 
     } 

     super.render(request, response); 
    } 

與上述實施的問題是,在重新加載頁面後,屏幕上出現了新的portlet: http://www.go-ahead.in/books/1.JPG http://www.go-ahead.in/books/2.JPG http://www.go-ahead.in/books/3.JPG

的原因的問題是,新的portlet在佈局渲染開始後添加。是否可以自定義或添加一個鉤子來執行我在頁面/佈局加載期間執行的操作。我遇到了添加portlet的liferay的源代碼LayoutAction.java。有沒有辦法添加一個post鉤子來添加portlet而不修改liferay的源代碼?

我使用的是Liferay 6.0.5。

在此先感謝。

回答

0

找到了解決問題的辦法。

我已將代碼從render方法移至processAction,並使用<portlet:actionURL>標記創建鏈接。