Liferay7开发文档_3.2.8显示留言条目

显示留言条目

显示留言簿条目:从portlet preferences中提取数据,循环访问数据并呈现在页面上。使用MVC Portlet实现Model-View-Controller是最佳方式。已经有视图(JSP文件)和控制器(portlet类)。现在需要建立模型。

创建你的模型

  1. Create a new package called com.liferay.docs.guestbook.model. To do this, right-click your src/main/javafolder and select New → Package. Then enter the package name in the dialog box that appears.
  2. Next, create your model class. This is a simple class that models a guestbook entry. To do this, right-click your new package and select New → Class. Name the class Entry, and click Finish.

    You now have a Java class for your guestbook entries. Next, you’ll give it the fields you need to store entries.

  3. Create two private String variables: name and message.
    private String name;
    private String message;
    
  4. Right-click a blank area of the editor and select Source → Generate Getters and Setters. Click Select All in the dialog that pops up, and then click OK.
  5. Next, provide two constructors: one that initializes the class with no values for the two fields, and one that takes the two fields as parameters and sets their values:
    public Entry() {
       this.name = null;
       this.message = null;
    }
    
    public Entry(String name, String message) {
       setName(name);
       setMessage(message);
    }

完成后的模型类像这样:

package com.liferay.docs.guestbook.model;

public class Entry {

    private String name;
    private String message;

    public Entry() {
        this.name = null;
        this.message = null;
    }

    public Entry(String name, String message) {
        setName(name);
        setMessage(message);
    }

    public String getName() {
        return name;
    }

    public String getMessage() {
        return message;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setMessage(String message) {
        this.message = message;
    }

}

现在建立好了模型,你可以简单地封装留言条目,这样他们就可以被控制层处理,并被视图层显示出来。下一步是增强控制器(您的Portlet类),以便在用户看到留言簿应用程序时处理留言簿条目并准备显示。

自定义您的应用程序呈现方式

如前所述,您的应用程序使用两个portlet阶段: render and action。为了让留言簿在用户查看应用程序时显示保存的留言簿条目,需要重新实现MVCPortlet子类的render功能。

  1. Open GuestbookPortlet and add the following method below your addEntry method:
    @Override
    public void render(RenderRequest renderRequest, RenderResponse renderResponse)
        throws PortletException, IOException {
    
        PortletPreferences prefs = renderRequest.getPreferences();
        String[] guestbookEntries = prefs.getValues("guestbook-entries", new String[1]);
    
        if (guestbookEntries[0] != null) {
            List<Entry> entries = parseEntries(guestbookEntries);
            renderRequest.setAttribute("entries", entries);
        }
    
        super.render(renderRequest, renderResponse);
    }
    

    This method retrieves the guestbook entries from the configuration, converts it to a List of Entry objects, and places that List into the request object. It then calls the parent class’s render method.

  2. render方法之下,添加将数组转换为List模型对象的方法:
    private List<Entry> parseEntries(String[] guestbookEntries) {
        List<Entry> entries = new ArrayList<Entry>();
    
        for (String entry : guestbookEntries) {
            String[] parts = entry.split("\\^", 2);
            Entry gbEntry = new Entry(parts[0], parts[1]);
            entries.add(gbEntry);
        }
    
        return entries;
    }
    
  3. Press [CTRL]+[SHIFT]+O to organize imports.

如你所见,这个方法String根据caret(^)字符将数组中的条目分成两部分。

现在您的控制器准备好要显示的数据,下一步就是实现该视图,以便用户可以看到留言条目。

显示留言条目

Liferay的开发框架可以很容易地循环访问数据,并很好地显示给最终用户。您将使用名为Search Container的Liferay UI构造来实现此目的。

  1. Add these tags to your view.jsp in between the </portlet:renderURL> and <aui:button-row> tags
    <jsp:useBean id="entries" class="java.util.ArrayList" scope="request"/>
    
    <liferay-ui:search-container>
        <liferay-ui:search-container-results results="<%= entries %>" />
    
        <liferay-ui:search-container-row
            className="com.liferay.docs.guestbook.model.Entry"
            modelVar="entry"
        >
            <liferay-ui:search-container-column-text property="message" />
    
            <liferay-ui:search-container-column-text property="name" />
        </liferay-ui:search-container-row>
    
        <liferay-ui:search-iterator />
    </liferay-ui:search-container>
    

保存,部署应用程序,尝试添加一些留言条目。

原型开发完成。

下一步,介绍使用Service Builder来生成持久类,以及将应用程序数据存储在数据库中所需的方法。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值