Liferay7开发文档_3.2.7实现PORTLET ACTION

实现PORTLET ACTION

当用户提交表单时,应用程序保存表单数据以在留言簿中显示。为了保持简单,将使用Portlet Preferences API实现此功能。(通常情况下会使用数据库。Liferay Portal的Service Builder工具消除了处理数据库的大量复杂性。) 但现在先使用Portlet Preferences,实现留言簿应用程序的第一次迭代。

要使portlet执行除re-render 之外的任何操作,必须实现portlet action。 An action defines some processing, usually based on user input, that the portlet must perform before it renders itself. 对于留言板portlet,接下来要实现的action会保存用户键入到表单中的留言簿条目。保存的留言簿条目可以被检索并在稍后显示。

使用Liferay’s MVC Portlet框架,可以轻松实现action 。 Portlet actions在portlet类中实现。刚刚创建的表单中,新建了一个action URL,称为addEntry。要创建Portlet action,需要在Portlet类中建立一个名称相同的方法。MVCPortlet在用户触发其匹配的URL时调用该方法。

  1. 打开GuestbookPortlet。项目模板在portlet工程时生成此类。
  2. 创建一个具有以下签名的方法:
    public void addEntry(ActionRequest request, ActionResponse response) {
    
    }
    
  3. Press [CTRL]+[SHIFT]+O to organize imports and import the required javax.portlet.ActionRequest and javax.portlet.ActionResponse classes.

现在已经创建了一个Portlet action。它不能做任何事情,但现在提交表单,至少不报错。接下来执行保存表单数据action。

由于portlet preferences API的限制,必须将每个guestbook条目存储为String,一个字符串数组。由于您的表单有两个字段,因此必须使用分隔符来确定用户名结尾以及留言簿条目的开始位置。插入符号(^)可以很好地区分分隔符,因为用户不太可能在留言簿条目中使用该符号。

以下方法实现将guestbook条目添加到名为的portlet preferences guestbook-entries

public void addEntry(ActionRequest request, ActionResponse response) {
    try {
        PortletPreferences prefs = request.getPreferences();

        String[] guestbookEntries = prefs.getValues("guestbook-entries",
                new String[1]);

        ArrayList<String> entries = new ArrayList<String>();

        if (guestbookEntries[0] != null) {
            entries = new ArrayList<String>(Arrays.asList(prefs.getValues(
                    "guestbook-entries", new String[1])));
        }

        String userName = ParamUtil.getString(request, "name");
        String message = ParamUtil.getString(request, "message");
        String entry = userName + "^" + message;

        entries.add(entry);

        String[] array = entries.toArray(new String[entries.size()]);

        prefs.setValues("guestbook-entries", array);

        try {
            prefs.store();
        }
        catch (IOException ex) {
            Logger.getLogger(GuestbookPortlet.class.getName()).log(
                    Level.SEVERE, null, ex);
        }
        catch (ValidatorException ex) {
            Logger.getLogger(GuestbookPortlet.class.getName()).log(
                    Level.SEVERE, null, ex);
        }

    }
    catch (ReadOnlyException ex) {
        Logger.getLogger(GuestbookPortlet.class.getName()).log(
                Level.SEVERE, null, ex);
    }
}
  1. Replace your existing addEntry method with the above method.
  2. Press [CTRL]+[SHIFT]+O to organize imports and select the javax.portlet.PortletPreferences and java.util.logging.Logger when prompted (not their Liferay equivalents).

First, the preferences are retrieved. Then the guestbook-entries preference is retrieved and converted to an ArrayList so that you can add an entry without worrying about exceeding the size of the array. Next, the name and message fields from your form are retrieved. Note that Liferay’s ParamUtil class makes it very easy to retrieve URL parameters.

Finally, the fields are combined into a String delimited by a caret, and the new entry is added to the ArrayList, which is then converted back to an array so it can be stored as a preference. The try/catch blocks are required by the portlet preferences API.

这并非使用portlet  preferences的正常方式,但为存储留言簿的第一个版本提供了一种快捷方便的方法。在稍后的步骤中,将实施一种可靠的方式将访客留言条目存储在数据库中。

下一个,也是最后一个要实现的功能是查看留言簿条目。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值