Using Page Fragments

Contributed by the NetBeans Tutorials Team
December, 2007 [Revision number: V6.0]    

In this tutorial, you use NetBeans IDE 6.0 to create an application that includes two page fragments. One fragment holds the application's logo. The second fragment holds links for navigating between the pages in the application.

Expected duration: 20 minutes

Contents

  Content on this page applies to the NetBeans 6.0 IDE

Sample company logo used in this tutorial
» sky.jpg

This tutorial works with the following technologies and resources:

JavaServer Faces Components/
Java EE Platform
works with1.2 with Java EE 5*
works with1.1 with J2EE 1.4
Travel Database not requiredNot required

* To take advantage of NetBeans IDE 6.0's Java EE 5 capabilities, use an application server that is fully compliant with the Java EE 5 specification, such as GlassFish.

This tutorial has been tailored for use with the GlassFish v2 Application Server. If you are using a different server, consult the Release Notes and FAQs for known problems and workarounds. For detailed information about the supported servers and Java EE platform, see the Release Notes.

About Page Fragments

A page fragment is a portion of a page, such as a header, footer, or navigation bar, that can be reused in other pages. For example, you might put a common element such as a graphic or a Search field in a page fragment and then include that fragment as a header in all pages in the application. You might also include your company name and copyright information in a page fragment and use that fragment as your application's footer. Like a main page, a page fragment is a JSP page with its own associated page bean; however, the file extension of a page fragment is jspf instead of jsp.

Designing a Page That Includes Page Fragments

You begin this tutorial by creating the home page for the application. You then create a header fragment and a navigation fragment and include these fragments in the home page.
  1. Create a new web application project and call it FragmentExample. Enable the Visual Web JavaServer Faces framework.

    Figure 1 shows the page that you will create in the steps that follow:

    Figure 1: Application Home Page Figure 1: Application Home Page
  2. From the Layout section of the Components Palette, drag a Page Fragment Box component onto the upper left corner of the page.

    The Select Page Fragment dialog box opens.
  3. Click Create New Page Fragment. Type CompanyLogo in the Name field and click OK.

    The page fragment appears on the page. In addition, the page fragment is added to the Projects window and to a <div> block in the Navigator window.
  4. Click Close to close the Select Page Fragment dialog box.

    The dotted line in the Visual Designer shows the size of the page fragment. The default size is 400 pixels wide by 200 pixels high.
  5. Place a second Page Fragment Box component on the left side of the page under the CompanyLogo page fragment. Name this page fragment Navigation.
  6. From the Basic section of the Palette, drag a Static Text component onto the page and drop it to the right of the Navigation page fragment. Be sure to drop the component on the page and not on the page fragment. Change the text of this component to Welcome to Sky Company.
  7. Click the Visual Designer in a place that does not have a component. In the Properties window, change the Title property to Sky Company Home.

Creating the Header Fragment

Now you define the content of the CompanyLogo fragment, as shown in Figure 2. Any changes you make to a fragment must be made in the fragment itself, and not in the page.

Figure 2: CompanyLogo Fragment Figure 2: CompanyLogo Page Fragment
  1. If you haven't done so already, save the sample company logo JPEG file to your filesystem.
  2. Open the CompanyLogo fragment by double-clicking the component in the Visual Designer.

    The white background shows the size of the page fragment.
  3. In the Properties window, set the Width property to 720px and the Height property to 120px.
  4. From the Basic section of the Palette, drag an Image component into the upper left corner page fragment.
  5. In the Properties window, click the ellipsis button ellipsis button for the Image's url property. Add the company logo to the page fragment as follows:

    1. In the dialog box, click Add File.
    2. Navigate to the folder where you stored the company logo, sky.jpg, and select the image.
    3. Click Add File. The IDE copies the image to the resources directory of the project and displays the image's relative URL.
    4. Click OK.
  6. If necessary, click and drag sky.jpg to position it within the page fragment's borders.
  7. Click the Page1 tab to view the updates to the CompanyLogo fragment. Adjust the layout of the components on the page as necessary.

Creating the Navigation Fragment

Next you define the content of the navigation fragment as shown in Figure 3.

Figure 3: Navigation Fragment Figure 3: Navigation Fragment
  1. Open the Navigation fragment by double-clicking the component in the Visual Designer.
  2. In the Properties window, set the Width property to 150px and the Height property to 100px.
  3. From the Basic section of the Palette, drag a Hyperlink component and drop it in the page fragment. Set the text of the Hyperlink to Home.
  4. In the Properties window, set the Hyperlink's id property to homeLink and the url property to /faces/Page1.jsp.
  5. Drag a second Hyperlink component into the page fragment. Set the text of this component to Company News.
  6. Set the Hyperlink's id property to newsLink and the url property to /faces/News.jsp.

    You will create the News page in the next section.

Adding Fragments to a Second Page

In this section, you create a second page that includes the header and navigation fragments. You set a background color for this page to demonstrate how the page's style settings are inherited by a page fragment.
  1. In the Projects window, right-click the FragmentExample > Web Pages node and choose New > Visual Web JSF Page. Name the new page News and click Finish.

    The News page opens in the Visual Designer. You will design the page shown in the figure below.

    Figure 4: News Page Figure 4: News Page
  2. Click the Page1 tab. You will copy the Page Fragment and Static Text components from this page to the News page.
  3. From the Page 1 - Navigator window, hold down the Control key and select both div elements and the Static Text component. Be sure you select the div elements themselves and not just their jsp:directive.include child elements.
  4. Right-click the selection and choose Copy from the pop-up menu.
  5. Click the News tab.
  6. In the Navigator window, expand the News > page1 > html1 > body1 node. Right-click form1 and select Paste. The components you copied from Page1.jsp appear in the Visual Designer.
  7. Change the text of the Static Text component to We have a new CEO.
  8. Click an empty space in the News page. In the Properties window, change the Title property to Sky Company News.
  9. Click the ellipsis button ellipsis button for the Background property and use the color chooser to set the color to light yellow. At runtime you will be able to see a clear distinction between the Sky Company News page and the Sky Company Home page.

    The Company Logo and Navigation fragments inherit the background color of the News page.

Disabling the Link for the Current Page

In this section you add code to disable the Home link on the Page1 page and the Company News link on the News page.
  1. Click the Page1 tab and then open the Java source code for the page.
  2. Add the following code to the prerender method:

    Code Sample 1: Code to Disable the Link for the Current Page
    public void prerender() { 
        Navigation navigationFragmentBean = (Navigation)getBean("Navigation");
        Hyperlink homeLink = navigationFragmentBean.getHomeLink();
        homeLink.setDisabled(true);
     }
  3. Right-click in the Java Editor and choose Fix Imports. The IDE adds the following import statement:

    import com.sun.webui.jsf.component.Hyperlink;
  4. Click the News tab and open the Java source code for the page.
  5. Add the following code to the prerender method.

    Code Sample 2: Code to Disable the Link for the Current Page
    public void prerender() { 
        Navigation navigationFragmentBean = (Navigation)getBean("Navigation");
        Hyperlink newsLink = navigationFragmentBean.getNewsLink();
        newsLink.setDisabled(true);
     }
  6. Right-click in the Java Editor and choose Fix Imports. The IDE adds the following import statement:

    import com.sun.webui.jsf.component.Hyperlink;
  7. Click the Run Main Project button Run Project toolbar icon to run the application.

    Verify that the Home and Company News links work correctly.

Doing More: Adding a Footer Fragment

This tutorial demonstrates how to use page fragments in a simple two-page application. A real application typically has more pages.

Try It.  Add a third page to the FragmentExample application. Be sure to add another Hyperlink component in the Navigation page fragment and set the Hyperlink's url property.

Try It.  Another common use for a page fragment is to include a company's copyright information. Add a page fragment at the bottom of each page with width of 720px and height of 100px. Include copyright information such as Copyright 1994-2007 Sky Company.

Tips for Using Page Fragments

Here are some things to consider when using page fragments:


See Also:



This page was last modified:  December, 2007


www.000webhost.com