You are looking at documentation for an older release. Not what you want? See the current release documentation.
In this example you add some language resources and CSS to be used in the JSP of the HelloWorld portlet. You can download the portlet's source code here.
The example is plain JSR-286, except one thing: eXo expects that the resource bundle should be found in the locale/portlet
folder.
The path is fixed and you need to pack your .properties
files in a sub-folder of this path.
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.RenderMode;
public class HelloPortlet extends GenericPortlet {
@RenderMode(name = "view")
public void Hello(RenderRequest request, RenderResponse response) throws IOException, PortletException {
PortletRequestDispatcher prDispatcher = getPortletContext().getRequestDispatcher("/jsp/hello.jsp");
prDispatcher.include(request, response);
}
}
Edit HelloPortlet_en.properties
to add language properties:
com.acme.samples.HelloPortlet.Hello=Hello! com.acme.samples.HelloPortlet.Msg1=This is a portlet example. com.acme.samples.HelloPortlet.Msg2=Written by a baker.
Edit HelloPortlet_fr.properties
to add language properties:
com.acme.samples.HelloPortlet.Hello=Bonjour! com.acme.samples.HelloPortlet.Msg1=C'est un example de portlet. com.acme.samples.HelloPortlet.Msg2=Ecrit par un boulanger.
Edit portlet.xml
to register supported locale and the resource-bundle:
<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>
</supports>
<supported-locale>en</supported-locale>
<resource-bundle>locale.portlet.HelloPortlet.HelloPortlet</resource-bundle>
<portlet-info>
<title>Hello</title>
</portlet-info>
</portlet>
</portlet-app>
Edit Stylesheet.css
:
.HelloPortlet1, .HelloPortlet2, .HelloPortlet3 {
padding: 10px;
font-style: italic;
font-size: 18px;
width: 400px;
}
.HelloPortlet1 {
background-color: antiquewhite;
}
.HelloPortlet2 {
background-color: lemonchiffon;
}
.HelloPortlet3 {
background-color: wheat;
}
Edit hello.jsp
to add language properties:
<%@ taglib uri="http://java.sun.com/portlet" prefix="portlet" %>
<%@ page import="java.util.ResourceBundle"%>
<portlet:defineObjects />
<%
String contextPath = request.getContextPath();
ResourceBundle resource = portletConfig.getResourceBundle(request.getLocale());
%>
<link rel="stylesheet" type="text/css" href="<%=contextPath%>/skin/Stylesheet.css"/>
<div class="HelloPortlet1">
<span><%=resource.getString("com.acme.samples.HelloPortlet.Hello")%></span>
</div>
<div class="HelloPortlet2">
<span><%=resource.getString("com.acme.samples.HelloPortlet.Msg1")%></span>
</div>
<div class="HelloPortlet3">
<span><%=resource.getString("com.acme.samples.HelloPortlet.Msg2")%></span>
</div>
Notice the taglib and portlet:defineObjects is added to be able to use the
portletConfig
object.
To simplify this example, a CSS link is added to the body (JSP) but this is not recommended. Please see the Portlet CSS section for a better way.
After deployment, add the portlet to a page and test:
The locale resource bundle needs to be packed in a sub folder under WEB-INF/classes/locale/portlet/
.