Writing Multilingual Portlets Enhancing PDK-Java Portlets

Enhancing Java Portlets 7-81 4. Repeat steps 1 through 3 for every language that you wish to create a resource bundle for, updating the class name with the appropriate language code and the message strings with their equivalent in the appropriate language. Updating Your Renderer To make use of the resource bundles you just created, you need to edit the JSP show page and replace the hard-coded messages with references that will pickup the messages at run time from the resource bundle that corresponds most closely with the locale object of the portal. To update your renderer, perform the following steps: 1. Open the JSP that represents your show page and change the following: page contentType=texthtml; charset=windows-1252 import=oracle.portal.provider.v2.render.PortletRenderRequest import=oracle.portal.provider.v2.http.HttpCommonConstants import=java.util.ResourceBundle PortletRenderRequest pReq = PortletRenderRequest request.getAttributeHttpCommonConstants.PORTLET_RENDER_REQUEST; -- Get a resource bundle object for the current language. -- ResourceBundle b = ResourceBundle.getBundlemypackage2.MyProviderBundle,pReq.getLocale; -- Pull the message from the appropriate resource bundle. -- P = b.getStringmypackage2.MyProviderBundle.HELLO_MSG = pReq.getUser.getName .P P = b.getStringmypackage2.MyProviderBundle.INFO_MSG P 2. Save your JSP page. Now you can refresh your portlet and view the changes Figure 7–12 . Figure 7–12 Portlet in English To view the French greeting, you set the language in the Set Language portlet to French instead of English Figure 7–13 . Figure 7–13 Portlet in French 7-82 Oracle Fusion Middleware Developers Guide for Oracle Portal Notice that the text inside the portlet has changed, but the portlet title remains in the default language, English. You can also have the portlet set the appropriate portlet attributes such as portlet name, portlet title, and portlet description by pointing to a resource bundle from provider.xml, as described in the next section.

7.2.11.2.2 Providing Translation for Portlet Attributes In your providers definition file,

provider.xml, a number of attributes describing your portlet are defined such as the portlets name and description, these are used in places, for example in your portlets title bar in Show mode and so should be translated, too. There are two different ways of providing these translations, which one you choose is up to you. Both of these methods are outlined in the following sections: ■ Method 1: Using Resource Bundles at the Provider Level ■ Method 2: Creating Resource Bundles at Portlet Level Method 1: Using Resource Bundles at the Provider Level You can provide translations for your portlet attributes in your resource bundles, then specify that you want to use these resource bundles in provider.xml, specifying the keys you have used in your resource bundles. Using this method you can use the keys you want to, and as long as you use different keys for each corresponding attribute in your providers various portlets you can have just one set of resource bundles that all of your providers portlets can use. This section consists of the following tasks: ■ Updating Your Resource Bundles Perform the following steps: 1. Open your default resource bundle, MyProviderBundle.java. 2. Add additional strings to your resource bundle that represent your portlet attributes and then add text for those strings: package mypackage2; import java.util.ListResourceBundle; public class MyProviderBundle extends ListResourceBundle { public static String HELLO_MSG = MyPortletHelloMessage; public static String INFO_MSG = MyPortletInfoMessage; public static String PORTLET_NAME = FirstPortletName; public static String PORTLET_TITLE = FirstPortletTitle; public static String PORTLET_SHORT_TITLE = FirstPortletShortTitle; public static String PORTLET_DESCRIPTION = FirstPortletDescription; public static String TIMEOUT_MESSAGE = FirstPortletTimeoutMessage; public Object[][] getContents { return contents; } static private final Object[][] contents = { {HELLO_MSG, Hi}, {INFO_MSG, This is the show page of the portlet and it is being generated in the default language}, {PORTLET_NAME, MyNLSPortlet}, {PORTLET_TITLE, My NLS Portlet}, {PORTLET_SHORT_TITLE, MyNLSPortlet}, {PORTLET_DESCRIPTION, My first ever NLS portlet, using my MyPortletShowPage.jsp}, Enhancing Java Portlets 7-83 {TIMEOUT_MESSAGE, Timed out waiting for MyNLSPortlet} }; } 3. Save MyProviderBundle.java. 4. Open MyProviderBundle_fr.java. Change it so that it contains the French strings that match the strings declared in MyProviderBundle. package mypackage2; import java.util.ListResourceBundle; public class MyProviderBundle_fr extends ListResourceBundle { public Object[][] getContents { return contents; } static private final Object[][] contents = { {MyProviderBundle.HELLO_MSG, Bonjour}, {MyProviderBundle.INFO_MSG, Cette page est le show mode de la portlet et est generee en francais}, {MyProviderBundle.PORTLET_NAME, MaPremierePortlet}, {MyProviderBundle.PORTLET_TITLE, Ma Portlet Multi-Langue}, {MyProviderBundle.PORTLET_SHORT_TITLE, Ma NLS Portlet}, {MyProviderBundle.PORTLET_DESCRIPTION, Ma premiere portlet multi-langue, utilisant mon renderer}, {MyProviderBundle.TIMEOUT_MESSAGE, Temps dacces a la portlet demandee expire} }; } 5. Save MyProviderBundle_fr.java. ■ Updating provider.xml Perform the following steps: 1. Open the XML provider definition file and update it to point to the resource bundle instead of using the hard-coded portlet attribute values. portlet class=oracle.portal.provider.v2.DefaultPortletDefinition id3id resourcemypackage2.MyProviderBundleresource nameKeyFirstPortletNamenameKey titleKeyFirstPortletTitletitleKey ShortTitleKeyFirstPortletShortTitleShortTitleKey descriptionKeyFirstPortletDescriptiondescriptionKey timeout10timeout timeoutMessageKeyFirstPortletTimeoutMessagetimeoutMessageKey showEditToPublicfalseshowEditToPublic hasAbouttruehasAbout For more information on the syntax of provider.xml, refer to the provider Javadoc on OTN: http:www.oracle.comtechnologyproductsiasportalhtmljavado cxml_tag_reference_v2.html 7-84 Oracle Fusion Middleware Developers Guide for Oracle Portal Method 2: Creating Resource Bundles at Portlet Level PDK-Java defines a set of resource bundle keys that you can use for providing translations for your portlet attributes. Making use of these keys means that you dont have to specify the resource bundle keys in your provider.xml file, as we did in Method 1: Using Resource Bundles at the Provider Level . However, you do have to provide a separate set of resource bundles for each portlet in your provider as the keys you use for each portlet need to be the same, but their values will differ. You must perform the following tasks: ■ Updating Your Resource Bundles Perform the following steps: 1. Open your default resource bundle, MyProviderBundle.java. 2. Remove any changes you made from the previous section, and import oracle.portal.provider.v2.PortletConstants. You can then reference the following constants instead of the strings. You do not have to declare static strings when using PortletConstants: {PortletConstants.NAME, MyNLSPortlet}, {PortletConstants.TITLE, My NLS portlet}, {PortletConstants.SHORTTITLE, MyNLSPortlet}, {PortletConstants.DESCRIPTION, My first ever NLS portlet}, {PortletConstants.TIMEOUTMSG, Timed out waiting for MyNLSPortlet} 3. Save MyProviderBundle.java. 4. Open MyProviderBundle_fr.java. Remove the portlet attributes added in the previous section, import oracle.portal.provider.v2.PortletConstants, and reference the constants instead of the strings. {PortletConstants.NAME, MaPremierePortlet}, {PortletConstants.TITLE, Ma Portlet Multi-Langue}, {PortletConstants.SHORTTITLE, Ma NLS Portlet}, {PortletConstants.DESCRIPTION, Ma premiere portlet multi-langue, utilisant mon renderer}, {PortletConstants.TIMEOUTMSG, Temps dacces a la portlet demandee expire} 5. Save MyProviderBundle_fr.java. ■ Updating provider.xml Perform the following steps: 1. In provider.xml, you need to use only one tag instead of one tag for each string as you did in Method 1: Using Resource Bundles at the Provider Level . Delete all translated strings in the file, for example, the nameKey, titleKey, ShortTitleKey, and descriptionKey tags. Then add the following tag between the portlet id and the timeout number value: resourcemypackage2.MyProviderBundleresource For more information on the syntax of provider.xml, refer to the provider Javadoc on OTN: http:www.oracle.comtechnologyproductsiasportalhtmljavado cxml_tag_reference_v2.html For more information on Java Internationalization see the Internationalization trail of the Java Tutorial. Enhancing Java Portlets 7-85

7.2.11.3 Viewing the Portlet

Once you have updated your provider and deployed it to Oracle Containers for Java EE, refresh the provider and portal page containing your portlet. To see your resource bundles working, add the Set Language portlet to your page and try changing the language setting to French. Remember that the default resource bundle is English, and that selecting any other language that doesnt have a corresponding resource bundle will result in the portlet being displayed in English.

7.3 Building Struts Portlets with Oracle JDeveloper

This section describes the framework for building Struts portlets with Oracle JDeveloper for use in Oracle Portal. You will learn how to build a Struts portlet from an existing application by adding the Struts Tag Library from the Oracle Portal Developer Kit version 9.0.4.0.2 or higher to Oracle JDeveloper, then use the Oracle PDK Java Portlet wizard to create the Java portlet itself. This sections covers the following tasks: ■ Section 7.3.1, Oracle Portal and the Apache Struts Framework ■ Section 7.3.2, Creating a Struts Portlet

7.3.1 Oracle Portal and the Apache Struts Framework

This section discusses the use of the Apache Struts with Oracle Portal. Struts is an implementation of the Model-View-Controller MVC design pattern. The following topics are discussed in this section: ■ Section 7.3.1.1, Model View Controller Overview ■ Section 7.3.1.2, Apache Struts Overview ■ Section 7.3.1.3, Oracle Portal Integration with Struts ■ Section 7.3.1.4, Summary

7.3.1.1 Model View Controller Overview

Enterprise applications undertake several distinct tasks, as follows: ■ Data access ■ Business logic implementation ■ User interface display ■ User interaction ■ Application page Flow The MVC Model View Controller architecture provides a way of compartmentalizing these tasks, based on the premise that activities, such as data presentation, should be separate from data access. This architecture enables you to easily plug a data source into the application without having to rewrite the user interface. MVC allows the logical separation of an application into three distinct layers: the Model, the View, and the Controller. The Model The Model is the repository for the application data and business logic. One facet of the Models purpose is to retrieve data from and persist data to the database. It is also responsible for exposing the data in such a way that the View can access it, and for implementing a business logic layer to validate and consume the data entered through 7-86 Oracle Fusion Middleware Developers Guide for Oracle Portal the View. At the application level, the Model acts as the validation and abstraction layer between the user interface and the business data that is displayed. The database server itself is simply a persistence layer for the Model. The View The View is responsible for rendering the Model data using JSPs. The View code does not include a hardcoded application or navigation logic, but may contain some logic to carry out tasks like displaying conditional data based on a user role. When an end user executes an action within the HTML page that the View renders, an event is submitted to the Controller. The Controller then determines the next step. The Controller The Controller is the linchpin of the MVC pattern. Every user action carried out in the View is submitted through the Controller. The Controller then performs the next action, based on the content of the request from the browser. The Controller can be driven in several different ways. For example, you can use URL arguments to route the requests to the correct code. The MVC pattern itself determines the function of the Controller, not how it should work. Benefits The MVC architecture provides a clear and modular view of the application and its design. By separating the different components and roles of the application logic, it allows developers to design applications as a series of simple and different components: the Model, the View, and the Controller. This pattern should help to create applications that are easier to maintain and evolve. For example, once you create one view, you can easily create another view using the same business logic. Because of the ease and reusability, the MVC pattern is the most widely used pattern in the context of Web-based application development. Figure 7–14 shows how the MVC pattern applies to a conventional thin-client Web application: Figure 7–14 The MVC Pattern

7.3.1.2 Apache Struts Overview

The Apache Struts framework http:struts.apache.org is one of the most popular frameworks for building Web applications, and provides an architecture based on the JSP Model 2 approach of the MVC design paradigm. In the Model 2 approach, end user requests are managed by a servlet that controls the flow, and uses components such as JavaBeans or EJBs to access and manipulate the data. It then uses Enhancing Java Portlets 7-87 JSPs to render the application content in a Web browser. This model differs from JSP Model 1, where the JSPs managed the browser request and data access. The Struts framework provides its own HTTP Servlet as a controller component. The Struts framework is driven by an XML configuration file that contains the page flow of the application. Struts does not provide the Model, but allows developers to integrate it to any data access mechanism, for example EJBs, TopLink, or JDBC. The most common technology for writing View components is JSP and Struts provides various tag libraries to help in writing these, although some of these tags have now been superseded by the Java Standard Tag Library JSTL, which may also be used.

7.3.1.3 Oracle Portal Integration with Struts

The Oracle Portal Developer Kit contains numerous examples and documents regarding the usage of the Oracle Portal APIs, such as personalization and caching. The integration of the application flow and business logic is not part of the portlet APIs. By using the Struts framework, however, you can leverage the MVC architecture to create and publish applications within your enterprise portal. Oracle Struts Portlet To create a portlet using the Struts framework, or to generate a portlet from an existing Struts application, you must deploy all the components in the J2EE container. In the context of Oracle Portal, the Struts application is called by the PPE, and not by the browser as compared to a standalone Struts application. When a portlet show call is made, the page engine sends a request to the Struts portlet renderer, which then forwards the request to the Apache Struts Controller servlet, as shown in Figure 7–15 . Figure 7–15 Integrating Struts Applications with Oracle Portal The following code shows a portion of the provider definition file provider.xml: ... Note: For more information about JSTL and JSF, see the FAQ on the Apache Software Foundation Web site http:struts.apache.orgkickstart.html .