第一个liferay portlet:guestbook-portlet

2016/02/17 09:03
阅读数 108

0. 效果图

    https://dev.liferay.com/documents/12052/447312/my-first-app.png/1bc55b63-9d5d-40fc-a758-c4777755d3ab?version=1.0&t=1441467971000

1. 新建项目: Life Plugin Project

    https://dev.liferay.com/documents/12052/447312/lds-new-liferay-plugin-project-1.png/4b7e52c6-0040-4c48-9537-d04b812f033b?version=1.0&t=1441467972000 (勾选Launch New Portlet..)

2. 新建Liferay Portlet

    https://dev.liferay.com/documents/12052/447312/lds-new-portlet-wizard.png/63a43d7b-0d8a-4551-b3cd-0c98b2f1a7bf?version=1.0&t=1441467972000

3. 项目结构:

    


4. 代码:(view + controller + model)

    1)view.jsp

<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%@ taglib uri="http://alloy.liferay.com/tld/aui" prefix="aui" %>
<%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui" %>
<portlet:defineObjects />
<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>

    <aui:button-row  cssClass="guestbook-buttons">    
    
            <portlet:renderURL var="addEntryURL">   
                    <portlet:param name="mvcPath" value="/html/guestbook/edit_entry.jsp"></portlet:param>
            </portlet:renderURL>   
    
            <aui:button onClick="<%= addEntryURL.toString() %>" value="Add Entry"></aui:button>
        
    </aui:button-row>

    2)edit_entry.jsp

<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%@ taglib uri="http://alloy.liferay.com/tld/aui" prefix="aui" %>
<portlet:defineObjects />

        <portlet:renderURL var="viewURL">
                <portlet:param name="mvcPath" value="/html/guestbook/view.jsp"></portlet:param>
        </portlet:renderURL>
        
        <portlet:actionURL name="addEntry" var="addEntryURL"></portlet:actionURL>
        
        <aui:form action="<%= addEntryURL %>" name="<portlet:namespace />fm">
                
                <aui:fieldset>
                        <aui:input name="name"></aui:input>
                        <aui:input name="message"></aui:input>
                </aui:fieldset>
                
                <aui:button-row> 
                        <aui:button type="submit"></aui:button> 
                        <aui:button onClick="<%= viewURL.toString() %>" type="cancel"></aui:button>
                </aui:button-row>
        
        </aui:form>

    3)GuestbookPortlet.java

package com.liferay.docs.guestbook.portlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;
import javax.portlet.ReadOnlyException;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.portlet.ValidatorException;
import javax.portlet.PortletPreferences;

import com.liferay.docs.guestbook.model.Entry;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.util.bridges.mvc.MVCPortlet;

/**
* Portlet implementation class GuestbookPortlet
*/

public class GuestbookPortlet extends MVCPortlet{
    
    //1.
    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 != 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);
    
            } 
        }
    
        //
    @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 != null) {

            List<Entry> entries = parseEntries(guestbookEntries);

            renderRequest.setAttribute("entries", entries);
        }

        super.render(renderRequest, renderResponse);

    }
    
    //
    private List<Entry> parseEntries (String[] guestbookEntries) {

        ArrayList<Entry> entries = new ArrayList();

        for (String entry : guestbookEntries) {
            String[] parts = entry.split("\\^", 2);
            Entry gbEntry = new Entry(parts[0], parts[1]);
            entries.add(gbEntry);
        }

        return entries;
    }
    
    }
    
    
    //2.

    4)Entry.java

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 void setName(String name) {
            this.name = name;
        }
        public String getMessage() {
            return message;
        }
        public void setMessage(String message) {
            this.message = message;
        }
        
        
}

5. 部署(deploy)到http://localhost:8080:

    https://dev.liferay.com/documents/12052/447312/lds-deploy-portlet.png/f60c70dd-bb47-433a-b7e0-fb8f479a8eed?version=1.0&t=1441467972000

6. 结果:

    

展开阅读全文
打赏
1
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
1
分享
返回顶部
顶部