我有Liferay MVC portlet示例guestbook-portlet
正在运行
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>
当我呈现页面时,我在null
中获得addEntryURL
引用。
我的WEB-INF/src
portlet类定义了addEntry
公共方法:
public class NewPortlet extends MVCPortlet {
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(NewPortlet.class.getName()).log(
Level.SEVERE, null, ex);
} catch (ValidatorException ex) {
Logger.getLogger(NewPortlet.class.getName()).log(
Level.SEVERE, null, ex);
}
} catch (ReadOnlyException ex) {
Logger.getLogger(NewPortlet.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;
}
}
我有portlet.xml
配置的操作,如
<portlet-app xmlns="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd" version="2.0">
<portlet>
<portlet-name>guestbook</portlet-name>
<display-name>Guestbook</display-name>
<portlet-class>com.test.NewPortlet</portlet-class>
<init-param>
<name>view-template</name>
<value>/html/guestbook/view.jsp</value>
</init-param>
但是,当我点击按钮时,网址为http://localhost:8080/null
答案 0 :(得分:0)
我有和你一样的问题。
<aui:button onClick=...>
完全不能工作,尽管它是Liferay网站上的示例。
最后,我将<aui:button>
更改为普通的html按钮,它可以正常工作。
<button class="btn" type="button" onClick="location.href = '<%= addEntryURL.toString() %>'">Add Entry</button>