Professional Documents
Culture Documents
This tutorial explains how to use the Sun Java Studio Creator integrated development environment (IDE) to develop a JSR-168 compliant JavaServer Faces portlet application to provide content for a portal. Portals are web sites that provide customized views of content from various sources based upon user input. Portal servers contain the portlets that build custom web content fragments, assembling (or aggregating) the fragments to create the customized web page. Developing portlet applications is similar to developing web applications. Before you work through this tutorial, familiarize yourself with the application development process. To do this, work through any of the Java Studio Creator tutorials. Getting Started With Java Studio Creator is a useful introduction to the IDE. Contents - About Portlets - Creating a Portlet Project - Adding View Mode Pages - Setting Up the Page Navigation - Adding Edit and Help Mode Pages - Adding a Web Service To a Portlet - Creating a Session Bean Property - Doing More With Portlets If you are behind a firewall, you will need to configure the IDE's server properties to set the HTTP proxy server (see portletlifecycle.html included in the PDF ZIP file).
About Portlets
Developing a portlet is similar to developing a regular web application, though some tasks, such as setting values on a page by taking an action on another page, are handled differently. In addition, the JSR-168 standard defines three portlet modes that determine the type of content the portlet shows:
q
q q
View. The View mode generates the portlet page with which the user interacts, and is the default mode. Edit. The Edit mode enables users to customize the behavior of the portlet. Help. The Help mode provides help information for the portlet.
Each mode is separate from the other modes, has its own page navigation, and is accessed by taking specific action. For example, in the Apache Pluto Portal server bundled with the IDE, users switch modes by clicking the portal server's Edit, Help, and View buttons. See the Apache Pluto Portal project site for more information about the Pluto Portal server. Working through this tutorial, you learn how to create View, Edit and Help mode pages and how to specify the initial page for the Edit and Help modes. You next learn how to use a button to set a value on a portlet
page by creating and setting a property on the Session Bean. Finally, you learn how to access a web service from a portlet.
1. From the main menu, choose File > New Project. 2. In the New Project wizard, select Web from the Categories list, and select JSR-168 JSF Portlet Project from the Projects list. 3. Click Next. 4. Name the project PortletEx. 5. Click Next. The Portlet Deployment Descriptor window opens, as shown in the following figure.
Figure 1: Portlet Deployment Descriptor Dialog Box 6. Click Finish to accept the default values. PortletEx's initial page, PortletPage1, opens in the Visual Designer, as shown in the following figure.
The IDE creates pages in View mode by default. PortletPage1 is the default View mode initial page. Notice that part of the portlet project's design view grid is shaded. The white part of the design view grid is what displays in the portal server. Recall that you are building a web content fragment that the portal server will aggregate into a complete web page. You can choose the portlet page layout using the Page Layout drop-down list in the Properties window. Flow Layout enables the web browser to determine page layout. You have the least control over how a web browser renders a page when you choose Flow Layout. Grid Layout assists with absolute positioning of components on the portlet page: what you see is what you get. Grid Layout is the preferred layout mode and is the default for regular web applications as well as portlet applications. You will use Grid Layout for this tutorial.
1. Drag a Static Text component onto the portlet page and change its text to Welcome to Page 1. 2. Drag a Button component onto the page and change its text to Go To Page 2. 3. In the Projects window, right-click PortletEx > Web Pages and choose New > Portlet Page from the pop-up menu. The New Portlet Page wizard opens. 4. Type PortletPage2 (with no spaces) in the File Name field and click Finish. PortletPage2 opens in the Visual Designer. 5. Drag a Static Text component onto the page and change its text to Welcome to Page 2. 6. Drag a Button component onto the page and change its text to Go To Page 1.
Figure 5: Portal Mode Navigation When switching modes and then returning to a previous mode, users return to the previous modes initial page rather than to the last page they were viewing. For example, if a user navigates to Page 2 in View mode, switches to Edit mode, and then switches back to View mode, the user returns to page 1 of View mode and not to page 2. Here you determine the initial page that the user sees when switching modes.
1. Create a new page. In the Projects window, right-click PortletEx > Web Pages and then choose New > Portlet Page. Name the new portlet page Edit1, and click Finish. 2. Drag a Label component onto the page and type Welcome to Edit Mode!. 3. In the Projects window, right-click PortletEx > Web Pages > Edit1.jsp, and then choose Set as Initial > Edit Mode Page from the pop-up menu. 4. Build and run the portlet application. Click the Pluto Portal server's Edit button to view the page, as shown in the following figure.
You follow a similar procedure to set the initial Help mode page.
1. Create a new page. In the Projects window, right-click PortletEx > Web Pages and then choose New > Portlet Page. Name the new portlet page Help1. 2. Drag a Label component onto the page and type Welcome to Help Mode!. 3. In the Projects window, right-click PortletEx > Web Pages > Help1.jsp, and then choose Set as Initial > Help Mode Page from the pop-up menu. 4. Build and run the portlet application. Click the Pluto Portal server's Help button to view the page. 5. Close the PortletEx project by choosing File > Close "PortletEx". In the next section, you create a new portlet project and add a web service to it.
1. Create a new JSR-168 JSF Portlet project and name it PortletWS. 2. From the Basic section of the Palette, drag a Button component onto PortletPage1, type The time is..., and press Enter. In the Properties window, set the id property to timeButton. 3. Drag a Static Text component onto the page and drop it below the Button component. 4. In the Servers window, expand Web Services > Samples, as shown in the following figure.
Figure 8: Choosing WorldTime Web Service 5. Drag the WorldTime sample web service and drop it on PortletPage1. worldTimeClient1 displays in the Outline window, as shown in the following figure.
Figure 9: worldTimeClient 1 in the Outline Window 6. Test the WorldTime sample web service. In the Servers window, expand the Samples > WorldTime web service node. A list of methods appears. Right-click the second tzStampNow method and choose Test Method from the pop-up menu, as shown in the following figure.
The Test Web Service Method dialog box displays. tzStampNow takes two parameters, both of type java.lang.String: tZone (time zone) and format (format specification for the results). Type PST for the value of tZone and MM-dd-yyyy HH:mm: ss for the value of format, as shown in the following figure.
Figure 11: tzStampNow Parameter Values 7. Click the Submit button. A type java.lang.String displays in the Results section of the dialog box when the test is successful. Click the Close button to close the Test Web Service Method dialog box. If an error dialog box opens displaying an InvocationTargetException error message, (see setproxy.html included in the PDF ZIP file) ensure that your HTTP proxy server settings are correct.
1. In the Outline window, right-click the SessionBean1 node and choose Add > Property from the popup menu. 2. In the New Property Pattern dialog box, type time as the name of the new property, and then click OK. The new property time is added to the SessionBean. 3. To bind the Static Text component to the time property, right-click the Static Text component and select Bind to Data from the pop-up menu.
4. In the Bind to Data for StaticText1 dialog box, click the Bind to an Object tab. The Select binding target pane opens. 5. Choose SessionBean1 > time, as shown in the following figure.
Figure 12: Binding the time property to staticText1 6. Click OK. Note that the Static Text component changes in the Visual Designer. 7. Double-click the The time is... button, then add the following code (shown in bold) to the timeButton() method. After inserting the code, you can press Ctrl-Shift-F to automatically reformat the code. Code Sample 1: WorldTime web service timeButton_action() method public String timeButton_action() { String result = ""; try { result = this.getWorldTimeClient1().tzStampNow("PST", "MM-dd-yyyy HH:mm:ss"); } catch (Exception e) { log("Page1 failure with web service", e); } this.getSessionBean1().setTime(result); return null; }
8. Run and deploy the portlet. When you click the The time is... button, the current Pacific Standard Time displays, similar to the following figure.
10
Summary
In this tutorial, you learned how to design and deploy a simple navigation portlet, to add pages for the Edit and Help modes, to set values on a portlet page using SessionBean properties when a user clicks a button on the same page, and to add a web service to a portlet.
11
See Also
q q q q q q q q q q
Getting Started With Java Studio Creator Accessing Web Services Building a GoogleSearch Web Service Portlet Deploying a Portlet Application Creating Portlets in Sun Java Studio Creator 2 How to Navigate Between Pages in a Portlet Application David Botterill's Weblog Greg Ziebold's Weblog Introduction to JSR 168 - The Portlet Specification Pluto portal project site
More Developer Resources: For more tutorials, articles, tips, forums, updates, and expert advice for developers, visit the Java Studio Creator developer resources on the Sun Developer Network (SDN) at http://developers.sun.com/jscreator/.
Sun Java Studio Creator integrated development environment (IDE) Sun Java System Application Server version number (Application Server) Java Platform, Standard Edition technology (Java SE(tm) platform) JavaServer(tm) Faces technology JavaServer Pages(tm) technology (JSP(tm) technology) Sun Java System Web Server version number (Web Server) Java Database Connectivity software (JDBC software) Enterprise JavaBeans(tm) specification (EJB(tm) specification) Solaris(tm) Operating System software (Solaris OS software)
The following third-party trademarked terms might be used in the Sun Java Studio Creator tutorials:
q q
Copyright 2006 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, California 95054, U.S.A. All rights reserved. Sun Microsystems, Inc. has intellectual property rights relating to technology embodied in the product that is described in this document. In particular, and without limitation, these intellectual property rights may include one or more of the U.S. patents listed at http://www.sun.com/patents and one or more additional patents or pending patent applications in the U.S. and in other countries.
12
U.S. Government Rights - Commercial software. Government users are subject to the Sun Microsystems, Inc. standard license agreement and applicable provisions of the FAR and its supplements. Use is subject to license terms. Sun, Sun Microsystems, the Sun logo, Java and the Java Coffee Cup logo are trademarks or registered trademarks of Sun Microsystems, Inc. in the U.S. and other countries.This product is covered and controlled by U.S. Export Control laws and may be subject to the export or import laws in other countries. Nuclear, missile, chemical biological weapons or nuclear maritime end uses or end users, whether direct or indirect, are strictly prohibited. Export or reexport to countries subject to U.S. embargo or to entities identified on U.S. export exclusion lists, including, but not limited to, the denied persons and specially designated nationals lists is strictly prohibited. Note: Sun is not responsible for the availability of third-party web sites mentioned in this document and does not endorse and is not responsible or liable for any content, advertising, products, or other materials on or available from such sites or resources. Sun will not be responsible or liable for any damage or loss caused or alleged to be caused by or in connection with use of or reliance on any such content, goods, or services available on or through any such sites or resources.
Copyright 2006 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, California 95054, tats-Unis. Tous droits rservs. Sun Microsystems, Inc. dtient les droits de proprit intellectuels relatifs la technologie incorpore dans le produit qui est dcrit dans ce document. En particulier, et ce sans limitation, ces droits de proprit intellectuelle peuvent inclure un ou plus des brevets amricains lists l'adresse http://www.sun.com/patents et un ou les brevets supplmentaires ou les applications de brevet en attente aux tats-Unis et dans les autres pays. L'utilisation est soumise aux termes de la Licence. Sun, Sun Microsystems, le logo Sun, Java et le logo Java Coffee Cup sont des marques de fabrique ou des marques dposes de Sun Microsystems, Inc. aux tats-Unis et dans d'autres pays.Ce produit est soumis la lgislation amricaine en matire de contrle des exportations et peut tre soumis la rglementation en vigueur dans d'autres pays dans le domaine des exportations et importations. Les utilisations, ou utilisateurs finaux, pour des armes nuclaires,des missiles, des armes biologiques et chimiques ou du nuclaire maritime, directement ou indirectement, sont strictement interdites. Les exportations ou rexportations vers les pays sous embargo amricain, ou vers des entits figurant sur les listes d'exclusion d'exportation amricaines, y compris, mais de manire non exhaustive, la liste de personnes qui font objet d'un ordre de ne pas participer, d'une faon directe ou indirecte, aux exportations des produits ou des services qui sont rgis par la lgislation amricaine en matire de contrle des exportations et la liste de ressortissants spcifiquement dsigns, sont rigoureusement interdites. Sun Microsystems n'est pas responsable de la disponibilit de tiers emplacements d'enchanement mentionns dans ce document et n'approuve pas et n'est pas responsable ou iresponsable d'aucun contenu, de la publicit, de produits, ou d'autres matriaux dessus ou fournis par de tels emplacements ou ressources. Sun ne sera pas responsable ou iresponsable d'aucuns dommages ou perte causs ou allgus pour tre caus par ou en liaison avec l'utilisation de ce produit ou la confiance dans des tels contenu, marchandises, ou services disponibles sur ou par des tels emplacements ou ressources.
13