You are looking at documentation for an older release. Not what you want? See the current release documentation.
JSR-168 lets the implementations decide whether portlet preferences are user-specific or not.
In this example you will learn that portlet preferences in eXo are not user-specific. The source code of this example is here.
Create a new Maven project as follows:
Edit pom.xml
:
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.acme.samples</groupId>
<artifactId>hello-portlet</artifactId>
<version>1.0</version>
<packaging>war</packaging>
<build>
<finalName>hello-portlet</finalName>
</build>
<dependencies>
<dependency>
<groupId>javax.portlet</groupId>
<artifactId>portlet-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
Edit web.xml
:
<web-app>
<display-name>hello-portlet</display-name>
</web-app>
Edit HelloPortlet.java
:
package com.acme.samples;
import java.io.IOException;
import javax.portlet.GenericPortlet;
import javax.portlet.PortletRequestDispatcher;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.portlet.PortletException;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletMode;
import javax.portlet.PortletPreferences;
public class HelloPortlet extends GenericPortlet {
@Override
protected void doView(RenderRequest request, RenderResponse response) throws IOException, PortletException {
PortletRequestDispatcher dispatcher = getPortletContext().getRequestDispatcher("/jsp/view.jsp");
dispatcher.forward(request, response);
}
@Override
protected void doEdit(RenderRequest request, RenderResponse response) throws IOException, PortletException {
PortletRequestDispatcher dispatcher = getPortletContext().getRequestDispatcher("/jsp/edit.jsp");
dispatcher.forward(request, response);
}
@Override
public void processAction(ActionRequest request, ActionResponse response) throws IOException, PortletException {
String borderColor = request.getParameter("border_color");
PortletPreferences preferences = request.getPreferences();
preferences.setValue("border_color", borderColor);
preferences.store();
response.setPortletMode(PortletMode.VIEW);
}
}
Edit portlet.xml
to support VIEW and EDIT modes:
<portlet-app version="2.0" 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">
<portlet>
<portlet-name>Hello</portlet-name>
<portlet-class>com.acme.samples.HelloPortlet</portlet-class>
<supports>
<mime-type>text/html</mime-type>
<portlet-mode>VIEW</portlet-mode>
<portlet-mode>EDIT</portlet-mode>
</supports>
<portlet-info>
<title>Portlet preferences</title>
</portlet-info>
</portlet>
</portlet-app>
Edit view.jsp
:
<%@ page import="javax.portlet.PortletURL" %>
<%@ page import="javax.portlet.PortletMode" %>
<%@ page import="javax.portlet.PortletPreferences" %>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet"%>
<portlet:defineObjects/>
<%
PortletURL editURL = renderResponse.createRenderURL();
editURL.setPortletMode(PortletMode.EDIT);
PortletPreferences preferences = renderRequest.getPreferences();
String borderColor = preferences.getValue("border_color", "transparent");
%>
<div style="border: solid 1px <%=borderColor%>">
<a href="<%=editURL%>">Click here to switch to edit mode!</a>
</div>
Edit edit.jsp
:
<%@ page import="javax.portlet.PortletURL" %>
<%@ page import="javax.portlet.PortletMode" %>
<%@ page import="javax.portlet.PortletPreferences" %>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet"%>
<portlet:defineObjects/>
<%
PortletURL viewURL = renderResponse.createRenderURL();
viewURL.setPortletMode(PortletMode.VIEW);
PortletURL actionURL = renderResponse.createActionURL();
PortletPreferences preferences = renderRequest.getPreferences();
String borderColor = preferences.getValue("border_color", "transparent");
%>
<div style="border: solid 1px <%=borderColor%>">
<a href="<%=viewURL%>">Click here to switch to view mode!</a>
<p></p>
<form action="<%=actionURL%>" method="POST">
<label>Select border color:</label>
<select name="border_color">
<option value="transparent" <%=(borderColor == "transparent" ? "selected=\"selected\"" : "")%>>None</option>
<option value="red" <%=(borderColor == "red" ? "selected=\"selected\"" : "")%>>Red</option>
<option value="blue" <%=(borderColor == "blue" ? "selected=\"selected\"" : "")%>>Blue</option>
</select>
<input type="submit" value="Save"/>
</form>
</div>
After deployment, add the portlet to a page and test:
Again, the portlet preferences are not user-specific. In this example, when a user changes the value of border color, it affects other users.