Liferay7開發文檔_3.2.7實現PORTLET ACTION

實現PORTLET ACTION

當用戶提交表單時,應用程序保存表單數據以在留言簿中顯示。爲了保持簡單,將使用Portlet Preferences API實現此功能。(通常情況下會使用數據庫。Liferay Portal的Service Builder工具消除了處理數據庫的大量複雜性。) 但現在先使用Portlet Preferences,實現留言簿應用程序的第一次迭代。

要使portlet執行除re-render 之外的任何操作,必須實現portlet action。 An action defines some processing, usually based on user input, that the portlet must perform before it renders itself. 對於留言板portlet,接下來要實現的action會保存用戶鍵入到表單中的留言簿條目。保存的留言簿條目可以被檢索並在稍後顯示。

使用Liferay’s MVC Portlet框架,可以輕鬆實現action 。 Portlet actions在portlet類中實現。剛剛創建的表單中,新建了一個action URL,稱爲addEntry。要創建Portlet action,需要在Portlet類中建立一個名稱相同的方法。MVCPortlet在用戶觸發其匹配的URL時調用該方法。

  1. 打開GuestbookPortlet。項目模板在portlet工程時生成此類。
  2. 創建一個具有以下簽名的方法:
    public void addEntry(ActionRequest request, ActionResponse response) {
    
    }
    
  3. Press [CTRL]+[SHIFT]+O to organize imports and import the required javax.portlet.ActionRequest and javax.portlet.ActionResponse classes.

現在已經創建了一個Portlet action。它不能做任何事情,但現在提交表單,至少不報錯。接下來執行保存表單數據action

由於portlet preferences API的限制,必須將每個guestbook條目存儲爲String,一個字符串數組。由於您的表單有兩個字段,因此必須使用分隔符來確定用戶名結尾以及留言簿條目的開始位置。插入符號(^)可以很好地區分分隔符,因爲用戶不太可能在留言簿條目中使用該符號。

以下方法實現將guestbook條目添加到名爲的portlet preferences guestbook-entries

public void addEntry(ActionRequest request, ActionResponse response) {
    try {
        PortletPreferences prefs = request.getPreferences();

        String[] guestbookEntries = prefs.getValues("guestbook-entries",
                new String[1]);

        ArrayList<String> entries = new ArrayList<String>();

        if (guestbookEntries[0] != null) {
            entries = new ArrayList<String>(Arrays.asList(prefs.getValues(
                    "guestbook-entries", new String[1])));
        }

        String userName = ParamUtil.getString(request, "name");
        String message = ParamUtil.getString(request, "message");
        String entry = userName + "^" + message;

        entries.add(entry);

        String[] array = entries.toArray(new String[entries.size()]);

        prefs.setValues("guestbook-entries", array);

        try {
            prefs.store();
        }
        catch (IOException ex) {
            Logger.getLogger(GuestbookPortlet.class.getName()).log(
                    Level.SEVERE, null, ex);
        }
        catch (ValidatorException ex) {
            Logger.getLogger(GuestbookPortlet.class.getName()).log(
                    Level.SEVERE, null, ex);
        }

    }
    catch (ReadOnlyException ex) {
        Logger.getLogger(GuestbookPortlet.class.getName()).log(
                Level.SEVERE, null, ex);
    }
}
  1. Replace your existing addEntry method with the above method.
  2. Press [CTRL]+[SHIFT]+O to organize imports and select the javax.portlet.PortletPreferences and java.util.logging.Logger when prompted (not their Liferay equivalents).

First, the preferences are retrieved. Then the guestbook-entries preference is retrieved and converted to an ArrayList so that you can add an entry without worrying about exceeding the size of the array. Next, the name and message fields from your form are retrieved. Note that Liferay’s ParamUtil class makes it very easy to retrieve URL parameters.

Finally, the fields are combined into a String delimited by a caret, and the new entry is added to the ArrayList, which is then converted back to an array so it can be stored as a preference. The try/catch blocks are required by the portlet preferences API.

這並非使用portlet  preferences的正常方式,但爲存儲留言簿的第一個版本提供了一種快捷方便的方法。在稍後的步驟中,將實施一種可靠的方式將訪客留言條目存儲在數據庫中。

下一個,也是最後一個要實現的功能是查看留言簿條目。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章