Liferay7開發文檔_3.2.8顯示留言條目

顯示留言條目

顯示留言簿條目:從portlet preferences中提取數據,循環訪問數據並呈現在頁面上。使用MVC Portlet實現Model-View-Controller是最佳方式。已經有視圖(JSP文件)和控制器(portlet類)。現在需要建立模型。

創建你的模型

  1. Create a new package called com.liferay.docs.guestbook.model. To do this, right-click your src/main/javafolder and select New → Package. Then enter the package name in the dialog box that appears.
  2. Next, create your model class. This is a simple class that models a guestbook entry. To do this, right-click your new package and select New → Class. Name the class Entry, and click Finish.

    You now have a Java class for your guestbook entries. Next, you’ll give it the fields you need to store entries.

  3. Create two private String variables: name and message.
    private String name;
    private String message;
    
  4. Right-click a blank area of the editor and select Source → Generate Getters and Setters. Click Select All in the dialog that pops up, and then click OK.
  5. Next, provide two constructors: one that initializes the class with no values for the two fields, and one that takes the two fields as parameters and sets their values:
    public Entry() {
       this.name = null;
       this.message = null;
    }
    
    public Entry(String name, String message) {
       setName(name);
       setMessage(message);
    }

完成後的模型類像這樣:

package com.liferay.docs.guestbook.model;

public class Entry {

    private String name;
    private String message;

    public Entry() {
        this.name = null;
        this.message = null;
    }

    public Entry(String name, String message) {
        setName(name);
        setMessage(message);
    }

    public String getName() {
        return name;
    }

    public String getMessage() {
        return message;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setMessage(String message) {
        this.message = message;
    }

}

現在建立好了模型,你可以簡單地封裝留言條目,這樣他們就可以被控制層處理,並被視圖層顯示出來。下一步是增強控制器(您的Portlet類),以便在用戶看到留言簿應用程序時處理留言簿條目並準備顯示。

自定義您的應用程序呈現方式

如前所述,您的應用程序使用兩個portlet階段: render and action。爲了讓留言簿在用戶查看應用程序時顯示保存的留言簿條目,需要重新實現MVCPortlet子類的render功能。

  1. Open GuestbookPortlet and add the following method below your addEntry method:
    @Override
    public void render(RenderRequest renderRequest, RenderResponse renderResponse)
        throws PortletException, IOException {
    
        PortletPreferences prefs = renderRequest.getPreferences();
        String[] guestbookEntries = prefs.getValues("guestbook-entries", new String[1]);
    
        if (guestbookEntries[0] != null) {
            List<Entry> entries = parseEntries(guestbookEntries);
            renderRequest.setAttribute("entries", entries);
        }
    
        super.render(renderRequest, renderResponse);
    }
    

    This method retrieves the guestbook entries from the configuration, converts it to a List of Entry objects, and places that List into the request object. It then calls the parent class’s render method.

  2. render方法之下,添加將數組轉換爲List模型對象的方法:
    private List<Entry> parseEntries(String[] guestbookEntries) {
        List<Entry> entries = new ArrayList<Entry>();
    
        for (String entry : guestbookEntries) {
            String[] parts = entry.split("\\^", 2);
            Entry gbEntry = new Entry(parts[0], parts[1]);
            entries.add(gbEntry);
        }
    
        return entries;
    }
    
  3. Press [CTRL]+[SHIFT]+O to organize imports.

如你所見,這個方法String根據caret(^)字符將數組中的條目分成兩部分。

現在您的控制器準備好要顯示的數據,下一步就是實現該視圖,以便用戶可以看到留言條目。

顯示留言條目

Liferay的開發框架可以很容易地循環訪問數據,並很好地顯示給最終用戶。您將使用名爲Search Container的Liferay UI構造來實現此目的。

  1. Add these tags to your view.jsp in between the </portlet:renderURL> and <aui:button-row> tags
    <jsp:useBean id="entries" class="java.util.ArrayList" scope="request"/>
    
    <liferay-ui:search-container>
        <liferay-ui:search-container-results results="<%= entries %>" />
    
        <liferay-ui:search-container-row
            className="com.liferay.docs.guestbook.model.Entry"
            modelVar="entry"
        >
            <liferay-ui:search-container-column-text property="message" />
    
            <liferay-ui:search-container-column-text property="name" />
        </liferay-ui:search-container-row>
    
        <liferay-ui:search-iterator />
    </liferay-ui:search-container>
    

保存,部署應用程序,嘗試添加一些留言條目。

原型開發完成。

下一步,介紹使用Service Builder來生成持久類,以及將應用程序數據存儲在數據庫中所需的方法。

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