2012-06-25 141 views
3

我試圖設置portlet首選項,而不需要觸摸portlet.xml。設置默認的portlet首選項

這裏是我的portlet:

-myportletclass 
-myportletjsp 
-myconclass 
-myconfjsp 

當我點擊的喜好,我可以看到confjsp,至極顯示形式爲我的喜好。而且,當我提交它時,將首選項設置爲表單中的值。

但我沒有任何默認值,沒有修改portlet.xml。

我要添加什麼和在哪裏?

我看到一些在配置類的渲染,但它不工作的方式。

問候。 謝謝。

編輯:我把我的喜好的方式:

第一,我有一個簡單的形式,我在我的conf JSP,基本的,沒有必要添加它; 然後,我有ConfController:

@Controller 
@RequestMapping("EDIT") 
public class ConfigurationController { 


    @ModelAttribute("validatePref") 
    public ValidatePrefForm getValidatePrefForm() { 
     ValidatePrefForm form = new ValidatePrefForm(); 
     return form; 
    } 

    @ActionMapping(params = "action=validatePref") 
    public void processAction(@ModelAttribute("validatePref") ValidatePrefForm form, 
      PortletPreferences portletPreferences, ActionResponse response) 
      throws Exception { 

     String mylink = form.getMylink(); 




     if (null != mylink && !mylink .equals("")) { 

      portletPreferences.setValue("mylink ", mylink); 

     } 

     portletPreferences.store(); 

    } 

    @RenderMapping 
    public String render() throws Exception { 
     return "myportletjsp.jsp"; 

    } 

} 

,然後在我的門戶我有這樣得到的數值:

mylink= preferences.getValue("mylink", mylink); 

     if(null==mylink){ 
      mylink= mydefaultUrl; 

     } 

,然後我可以使用myLink的

回答

6

首先: portlet.xml是擁有默認portlet首選項的好地方。

如果你絕對不希望這個由於某種原因,當然你可以檢查是否首選是null,然後假設你的默認值。無論您是在操作,渲染,事件還是資源階段檢索Portlet首選項,只需執行此操作即可。

+0

但在第二種方式,似乎當我添加一個新的portlet實例時,它保留了我爲portlet設置的首選項已經在頁面中。我該如何解決這個問題:當我在頁面中擁有portlet時,有默認值。謝謝。編輯:如果可能,我prefere方式不使用portlet.xml,但我不排除它。 –

+0

@Jean:你如何設定你的偏好?因爲我認爲portlet首選項是爲添加到頁面的每個portlet實例設置的,而不是整個portlet或用戶。 Liferay還爲首選項設置默認值,您可以檢查Liferay的內置Portlet的'init.jsp'(如果有源代碼的話)。 –

+0

我爲你編輯我的問題Prakash。 –