显示留言簿条目:从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来生成持久类,以及将应用程序数据存储在数据库中所需的方法。