Jeg har et gammelt eksempel liggende.
fragment af portlet.xml:
<portlet>
<portlet-name>LinksPortlet</portlet-name>
<display-name>Link collection</display-name>
<portlet-class>test.LinksPortlet</portlet-class>
<init-param>
<name>jspView</name>
<value>/viewlinks.jsp</value>
</init-param>
<init-param>
<name>jspEdit</name>
<value>/editlinks.jsp</value>
</init-param>
<supports>
<mime-type>text/html</mime-type>
<portlet-mode>VIEW</portlet-mode>
<portlet-mode>EDIT</portlet-mode>
</supports>
<supported-locale>en</supported-locale>
<portlet-info>
<title>Links collection.</title>
<short-title>Links.</short-title>
<keywords>links web resources</keywords>
</portlet-info>
</portlet>
viewlinks.jsp:
<%@ page session="false" %>
<%@ page import="java.util.*" %>
<%@ page import="javax.portlet.*" %>
<%@ taglib uri="
http://java.sun.com/portlet" prefix="portlet" %>
<portlet:defineObjects/>
<ul>
<%
PortletPreferences prefs = renderRequest.getPreferences();
Enumeration e = prefs.getNames();
while(e.hasMoreElements()) {
String title = (String)e.nextElement();
String url = prefs.getValue(title, "*");
%>
<li><a href="<%=url%>"><%=title%></a></li>
<%
}
%>
</ul>
editlinks.jsp:
<%@ page session="false" %>
<%@ page import="javax.portlet.*" %>
<%@ taglib uri="
http://java.sun.com/portlet" prefix="portlet" %>
<jsp:useBean id="action" scope="request" class="java.lang.String"/>
<portlet:defineObjects/>
<form method="POST" action="<%=action%>">
Title: <input type="TEXT" name="title"/>
<br/>
URL: <input type="TEXT" name="url"/>
<br/>
<input type="SUBMIT" value="Save"/>
</form>
LinksServlet.java:
package test;
import javax.portlet.*;
import java.io.*;
public class LinksPortlet extends GenericPortlet
{
private final static String ADD = "add";
public void processAction(ActionRequest request, ActionResponse response) throws PortletException, IOException {
if(request.getParameter(ADD) != null) {
PortletPreferences prefs = request.getPreferences();
prefs.setValue(request.getParameter("title"), request.getParameter("url"));
prefs.store();
}
}
public void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException
{
response.setContentType("text/html");
String jspName = getPortletConfig().getInitParameter("jspView");
PortletRequestDispatcher rd = getPortletContext().getRequestDispatcher(jspName);
rd.include(request,response);
}
public void doEdit(RenderRequest request, RenderResponse response) throws PortletException, IOException
{
response.setContentType("text/html");
String jspName = getPortletConfig().getInitParameter("jspEdit");
PortletURL action = response.createActionURL();
action.setPortletMode(PortletMode.VIEW);
action.setParameter(ADD, "yes");
request.setAttribute("action", action.toString());
PortletRequestDispatcher rd = getPortletContext().getRequestDispatcher(jspName);
rd.include(request,response);
}
}