Escolar Documentos
Profissional Documentos
Cultura Documentos
In this tutorial, you use the Sun Java Studio Creator integrated development environment (IDE) to create Page Alerts of type error, information, warning, and question. Each Page Alert contains Back, Next, and Cancel buttons when appropriate. Contents - What Is a Page Alert - Designing the Start Page - Adding Properties to the SessionBean - Configuring the Start Page Buttons - Creating a Page to Display the Page Alerts - Adding a Group Panel to the Page Alert - Creating the Alert Status Page - Defining Page Navigation - Running and Testing the Application
1. Create a new web application and name it PageAlertExample. 2. From the Basic section of the Components Palette, drag a Label component and drop it at the top of the page. In the Properties window, set the text property to Page Alert Button Facets and the labelLevel property to Strong(1). 3. From the Basic section of the Palette, drag a Button component onto the page. Set the text property of the Button to Question and set its id property to questionBtn. 4. Drag a Label onto the page and place it to the right of the Question button. Set its text to Render a Question Page Alert. 5. Drag three more Buttons and corresponding Labels onto the Visual Designer and configure them as follows: a. Set the text property of the second Button to Error and its id property to errorBtn. Set its Label's text property to Render an Error Page Alert. b. Set the text property of the third Button to Warning and its id property to warningBtn. Set its Label's text property to Render a Warning Page Alert. c. Set the text property of the fourth Button to Information and its id property to infoBtn. Set its Label's text property to Render an Information Page Alert. 6. From the Basic section of the Palette, drag a Message Group component onto the page and place it beneath the Buttons.
3. Repeat Step 1 to create another property. In the New Property Pattern dialog box, set the Name to userMessage, set the Type to String, and then click OK. 4. Create a third property. In the New Property Pattern dialog box, set the Name to userVisible, set the Type to boolean, and then click OK. NOTE: The Type is case sensitive. Make sure you choose boolean and not Boolean.
2. Fix the import statements by pressing Alt-Shift-F. You can reformat the code in this and other code fragments you add to the project by pressing CTRL-Shift-F. 3. Press the Design button to return to the Visual Designer. 4. Double-click the Error button and add the following event handler code to the errorBtn_action() method: Code Sample 2: Define the Error Page Alert public String errorBtn_action() { PageAlert errorAlert = new PageAlert(); errorAlert.setType("error"); errorAlert.setDetail("Installation failed: Insufficient Privileges!"); errorAlert.setSummary("An Error has Occurred!"); errorAlert.setTitle(" " + "Machine Upgrade - Error"); getSessionBean1().setUserPageAlert(errorAlert); getSessionBean1().setUserVisible(false); return null; }
5. Return to the Visual Designer and double-click the Warning button. Add the following source to the warningBtn_action() method:
Code Sample 3: Define the Warning Page Alert public String warningBtn_action() { PageAlert warningAlert = new PageAlert(); warningAlert.setType("warning"); warningAlert.setDetail("Warning: Patch 12345-01 is missing." + " Press Next to install patch prior to upgrade"); warningAlert.setSummary("Missing Patch is required"); warningAlert.setTitle(" " + "Machine Upgrade - Warning"); getSessionBean1().setUserPageAlert(warningAlert); getSessionBean1().setUserVisible(true); return null; }
6. Return to the Visual Designer, double-click the Information button, and add the following source to the infoBtn_action() method: Code Sample 4: Define the Information Page Alert public String infoBtn_action() { PageAlert informationAlert = new PageAlert(); informationAlert.setType("information"); informationAlert.setDetail("Installation has completed successfully."); informationAlert.setSummary("Install complete"); informationAlert.setTitle(" " + "Machine Upgrade - Completed"); getSessionBean1().setUserPageAlert(informationAlert); getSessionBean1().setUserVisible(true); return null; }
7. Press CTRL-Shift-S to save the code, and then return to the Visual Designer.
1. In the Projects window, right-click the Web Pages node, and choose New > Page. Name the new page UserAlert and click Finish. 2. From the Layout section of the Palette, drag a Page Alert component onto the UserAlert page. 3. Right-click the Page Alert component and choose Property Bindings from the pop-up menu. 4. In the Selecting binding target column, expand SessionBean1 > userPageAlert. 5. Bind the following properties to the following targets. You set a binding target by selecting it as shown in the figure below. a. b. c. d. Bind the type property to #{SessionBean1.userPageAlert.type}. Click Apply. Bind the detail property to #{SessionBean1.userPageAlert.detail}. Click Apply Bind the summary property to #{SessionBean1.userPageAlert.summary}. Click Apply Bind the title property to #{SessionBean1.userPageAlert.title}. Click Apply
When you click Apply, the property is bound and turns bold to indicate that it has a value. In addition, the New binding expression field shows the binding target.
Code Sample 5: Manually Create pageAlertButtons Facet <ui:pageAlert binding="#{UserAlert.pageAlert1}" detail="#{SessionBean1.myPageAlert.detail}" id="pageAlert1" style="left: 24px; top: 24px; position: absolute" summary="#{SessionBean1.myPageAlert.summary}" title="#{SessionBean1.myPageAlert.title}" type="#{SessionBean1.myPageAlert.type}"> <f:facet name="pageAlertButtons"> <ui:panelGroup binding="#{UserAlert.groupPanel1}" id="groupPanel1"/> </f:facet> </ui:pageAlert>
3. Return to the Visual Designer. Note that the Group Panel is right justified in the Page Alert. This alignment is defined by the pageAlertButtons facet. 4. From the Basic section of the Palette, drag a Button onto the Group Panel. Before you drop the button, make sure that only the Group Panel is highlighted in blue. In the Properties window, change its text property to Back, and change the button's id to backBtn. 5. Drag a second Button onto the Group Panel. This time, make sure that only the Back button is highlighted in blue before dropping the button. Change the Button's text property to Next, and change the button's id to nextBtn. Under the Behavior section of the Properties window click the ellipsis button (...) for the visible property. 6. In the Property dialog box, select the Use binding option. On the Bind to an Object tab, select SessionBean1 > userVisible as shown in Figure 4, and click OK.
Figure 4 : Object Binding Note: The Next button disappears from the Visual Designer after you bind it, because the default value is False. This is the expected behavior. To select this button, choose it in the Outline window. 7. Drag a third Button onto the Group Panel, and again make sure that only the Back button is highlighted in blue before dropping the button.. Change its text property to Cancel. Change the button's id to cancelBtn, and select the checkbox associated with the primary property. This sets it value to True. Selecting the primary property makes the Cancel button the default button which is activated when the page is rendered.
8. Go back to the JSP view. Inside the <f:facet> tags, you can see the code in which you have defined the action of the Back, Next, and Cancel navigation buttons: Code Sample 6: View of Completed pageAlertButtons Facet ... <ui:pageAlert binding="#{UserAlert.pageAlert1}" detail="#{SessionBean1.myPageAlert.detail}" id="pageAlert1" style="left: 24px; top: 24px; position: absolute" summary="#{SessionBean1.myPageAlert.summary}" title="#{SessionBean1.myPageAlert.title}" type="#{SessionBean1.userPageAlert.type}"> <f:facet name="pageAlertButtons"> <ui:panelGroup binding="#{UserAlert.groupPanel1}" id="groupPanel1"> <ui:button action="back" binding="#{UserAlert.backBtn}" id="backBtn" text="Back"/> <ui:button action="#{UserAlert.nextBtn_action}" binding="#{UserAlert.nextBtn}" id="nextBtn" text="Next" visible="#{SessionBean1.userVisible}"/> <ui:button action="cancel" binding="#{UserAlert.cancelBtn}" id="cancelBtn" primary="true" text="Cancel"/> </ui:panelGroup> </f:facet> </ui:pageAlert> ...
9. Go back to the Visual Designer, and double-click the Cancel button. Add the following source to the button's cancelBtn_action() method: Code Sample 7: Add Cancel Button Source public String cancelBtn_action() { info("Installation has been cancelled by the user."); return null; }
When the Cancel button is clicked, an informational FacesMessage picked up by the Message Group component on Page1 is generated. 10. Go back to the Visual Designer. In the Outline window, right-click the nextBtn node and select Edit action Event Handler. This opens the Java editor to the Next button's nextBtn_action() method. Add the following source. Note that you replace the line return null; with return "next";.
Code Sample 8: Add Next Button Source public String nextBtn_action() { PageAlert pa = getSessionBean1().getUserPageAlert(); String type = pa.getType(); /*Note that type error should never occur since the *next button does not render when type is set to error.*/ if (type != null) { if (type == "question") { getSessionBean1().setUserMessage ("Your machine has been successfully upgraded."); } if (type == "error") { getSessionBean1().setUserMessage ("Please correct the permissions and restart."); } if (type == "warning") { getSessionBean1().setUserMessage("Patch installed successfully. " + "Installation has completed."); } if (type == "information") { getSessionBean1().setUserMessage("Please fill out our survey!"); } } else { getSessionBean1().setUserMessage("Application Error!"); } return "next"; }
When the user clicks the Next button, the userMessage is set with a value appropriate for the type of alert being used in the browser's session. 11. Reformat the code, press Ctrl-S to save the changes, and return to the Visual Designer.
1. Create a new web page and name it AlertStatus. 2. Drag a Label onto the page. 3. Right-click the Label and choose Property Bindings from the pop-up menu.
4. Under Select bindable property, choose text. In the Select binding target column, select SessionBean1 > userMessage. Click Apply to set the value and then click Close. The Label's text displays abc in the Visual Designer to show it is bound. 5. Select the Label. In the Properties window, open the style property by clicking its ellipsis (...) button. Set the font size to 18 and click OK. 6. Drag a Button onto the page. Set its id to returnBtn and its text to Return to Start Page.
2. Select UserAlert.jsp. a. Link UserAlert.backBtn to Page1 and name the link back. b. Link UserAlert.nextBtn to the AlertStatus Page and name the link next. c. Link UserAlert.cancelBtn to Page1 and name the link cancel. 3. Select AlertStatus.jsp and link AlertStatus.returnBtn to Page1. Name the link start. The result of these links looks like the figure below.
10
Figure 7: Deployed Page Alert Application 2. Explore the application by clicking the buttons. Each button renders a different Page Alert icon and message. Clicking the Back or Cancel buttons returns you to Page 1. The Message Group component shows the Installation cancelled message displayed in the figure below.
Figure 8: Cancel Button Message 3. Click the Next button on the Question, Warning, or Information Alert page. This brings you to the Status page. Each Status message is unique to the Alert. The Error Page Alert does not have a Next button, as you can see in Figure 9 below.
11
See Also:
q q q
Delving Into Components Using Message Components Understanding Scope and Managed Beans
More Developer Resources: For more tutorials, articles, tips, and expert advice for developers, visit the Java Studio Creator developer resources on the Sun Developer Network (SDN)
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. U.S. Government Rights - Commercial software.
12
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