Liferay7开发文档_3.5.4定义PORTLET ACTIONS

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并按照以下步骤操作:

  1. 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方法,因此需要ActionRequestActionResponse参数。服务调用时,须从请求中检索留言簿的名称。还须从请求中检索serviceContext,并在服务调用中作为参数传递。如果抛出异常,则应该显示Add Guestbook表单,而不是默认视图。:

    response.setRenderParameter("mvcPath",
            "/guestbookadminportlet/edit_guestbook.jsp");
    

    稍后,您将使用它进行字段验证,并向用户显示错误消息。注意,/guestbookadminportlet/edit_guestbook.jsp目前尚不存在; 将在下一节设计Guestbook Admin portlet的用户界面时创建。

  2. 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来避免重复的代码。

  3. 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删除留言簿。由于该deleteGuestbook action是从Guestbook Admin portlet的默认视图中调用的,如果deleteGuestbook服务调用存在问题,则不需要设置mvcPath呈现参数来指向特定的JSP。

  4. Hit [CTRL]+[SHIFT]+O to organize imports. Save the file。

现在已经有了service methods和 portlet action methods。最后一项任务是实施Guestbook Admin portlet的用户界面。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值