1. 程式人生 > 實用技巧 >Liferay7開發文件_3.4.4整合新的後端

Liferay7開發文件_3.4.4整合新的後端

參考:https://blog.csdn.net/qmkemail/article/details/80170401

整合新的後端

現在可以將原型後端替換為使用Service Builder建立的真正資料庫驅動的後端。

在原型手動建立了應用程式的模型。現在第一件事是刪除它,因為Service Builder生成了一個新的:

  1. Find thecom.liferay.docs.guestbook.modelpackage in theguestbook-webmodule.
  2. Delete it. You’ll see errors in your project, but that’s because you haven’t replaced the model yet.

現在可以做一些依賴管理。要讓Web模組訪問生成的服務,必須使其知道API和服務模組。然後,修改GuestbookPortletaddEntry方法:

  1. First, openguestbook-web’sbuild.gradlefile and add these dependencies:

    compileOnly project(":modules:guestbook:guestbook-api")
    compileOnly project(":modules:guestbook:guestbook-service")

   2.Right-click on theguestbook-web

project and selectGradle→Refresh Gradle Project.

   3.Now you must addreferencesto the Service Builder services you need. To do this, add them as class variables with@Referenceannotations on their setter methods. OpenGuestbookPortletand 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@Referenceannotation on the setters allows Liferay’s OSGi container to inject references to your generated services so you can use them. Theunbindparameter 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 theaddEntrymethod 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");
        }
    }
}

  1. ThisaddEntrymethod 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-elselogic 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...catchstatements.

    addEntry方法獲取使用者在JSP中提交的名稱、訊息和電子郵件欄位,並將它們傳遞給服務作為輸入資料儲存。
    if-else邏輯檢查是否存在一個現有的entryId。
    如果是,則呼叫update service方法,如果否,則呼叫add service方法。
    在這兩種情況下,它都使用Guestbook ID設定一個呈現引數,這樣應用程式就可以在添加了這一項之後顯示Guestbook的條目。
    這都在try…catch語句中完成。

  2. Now adddeleteEntry, 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);
        }
}

  

  1. This method retrieves the entry object (using its ID from the request) and calls the service to delete it.

    該方法檢索條目物件(從請求中使用它的ID),並呼叫服務來刪除它。

  2. Next you must replace therendermethod:
    
@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);
}

  1. This newrendermethod 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,這樣它的條目就可以被您的檢視層顯示出來。

  2. Remove theparseEntriesmethod. It’s a remnant of the prototype application.
  3. Hit Ctrl-Shift-O to organize your imports.