您已经从后端创建了一个完整的数据驱动应用程序。现在研究如何整合。
THE ENTRY
首先,在Service Builder的配置文件中定义了您的模型service.xml
。主要是Entry
对象:
<entity local-service="true" name="Entry" uuid="true">
<!-- PK fields -->
<column name="entryId" primary="true" type="long" />
<!-- Group instance -->
<column name="groupId" type="long" />
<!-- Audit fields -->
<column name="companyId" type="long" />
<column name="userId" type="long" />
<column name="userName" type="String" />
<column name="createDate" type="Date" />
<column name="modifiedDate" type="Date" />
<column name="name" type="String" />
<column name="email" type="String" />
<column name="message" type="String" />
<column name="guestbookId" type="long" />
<finder name="G_G" return-type="Collection">
<finder-column name="groupId" />
<finder-column name="guestbookId" />
</finder>
</entity>
接下来,创建一个服务实现类EntryLocalServiceImpl
,定义如何获取和存储条目。 Every field you defined was accounted for in the addEntry
method.
public Entry addEntry(long userId, long guestbookId, String name, String email,
String message, ServiceContext serviceContext)
throws PortalException {
long groupId = serviceContext.getScopeGroupId();
User user = userLocalService.getUserById(userId);
Date now = new Date();
validate(name, email, message);
long entryId = counterLocalService.increment();
Entry entry = entryPersistence.create(entryId);
entry.setUuid(serviceContext.getUuid());
entry.setUserId(userId);
entry.setGroupId(groupId);
entry.setCompanyId(user.getCompanyId());
entry.setUserName(user.getFullName());
entry.setCreateDate(serviceContext.getCreateDate(now));
entry.setModifiedDate(serviceContext.getModifiedDate(now));
entry.setExpandoBridgeAttributes(serviceContext);
entry.setGuestbookId(guestbookId);
entry.setName(name);
entry.setEmail(email);
entry.setMessage(message);
entryPersistence.update(entry);
return entry;
}
请注意,您在Service Builder中描述的所有字段(包括uuid
)都在这里保存。
添加获取条目的方法:
public List<Entry> getEntries(long groupId, long guestbookId) {
return entryPersistence.findByG_G(groupId, guestbookId);
}
public List<Entry> getEntries(
long groupId, long guestbookId, int start, int end, OrderByComparator<Entry> obc) {
return entryPersistence.findByG_G(groupId, guestbookId, start, end, obc);
}
public List<Entry> getEntries(long groupId, long guestbookId, int start, int end)
throws SystemException {
return entryPersistence.findByG_G(groupId, guestbookId, start, end);
}
在service.xml
定义groupId
和guestbookId
作为所述两个finder字段,在这些方法中调用持久层。
在这些工作后,Service Builder会将您的实现类方法反向写入到接口。然后,在portlet类中,创建了对Service Builder生成的服务类的引用,并使用这些引用访问该服务以添加条目:
_entryLocalService.addEntry( serviceContext.getUserId(), guestbookId,
userName, email,message, serviceContext);
最后,您将所有这些内容封装在一个用户界面中,用户界面允许用户输入他们想要的信息,并显示他们输入的数据。
现在您已经构建了应用程序,并且了解清楚它是如何工作的,该进行测试了。
部署和测试应用程序
- Drag and drop the
guestbook-api
module onto the server. - Drag and drop the
guestbook-service
module onto the server. - Look for the STARTED messages from the console.
- Go to your Liferay Portal instance at
localhost:8080
in your browser to test your updated application. - Click Add Entry.
- Enter a Name, Message, and Email Address.
- Click Submit.
- Verify that your entry appears.
下一步是什么?
已经创建了一个可用的Web应用程序并将其部署在Liferay Portal上。但是,它缺少一些重要功能:安全性,前端验证,以及创建多个留言簿的管理界面。在下一节中,将开始添加这些(以及更多)功能。