顯示留言條目
顯示留言簿條目:從portlet preferences中提取數據,循環訪問數據並呈現在頁面上。使用MVC Portlet實現Model-View-Controller是最佳方式。已經有視圖(JSP文件)和控制器(portlet類)。現在需要建立模型。
創建你的模型
- Create a new package called
com.liferay.docs.guestbook.model
. To do this, right-click yoursrc/main/java
folder and select New → Package. Then enter the package name in the dialog box that appears. - 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.
- Create two private String variables:
name
andmessage
.private String name; private String message;
- 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.
- 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功能。
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
ofEntry
objects, and places thatList
into the request object. It then calls the parent class’srender
method.- 在
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; }
- Press [CTRL]+[SHIFT]+O to organize imports.
如你所見,這個方法String
根據caret(^
)字符將數組中的條目分成兩部分。
現在您的控制器準備好要顯示的數據,下一步就是實現該視圖,以便用戶可以看到留言條目。
顯示留言條目
Liferay的開發框架可以很容易地循環訪問數據,並很好地顯示給最終用戶。您將使用名爲Search Container的Liferay UI構造來實現此目的。
- 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來生成持久類,以及將應用程序數據存儲在數據庫中所需的方法。