第一个liferay portlet:guestbook-portlet

摘要: 注:这个是官网的例子(官网有详细步骤,链接在最下面) liferay SDK:liferay-plugins-sdk-6.2 liferay portal:liferay-portal-6.2-ce-ga5 eclipse version=4.4.2

  1. 效果图

  2. 新建项目: Life Plugin Project

  3. 新建Liferay Portlet

  4. 项目结构:

  5. 代码:(view + controller + model)
    1)view.jsp

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    <%@ 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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<%@ 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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
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;
}
}

4)Entry.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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;
}
}

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

  2. 结果:

参考:
https://dev.liferay.com/develop/learning-paths/mvc/-/knowledge_base/6-2/writing-your-first-lifera…