更新UI,创建一个用于管理留言簿的新JSP,并更新现有的JSP。
- 首先,更新依赖关系。在
guestbook-web
模块,打开/src/main/resources/META-INF/resources/
的init.jsp,添加以下依赖项:<%@ taglib uri="http://liferay.com/tld/frontend" prefix="liferay-frontend" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@ taglib uri="http://liferay.com/tld/security" prefix="liferay-security" %> <%@ page import="java.util.List" %> <%@ page import="com.liferay.portal.kernel.util.ParamUtil" %> <%@ page import="com.liferay.portal.kernel.util.HtmlUtil" %> <%@ page import="com.liferay.portal.kernel.util.StringPool" %> <%@ page import="com.liferay.portal.kernel.model.PersistedModel" %> <%@ page import="com.liferay.portal.kernel.dao.search.SearchEntry" %> <%@ page import="com.liferay.portal.kernel.dao.search.ResultRow" %> <%@ page import="com.liferay.docs.guestbook.model.Guestbook" %> <%@ page import="com.liferay.docs.guestbook.service.EntryLocalServiceUtil" %> <%@ page import="com.liferay.docs.guestbook.service.GuestbookLocalServiceUtil" %> <%@ page import="com.liferay.docs.guestbook.model.Entry" %>
- 从
/resources/META-INF/resources/guestbookwebportlet
打开view.jsp。将内容替换为以下代码:<%@include file="../init.jsp"%> <% long guestbookId = Long.valueOf((Long) renderRequest .getAttribute("guestbookId")); %> <aui:button-row cssClass="guestbook-buttons"> <portlet:renderURL var="addEntryURL"> <portlet:param name="mvcPath" value="/guestbookwebportlet/edit_entry.jsp" /> <portlet:param name="guestbookId" value="<%=String.valueOf(guestbookId)%>" /> </portlet:renderURL> <aui:button onClick="<%=addEntryURL.toString()%>" value="Add Entry"></aui:button> </aui:button-row> <liferay-ui:search-container total="<%=EntryLocalServiceUtil.getEntriesCount()%>"> <liferay-ui:search-container-results results="<%=EntryLocalServiceUtil.getEntries(scopeGroupId.longValue(), guestbookId, searchContainer.getStart(), searchContainer.getEnd())%>" /> <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>
This view.jsp
now retrieves the entries from the guestbook it gets from the render
method. It does this inside a Liferay Portal construct called a Search Container. This is a front-end component that makes it easy to display data in rows and columns. The EntryLocalServiceUtil
call retrieves the data from your new Service Builder-based back-end. Otherwise, this JSP is much the same: you still have an Add Entry button with its corresponding URL.
接下来,编辑edit_entry.jsp
:
- Open
edit_entry.jsp
and replace the existing code with this:<%@include file="../init.jsp" %> <% long entryId = ParamUtil.getLong(renderRequest, "entryId"); Entry entry = null; if (entryId > 0) { entry = EntryLocalServiceUtil.getEntry(entryId); } long guestbookId = ParamUtil.getLong(renderRequest, "guestbookId"); %> <portlet:renderURL var="viewURL"> <portlet:param name="mvcPath" value="/guestbookwebportlet/view.jsp"></portlet:param> </portlet:renderURL> <portlet:actionURL name="addEntry" var="addEntryURL"></portlet:actionURL> <aui:form action="<%= addEntryURL %>" name="<portlet:namespace />fm"> <aui:model-context bean="<%= entry %>" model="<%= Entry.class %>" /> <aui:fieldset> <aui:input name="name" /> <aui:input name="email" /> <aui:input name="message" /> <aui:input name="entryId" type="hidden" /> <aui:input name="guestbookId" type="hidden" value='<%= entry == null ? guestbookId : entry.getGuestbookId() %>'/> </aui:fieldset> <aui:button-row> <aui:button type="submit"></aui:button> <aui:button type="cancel" onClick="<%= viewURL.toString() %>"></aui:button> </aui:button-row> </aui:form>
虽然现在有更多的字段,但这是相同的形式。通过一些AlloyUI标签,表单链接到您的
Entry
实体。两个隐藏字段包含新留言簿entryId
和guestbookId
。submit按钮是一个ActionURL,它在控制器(您的portlet类)中执行addEntry方法。
现在已经成功地用真正的数据库驱动的后端替换了原型后端。接下来,部署您的应用程序。