Liferay7开发文档_3.4.4整合新的后端
参考:https://blog.csdn.net/qmkemail/article/details/80170401
现在可以将原型后端替换为使用Service Builder创建的真正数据库驱动的后端。
在原型手动创建了应用程序的模型。现在第一件事是删除它,因为Service Builder生成了一个新的:
- Find the
com.liferay.docs.guestbook.model
package in theguestbook-web
module. - Delete it. You’ll see errors in your project, but that’s because you haven’t replaced the model yet.
现在可以做一些依赖管理。要让Web模块访问生成的服务,必须使其知道API和服务模块。然后,修改GuestbookPortlet
的addEntry
方法:
- First, open
guestbook-web
’sbuild.gradle
file and add these dependencies:
compileOnly project(":modules:guestbook:guestbook-api")
compileOnly project(":modules:guestbook:guestbook-service")
2.Right-click on the guestbook-web
project and select Gradle → Refresh Gradle Project.
3.Now you must add references to the Service Builder services you need. To do this, add them as class variables with @Reference
annotations on their setter methods. Open GuestbookPortlet
and add these references to the bottom of the file:
@Reference(unbind = "-") protected void setEntryService(EntryLocalService entryLocalService) { _entryLocalService = entryLocalService; } @Reference(unbind = "-") protected void setGuestbookService(GuestbookLocalService guestbookLocalService) { _guestbookLocalService = guestbookLocalService; } private EntryLocalService _entryLocalService; private GuestbookLocalService _guestbookLocalService;
Note that it’s Liferay’s code style to add class variables this way. The @Reference
annotation on the setters allows Liferay’s OSGi container to inject references to your generated services so you can use them. The unbind
parameter tells the container there’s no method for unbinding these services: the references can die with the class during garbage collection when they’re no longer needed.
注意,用这种方式添加类变量是Liferay的代码风格。setters上的@Reference注释允许Liferay的OSGi容器为您生成的服务注入引用,以便使用它们。unbind参数告诉容器没有对这些服务进行解绑定的方法:当不再需要时,引用可以在垃圾收集期间与类一起销毁。
4.Now you can modify the addEntry
method to use these service references:
public void addEntry(ActionRequest request, ActionResponse response) throws PortalException { ServiceContext serviceContext = ServiceContextFactory.getInstance( Entry.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"); if (entryId > 0) { try { _entryLocalService.updateEntry( serviceContext.getUserId(), guestbookId, entryId, userName, email, message, serviceContext); SessionMessages.add(request, "entryAdded"); response.setRenderParameter( "guestbookId", Long.toString(guestbookId)); } catch (Exception e) { System.out.println(e); SessionErrors.add(request, e.getClass().getName()); PortalUtil.copyRequestParameters(request, response); response.setRenderParameter( "mvcPath", "/guestbookwebportlet/edit_entry.jsp"); } } else { try { _entryLocalService.addEntry( serviceContext.getUserId(), guestbookId, userName, email, message, serviceContext); SessionMessages.add(request, "entryAdded"); response.setRenderParameter( "guestbookId", Long.toString(guestbookId)); } catch (Exception e) { SessionErrors.add(request, e.getClass().getName()); PortalUtil.copyRequestParameters(request, response); response.setRenderParameter( "mvcPath", "/guestbookwebportlet/edit_entry.jsp"); } } }
-
This
addEntry
method gets the name, message, and email fields that the user submits in the JSP and passes them to the service to be stored as entry data. Theif-else
logic checks whether there’s an existingentryId
. If there is, the update service method is called, and if not, the add service method is called. In both cases, it sets a render parameter with the Guestbook ID so the application can display the guestbook’s entries after this one has been added. This is all done intry...catch
statements.addEntry方法获取用户在JSP中提交的名称、消息和电子邮件字段,并将它们传递给服务作为输入数据存储。
if-else逻辑检查是否存在一个现有的entryId。
如果是,则调用update service方法,如果否,则调用add service方法。
在这两种情况下,它都使用Guestbook ID设置一个呈现参数,这样应用程序就可以在添加了这一项之后显示Guestbook的条目。
这都在try…catch语句中完成。 - Now add
deleteEntry
, which you didn’t have before:
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( Entry.class.getName(), request); try { response.setRenderParameter( "guestbookId", Long.toString(guestbookId)); _entryLocalService.deleteEntry(entryId, serviceContext); } catch (Exception e) { Logger.getLogger(GuestbookPortlet.class.getName()).log( Level.SEVERE, null, e); } }
-
This method retrieves the entry object (using its ID from the request) and calls the service to delete it.
该方法检索条目对象(从请求中使用它的ID),并调用服务来删除它。
- Next you must replace the
render
method:
@Override public void render(RenderRequest renderRequest, RenderResponse renderResponse) throws IOException, PortletException { 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); if (guestbooks.isEmpty()) { Guestbook guestbook = _guestbookLocalService.addGuestbook( serviceContext.getUserId(), "Main", serviceContext); guestbookId = guestbook.getGuestbookId(); } if (guestbookId == 0) { guestbookId = guestbooks.get(0).getGuestbookId(); } renderRequest.setAttribute("guestbookId", guestbookId); } catch (Exception e) { throw new PortletException(e); } super.render(renderRequest, renderResponse); }
-
This new
render
method checks for any guestbooks in the current site. If there aren’t any, it creates one. Either way, it grabs the first guestbook so its entries can be displayed by your view layer.这个新的呈现方法检查当前站点中的任何guestbook。如果没有,就创建一个。无论哪种方式,它都可以获取第一个guestbook,这样它的条目就可以被您的视图层显示出来。
- Remove the
parseEntries
method. It’s a remnant of the prototype application. - Hit Ctrl-Shift-O to organize your imports.