Guestbook Admin portlet需要adding, updating, and deleting 留言簿的操作方法。和 Guestbook portlet一样,action methods调用对应service methods。请注意,由于服务和应用程序都在同一个容器中运行,因此任何应用程序都可以调用Guestbook services。这是Liferay Portal基于OSGi架构的优势:不同的应用程序或模块可以调用其他模块发布的服务。如果一个服务被发布,可以通过@Reference使用。您将在Guestbook Admin portlet中利用这一点,使用Guestbook portlet (addGuestbook服务)的相同服务。
添加三个PORTLET ACTIONS
Guestbook Admin portlet允许管理员添加,更新和删除Guestbook对象。创建portlet actions满足要求。打开GuestbookAdminPortlet.java并按照以下步骤操作:
- Add the following action method and instance variables needed for adding a new guestbook:
public void addGuestbook(ActionRequest request, ActionResponse response) throws PortalException { ServiceContext serviceContext = ServiceContextFactory.getInstance( Guestbook.class.getName(), request); String name = ParamUtil.getString(request, "name"); try { _guestbookLocalService.addGuestbook( serviceContext.getUserId(), name, serviceContext); } catch (PortalException pe) { Logger.getLogger(GuestbookAdminPortlet.class.getName()).log( Level.SEVERE, null, pe); response.setRenderParameter( "mvcPath", "/guestbookadminportlet/edit_guestbook.jsp"); } } private GuestbookLocalService _guestbookLocalService; @Reference(unbind = "-") protected void setGuestbookService(GuestbookLocalService guestbookLocalService) { _guestbookLocalService = guestbookLocalService; }由于
addGuestbook是portlet action方法,因此需要ActionRequest和ActionResponse参数。服务调用时,须从请求中检索留言簿的名称。还须从请求中检索serviceContext,并在服务调用中作为参数传递。如果抛出异常,则应该显示Add Guestbook表单,而不是默认视图。:response.setRenderParameter("mvcPath", "/guestbookadminportlet/edit_guestbook.jsp");稍后,您将使用它进行字段验证,并向用户显示错误消息。注意,
/guestbookadminportlet/edit_guestbook.jsp目前尚不存在; 将在下一节设计Guestbook Admin portlet的用户界面时创建。 - Add the following action method for updating an existing guestbook:
public void updateGuestbook(ActionRequest request, ActionResponse response) throws PortalException { ServiceContext serviceContext = ServiceContextFactory.getInstance( Guestbook.class.getName(), request); String name = ParamUtil.getString(request, "name"); long guestbookId = ParamUtil.getLong(request, "guestbookId"); try { _guestbookLocalService.updateGuestbook( serviceContext.getUserId(), guestbookId, name, serviceContext); } catch (PortalException pe) { Logger.getLogger(GuestbookAdminPortlet.class.getName()).log( Level.SEVERE, null, pe); response.setRenderParameter( "mvcPath", "/guestbookadminportlet/edit_guestbook.jsp"); } }此方法将检索留言簿名称,ID和
serviceContext请求。updateGuestbook服务通过留言簿ID来标识要修改的留言簿。如果服务调用出现问题,则Guestbook Admin portlet 会再次显示留言表单,以便用户可以编辑表单并重新提交:response.setRenderParameter("mvcPath", "/guestbookadminportlet/edit_guestbook.jsp");请注意,Edit Guestbook表单使用与Add Guestbook表单相同的JSP来避免重复的代码。
- Add the following action method for deleting a guestbook:
public void deleteGuestbook(ActionRequest request, ActionResponse response) throws PortalException { ServiceContext serviceContext = ServiceContextFactory.getInstance( Guestbook.class.getName(), request); long guestbookId = ParamUtil.getLong(request, "guestbookId"); try { _guestbookLocalService.deleteGuestbook(guestbookId, serviceContext); } catch (PortalException pe) { Logger.getLogger(GuestbookAdminPortlet.class.getName()).log( Level.SEVERE, null, pe); } }此方法使用服务层通过ID删除留言簿。由于该
deleteGuestbookaction是从Guestbook Admin portlet的默认视图中调用的,如果deleteGuestbook服务调用存在问题,则不需要设置mvcPath呈现参数来指向特定的JSP。 - Hit [CTRL]+[SHIFT]+O to organize imports. Save the file。
现在已经有了service methods和 portlet action methods。最后一项任务是实施Guestbook Admin portlet的用户界面。
本文介绍如何为GuestbookAdminportlet定义添加、更新和删除留言簿的方法。这些方法通过调用服务层实现,确保了数据的一致性和安全性。
850

被折叠的 条评论
为什么被折叠?



