GuestbookPortlet_02
接着上一篇
修改GuestbookEntryServiceImpl類 代碼如下
/**
* Copyright (c) 2000-present Liferay, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*/
package com.liferay.docs.guestbook.service.impl;
import com.liferay.docs.guestbook.exception.GuestbookEntryEmailException;
import com.liferay.docs.guestbook.exception.GuestbookEntryMessageException;
import com.liferay.docs.guestbook.exception.GuestbookEntryNameException;
import com.liferay.docs.guestbook.model.GuestbookEntry;
import com.liferay.docs.guestbook.service.base.GuestbookEntryServiceBaseImpl;
import com.liferay.portal.aop.AopService;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.model.User;
import com.liferay.portal.kernel.service.ServiceContext;
import com.liferay.portal.kernel.util.OrderByComparator;
import com.liferay.portal.kernel.util.Validator;
import org.osgi.service.component.annotations.Component;
import java.util.Date;
import java.util.List;
/**
* The implementation of the guestbook entry remote service.
*
* <p>
* All custom service methods should be put in this class. Whenever methods are added, rerun ServiceBuilder to copy their definitions into the <code>com.liferay.docs.guestbook.service.GuestbookEntryService</code> interface.
*
* <p>
* This is a remote service. Methods of this service are expected to have security checks based on the propagated JAAS credentials because this service can be accessed remotely.
* </p>
*
* @author liferay
* @see GuestbookEntryServiceBaseImpl
*/
@Component(
property = {
"json.web.service.context.name=gb",
"json.web.service.context.path=GuestbookEntry"
},
service = AopService.class
)
public class GuestbookEntryServiceImpl extends GuestbookEntryServiceBaseImpl {
/*
* NOTE FOR DEVELOPERS:
*
* Never reference this class directly. Always use <code>com.liferay.docs.guestbook.service.GuestbookEntryServiceUtil</code> to access the guestbook entry remote service.
*/
public GuestbookEntry addGuestbookEntry(long userId, long guestbookId, String name,
String message, ServiceContext serviceContext)
throws PortalException {
long groupId = serviceContext.getScopeGroupId();
User user = userLocalService.getUserById(userId);
Date now = new Date();
validate(name, message);
long entryId = counterLocalService.increment();
GuestbookEntry entry = guestbookEntryPersistence.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.setMessage(message);
guestbookEntryPersistence.update(entry);
// Calls to other Liferay frameworks go here
return entry;
}
public GuestbookEntry updateGuestbookEntry(long userId, long guestbookId,
long entryId, String name, String message,
ServiceContext serviceContext)
throws PortalException, SystemException {
Date now = new Date();
validate(name, message);
GuestbookEntry entry =
guestbookEntryPersistence.findByPrimaryKey(entryId);
User user = userLocalService.getUserById(userId);
entry.setUserId(userId);
entry.setUserName(user.getFullName());
entry.setModifiedDate(serviceContext.getModifiedDate(now));
entry.setName(name);
entry.setMessage(message);
entry.setExpandoBridgeAttributes(serviceContext);
System.out.println("===entry=="+entry.getName());
guestbookEntryPersistence.update(entry);
// Integrate with Liferay frameworks here.
return entry;
}
public GuestbookEntry deleteGuestbookEntry(GuestbookEntry entry)
throws PortalException {
guestbookEntryPersistence.remove(entry);
return entry;
}
public GuestbookEntry deleteGuestbookEntry(long entryId) throws PortalException {
GuestbookEntry entry =
guestbookEntryPersistence.findByPrimaryKey(entryId);
return deleteGuestbookEntry(entry);
}
public List<GuestbookEntry> getGuestbookEntries(long groupId, long guestbookId) {
return guestbookEntryPersistence.findByG_G(groupId, guestbookId);
}
public List<GuestbookEntry> getGuestbookEntries(long groupId, long guestbookId,
int start, int end) throws SystemException {
return guestbookEntryPersistence.findByG_G(groupId, guestbookId, start,
end);
}
public List<GuestbookEntry> getGuestbookEntries(long groupId, long guestbookId,
int start, int end, OrderByComparator<GuestbookEntry> obc) {
return guestbookEntryPersistence.findByG_G(groupId, guestbookId, start,
end, obc);
}
public GuestbookEntry getGuestbookEntry(long entryId) throws PortalException {
return guestbookEntryPersistence.findByPrimaryKey(entryId);
}
public int getGuestbookEntriesCount(long groupId, long guestbookId) {
return guestbookEntryPersistence.countByG_G(groupId, guestbookId);
}
protected void validate(String name, String entry)
throws PortalException {
if (Validator.isNull(name)) {
throw new GuestbookEntryNameException();
}
if (Validator.isNull(entry)) {
throw new GuestbookEntryMessageException();
}
}
}
修改GuestbookLocalServiceImpl類 代碼如下
/**
* Copyright (c) 2000-present Liferay, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*/
package com.liferay.docs.guestbook.service.impl;
import com.liferay.docs.guestbook.exception.GuestbookNameException;
import com.liferay.docs.guestbook.model.Guestbook;
import com.liferay.docs.guestbook.service.base.GuestbookLocalServiceBaseImpl;
import com.liferay.portal.aop.AopService;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.model.User;
import com.liferay.portal.kernel.service.ServiceContext;
import com.liferay.portal.kernel.util.OrderByComparator;
import com.liferay.portal.kernel.util.Validator;
import org.osgi.service.component.annotations.Component;
import java.util.Date;
import java.util.List;
/**
* The implementation of the guestbook local service.
*
* <p>
* All custom service methods should be put in this class. Whenever methods are added, rerun ServiceBuilder to copy their definitions into the <code>com.liferay.docs.guestbook.service.GuestbookLocalService</code> interface.
*
* <p>
* This is a local service. Methods of this service will not have security checks based on the propagated JAAS credentials because this service can only be accessed from within the same VM.
* </p>
*
* @author liferay
* @see GuestbookLocalServiceBaseImpl
*/
@Component(
property = "model.class.name=com.liferay.docs.guestbook.model.Guestbook",
service = AopService.class
)
public class GuestbookLocalServiceImpl extends GuestbookLocalServiceBaseImpl {
/*
* NOTE FOR DEVELOPERS:
*
* Never reference this class directly. Use <code>com.liferay.docs.guestbook.service.GuestbookLocalService</code> via injection or a <code>org.osgi.util.tracker.ServiceTracker</code> or use <code>com.liferay.docs.guestbook.service.GuestbookLocalServiceUtil</code>.
*/
public Guestbook addGuestbook(long userId, String name,
ServiceContext serviceContext) throws PortalException, PortalException {
long groupId = serviceContext.getScopeGroupId();
User user = userLocalService.getUserById(userId);
Date now = new Date();
validate(name);
long guestbookId = counterLocalService.increment();
Guestbook guestbook = guestbookPersistence.create(guestbookId);
guestbook.setUuid(serviceContext.getUuid());
guestbook.setUserId(userId);
guestbook.setGroupId(groupId);
guestbook.setCompanyId(user.getCompanyId());
guestbook.setUserName(user.getFullName());
guestbook.setCreateDate(serviceContext.getCreateDate(now));
guestbook.setModifiedDate(serviceContext.getModifiedDate(now));
guestbook.setName(name);
guestbook.setExpandoBridgeAttributes(serviceContext);
guestbookPersistence.update(guestbook);
return guestbook;
}
public List<Guestbook> getGuestbooks(long groupId) {
return guestbookPersistence.findByGroupId(groupId);
}
public List<Guestbook> getGuestbooks(long groupId, int start, int end,
OrderByComparator<Guestbook> obc) {
return guestbookPersistence.findByGroupId(groupId, start, end, obc);
}
public List<Guestbook> getGuestbooks(long groupId, int start, int end) {
return guestbookPersistence.findByGroupId(groupId, start, end);
}
public int getGuestbooksCount(long groupId) {
return guestbookPersistence.countByGroupId(groupId);
}
protected void validate(String name) throws PortalException {
if (Validator.isNull(name)) {
throw new GuestbookNameException();
}
}
}
GuestbookPortlet類中代碼如下
package com.liferay.docs.guestbook.portlet.portlet;
import com.liferay.docs.guestbook.model.Guestbook;
import com.liferay.docs.guestbook.model.GuestbookEntry;
import com.liferay.docs.guestbook.portlet.constants.GuestbookPortletKeys;
import com.liferay.docs.guestbook.service.GuestbookEntryLocalService;
import com.liferay.docs.guestbook.service.GuestbookEntryService;
import com.liferay.docs.guestbook.service.GuestbookLocalService;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.portlet.bridges.mvc.MVCPortlet;
import com.liferay.portal.kernel.service.ServiceContext;
import com.liferay.portal.kernel.service.ServiceContextFactory;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.PortalUtil;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import javax.portlet.*;
import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* @author 18404
*/
@Component(
immediate = true,
property = {
"com.liferay.portlet.display-category=category.social",
"com.liferay.portlet.instanceable=false",
"com.liferay.portlet.scopeable=true",
"javax.portlet.display-name=Guestbook",
"javax.portlet.expiration-cache=0",
"javax.portlet.init-param.template-path=/",
"javax.portlet.init-param.view-template=/guestbook/view.jsp",
"javax.portlet.resource-bundle=content.Language",
"javax.portlet.security-role-ref=power-user,user",
"javax.portlet.supports.mime-type=text/html",
"javax.portlet.name=" + GuestbookPortletKeys.GUESTBOOK
},
service = Portlet.class
)
public class GuestbookPortlet extends MVCPortlet {
@Reference
private GuestbookEntryService _guestbookEntryService;
@Reference
private GuestbookLocalService _guestbookLocalService;
public void addEntry(ActionRequest request, ActionResponse response)
throws PortalException {
ServiceContext serviceContext = ServiceContextFactory.getInstance(
GuestbookEntry.class.getName(), request);
String userName = ParamUtil.getString(request, "name");
String email = ParamUtil.getString(request, "email");
String message = ParamUtil.getString(request, "message");
long guestbookId = ParamUtil.getLong(request, "guestbookId");
long entryId = ParamUtil.getLong(request, "entryId");
System.out.println("name===="+userName);
if (entryId > 0) {
try {
_guestbookEntryService.updateGuestbookEntry(
serviceContext.getUserId(), guestbookId, entryId, userName,
message, serviceContext);
response.setRenderParameter(
"guestbookId", Long.toString(guestbookId));
}
catch (Exception e) {
System.out.println(e);
PortalUtil.copyRequestParameters(request, response);
response.setRenderParameter(
"mvcPath", "/guestbook/edit_entry.jsp");
}
}
else {
try {
System.out.println("--進入保存--"+serviceContext.getUserId()+email);
_guestbookEntryService.addGuestbookEntry(serviceContext.getUserId(), guestbookId, userName,
message, serviceContext);
response.setRenderParameter(
"guestbookId", Long.toString(guestbookId));
}
catch (Exception e) {
System.out.println(e);
PortalUtil.copyRequestParameters(request, response);
response.setRenderParameter(
"mvcPath", "/guestbook/edit_entry.jsp");
}
}
}
public void deleteEntry(ActionRequest request, ActionResponse response) throws PortalException {
long entryId = ParamUtil.getLong(request, "entryId");
long guestbookId = ParamUtil.getLong(request, "guestbookId");
ServiceContext serviceContext = ServiceContextFactory.getInstance(
GuestbookEntry.class.getName(), request);
try {
response.setRenderParameter(
"guestbookId", Long.toString(guestbookId));
_guestbookEntryService.deleteGuestbookEntry(entryId);
}
catch (Exception e) {
Logger.getLogger(GuestbookPortlet.class.getName()).log(
Level.SEVERE, null, e);
}
}
@Override
public void render(RenderRequest renderRequest, RenderResponse renderResponse)
throws IOException, PortletException {
System.out.println("進入搜========");
try {
ServiceContext serviceContext = ServiceContextFactory.getInstance(
Guestbook.class.getName(), renderRequest);
long groupId = serviceContext.getScopeGroupId();
long guestbookId = ParamUtil.getLong(renderRequest, "guestbookId");
List<Guestbook> guestbooks = _guestbookLocalService.getGuestbooks(
groupId);
System.out.println("搜索結果========"+guestbooks.get(0));
if (guestbooks.isEmpty()) {
Guestbook guestbook = _guestbookLocalService.addGuestbook(
serviceContext.getUserId(), "Main", serviceContext);
guestbookId = guestbook.getGuestbookId();
}
if (guestbookId == 0) {
guestbookId = guestbooks.get(0).getGuestbookId();
}
System.out.println("guestbookId===="+guestbookId);
renderRequest.setAttribute("guestbookId", guestbookId);
}
catch (Exception e) {
throw new PortletException(e);
}
super.render(renderRequest, renderResponse);
}
}
接下來創建jsp頁面
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="/guestbook/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="<%=GuestbookEntryLocalServiceUtil.getGuestbookEntriesCount()%>">
<liferay-ui:search-container-results
results="<%=GuestbookEntryLocalServiceUtil.getGuestbookEntries(
searchContainer.getStart(),
searchContainer.getEnd())%>" />
<liferay-ui:search-container-row
className="com.liferay.docs.guestbook.model.GuestbookEntry" modelVar="entry">
<liferay-ui:search-container-column-text property="message" />
<liferay-ui:search-container-column-text property="name" />
<liferay-ui:search-container-column-jsp
align="right"
path="/guestbook/entry_actions.jsp" />
</liferay-ui:search-container-row>
<liferay-ui:search-iterator />
</liferay-ui:search-container>
entry_actions.jsp
<%@include file="../init.jsp"%>
<%@ page import="com.liferay.portal.kernel.util.ParamUtil" %>
<%@ page import="com.liferay.portal.kernel.dao.search.ResultRow" %>
<%@ page import="com.liferay.docs.guestbook.model.GuestbookEntry" %>
<%@ page import="com.liferay.portal.kernel.util.WebKeys" %>
<%
String mvcPath = ParamUtil.getString(request, "mvcPath");
ResultRow row = (ResultRow)request.getAttribute(WebKeys.SEARCH_CONTAINER_RESULT_ROW);
GuestbookEntry entry = (GuestbookEntry)row.getObject();
%>
<liferay-ui:icon-menu>
<portlet:renderURL var="editURL">
<portlet:param name="entryId"
value="<%= String.valueOf(entry.getEntryId()) %>" />
<portlet:param name="mvcPath" value="/guestbook/edit_entry.jsp" />
</portlet:renderURL>
<liferay-ui:icon image="edit" message="Edit"
url="<%=editURL.toString() %>" />
<portlet:actionURL name="deleteEntry" var="deleteURL">
<portlet:param name="entryId"
value="<%= String.valueOf(entry.getEntryId()) %>" />
<portlet:param name="guestbookId"
value="<%= String.valueOf(entry.getGuestbookId()) %>" />
</portlet:actionURL>
<liferay-ui:icon-delete url="<%=deleteURL.toString() %>" />
</liferay-ui:icon-menu>
edit_entry.jsp
<%@ page import="com.liferay.portal.kernel.util.ParamUtil" %>
<%@ page import="com.liferay.docs.guestbook.service.GuestbookEntryLocalServiceUtil" %>
<%@ page import="com.liferay.docs.guestbook.model.GuestbookEntry" %>
<%@ include file="../init.jsp" %>
<%
long entryId = ParamUtil.getLong(renderRequest, "entryId");
GuestbookEntry entry = null;
if (entryId > 0) {
entry = GuestbookEntryLocalServiceUtil.getGuestbookEntry(entryId);
}
long guestbookId = ParamUtil.getLong(renderRequest, "guestbookId");
%>
<portlet:renderURL var="viewURL">
<portlet:param name="mvcPath" value="/guestbook/view.jsp"></portlet:param>
</portlet:renderURL>
<portlet:actionURL name="addEntry" var="addEntryURL" />
<aui:form action="<%= addEntryURL %>" name="<portlet:namespace />fm">
<aui:model-context bean="<%= entry %>" model="<%= GuestbookEntry.class %>" />
<aui:fieldset>
<aui:input name="name" />
<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>
init.jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet"%>
<%@ taglib uri="http://liferay.com/tld/aui" prefix="aui"%>
<%@ taglib uri="http://liferay.com/tld/portlet" prefix="liferay-portlet"%>
<%@ taglib uri="http://liferay.com/tld/theme" prefix="liferay-theme"%>
<%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui"%>
<%@ taglib uri="http://liferay.com/tld/frontend" prefix="liferay-frontend" %>
<%@ 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.WebKeys" %>
<%@ page import="com.liferay.petra.string.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.GuestbookEntryLocalServiceUtil" %>
<%@ page import="com.liferay.docs.guestbook.service.GuestbookLocalServiceUtil" %>
<%@ page import="com.liferay.docs.guestbook.model.GuestbookEntry" %>
<liferay-theme:defineObjects />
<portlet:defineObjects />
好了代碼到此結束我們開始編譯
編譯之後將生成的jar放到osgi下的modules中
現在我們可以運行門戶了,直接可以把該插件添加到頁面上
完事,有哪塊不明白的歡迎留言