
Creating a CSS-based Fixed Page Layout - Part1
Contributed by Winston Prakash
In this tutorial, you use the NetBeans Visual Web JSF framework to create a CSS-based fixed page layout. You will use different Layout Panels and other HTML elements from the palette to complete the layout for your page.
Contents
To follow this tutorial, you need the following software and resources.
| NetBeans IDE |
Web & Java EE version 6.1 or 6.0 |
| Java Developer Kit (JDK) |
Version 6 or
version 5 |
JavaServer Faces Components/
Java EE Platform |
1.2 with Java EE 5* or
1.1 with J2EE 1.4
|
| GlassFish Application Server |
V2 |
| Travel Database |
Not required |
* To take advantage of NetBeans IDE's Java EE 5 capabilities, use an application server that is fully compliant with the Java EE 5 specification, such as the GlassFish Application Server V2 UR2. 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.
Note for NetBeans IDE 6.1 users:
- Creating a project in NetBeans 6.1 includes new options which can be left at the default. For example, the Use Dedicated Folder for Storing Libraries checkbox may be left unselected.
- NetBeans IDE 6.1 features on-demand binding. Where components require Java coding, you must now manually add the binding attribute to components in a Visual Web JSF application. To do so, right-click each component and choose Add Binding Attribute. For more information, see the On-demand Binding Attribute Wiki.
Creating the Application
In this tutorial, you create a CSS-based, single-page web application. You begin by constructing a page with the layout structure shown in the following figure.
-
Create a web application that uses the Visual Web JSF framework. Name the project com.samples.layout and the default source package fixedpagelayoutex. Note: Creating a project in NetBeans 6.1 includes new options which can be left at the default. For example, the Use Dedicated Folder for Storing Libraries checkbox may be left unselected.
Page1.jsp file opens in the Visual Designer.
-
In the Projects window, expand the Web pages node, right-click on Page1.jsp, and choose Refactor > Rename.
The Rename dialog box opens.
-
In the Rename dialog box, type PageLayout1 as the new name of the page. Check to apply rename of comments and click Refactor.
The name of the page changes in the Project window and on the tab in the Visual Designer.
-
Click the JSP tab to open the JSP Editor. Add the following HTML to the <form> component.
| Code Sample 1: Editing the JSP. |
<div id="container"> <div id="header"> Header </div> <div id="leftBar"> Left Bar </div> <div id="content"> Main Content </div> <div id="rightBar"> Right Bar </div> <div id="footer"> Footer </div> </div> |
- In the Projects window, right-click the resources folder and select New > Cascading Style Sheet.
-
In the New Cascading Style Sheet wizard, type layout as the file name and click Finish.
layout.css appears under the resources node in the Projects window.
- Return to the design view of
PageLayout1.jsp. From the Advanced section of the Palette, drag and drop a Link component onto the Visual Designer.
- Select the link component in the Navigator window. In the Property window, set the URL as
/resources/layout.css.
- In the Projects window, double-click
layout.css to open it in the Visual Designer.
Add the following CSS rules to layout.css.
| Code Sample 2: Adding CSS Rules. |
#container { margin:auto; width: 760px; background: #ffffff; }
#header {
background: #DDDDDD;
}
#leftBar {
float: left;
width: 150px;
background: #EBEBEB;
}
#content {
float:left;
width:450px;
background-color: #cdcde6;
}
#rightBar {
float:right;
width: 160px;
background: #EBEBEB;
}
#footer {
clear:both;
background:#DDDDDD;
} |
Give special attention to the CSS properties highlighted in blue. They are responsible for placing the containers () in the correct place. The footer CSS rule has the property clear:both which gives a hint to the browser that both of its ends should be cleared (does not allow floating elements on both sides), so it is always placed beneath the other floating elements.
- To achieve the border, select the
div:container in the Navigator window and set its style property
Border to solid, 5px width, and leave the default color at black.
- Right-click the project node and choose Run to deploy the application. The Browser should display a page resembling the following figure:
Adding the Header Contents
First we add the header to our page and then add its contents.
-
From the Layout section of the Palette, drag and drop a Layout Panel component onto the header <div> in the Visual Designer.
Note: The default size of the Layout Panel is 128 x 128 pixels.
- Open the JSP Editor and remove the content
Header from the header <div>.
-
Select the Layout Panel in the Visual Designer. In the Properties window for the Layout Panel, click the ellipsis (...) next to the style property.
The Style Editor opens.
-
In the Style Editor for the Layout Panel, set the height to 175px and width to 760px.
Note: The width is same as width of the container <div> as specified in layout.css.
-
Set the background image of the Layout Panel using the Style Editor.
If you do not have a header image, you can use the download one here.
- Click OK to save your changes.
In the Properties window, set the panelLayout property to either Grid Layout or Flow Layout.
Note: If you select GridLayout, then you can more easily place other components on the page.
If you would rather wish to use Flow Layout then you must use other layout components to format the component placement.
- Drag and drop a Static Text on to the Layout Panel and set its
text property to Fixed Page Layout 1. Use the Style Editor to change the style of the text.
-
Run the application. The application should resemble the following figure.
Adding the Left Sidebar Contents
Next we add a Navigation Bar to the left-hand side of the page. Let us keep its contents simple by adding only hyperlinks.
-
Drag and drop a Grid Panel component on to the leftBar <div>.
Note: The default size of the Grid Panel is 128 x 128 pixels. Once a component is added to the Grid Panel, the initial size is discarded.
- In the JSP Editor, remove the content
Left Bar from the leftBar <div>.
- From the Basic section of the Palette, drag and drop several HyperLink components onto the Grid Panel. Look at the Navigator window to ensure that all the hyperlinks are inside the Grid Panel, and rearrange if necessary.
-
Run the application. The application should resemble the following figure.
Adding the Right Sidebar Contents
As with the Left Sidebar, let us keep the contents of Right Bar simple. Let us add some text and an image.
-
From the Layout section of the Palette, drag and drop a Group Panel component onto the rightBar <div>.
Note: Like other panels, the default size of the Group Panel component is 128 x 128 pixels. Once a component is added to the Group Panel, the initial size is discarded.
- In the JSP Editor, remove the content
Right Bar from the rightBar <div>.
-
From the Basic section of the Palette, drag and drop a StaticText component and change its text property in the Properties window.
- Note: You can also add HTML as text. Be sure to uncheck the
escape property of the StaticText component.
- From the Basic section of the Palette, drag an Image component to the Group panel. In the Properties window, set the
url property to any image. If you do not have an image, you can download one here.
-
Run the application. The result should resemble the following figure.
Adding the Footer Contents
Now we add some hyperlinks to the footer panel that are aligned centered.
- From the Layout section of the Palette, drag and drop a Group Panel component onto the
footer div.
- In the JSP Editor, remove the content
Footer from the footer <div>.
- From the Basic section of the Palette, drag and drop several Hyperlink components onto the footer panel. Change their
text property in the Properties window.
- Select the Group Panel component in the Visual Designer. In the Properties window, set the
separator text property to "|".
-
Run the application and view the results. The application should resemble the following figure.
Adding Contents to the Main Content Menu
This last step is the important step. Here we add the panel for main contents.
- Ftom the Layout section of the Palette, drag and drop a Layout Panel component on to the
content div.
- In the JSP Editor, remove the content
Main Content from the content <div>.
- From the property sheet of Layout Panel, set the width to 100%. Now the Layout Panel should cover the entire Main Content <div>
- In the Properties window,
set the Page Layout mode to Grid Layout for easier placement of components in the Visual Designer.
-
Hold the bottom resize handle of the Layout Panel component and resize to the Layout Panel to the maximum area your contents will occupy. Press Refresh Designer button.
Note: If you want the main content panel to grow according to amount of content, then use the Group Panel component instead of the Layout Panel component. However, the Group Panel compoent does not support Grid Layout mode, and you should use different Layout components to place components in Flow Layout mode.
-
Run the application. It should resemble the following figure.
In this tutorial, you created a web application with a CSS based fixed page layout. You also changed properties for basic and layout components using the Style Editor.
See Also
This page was last modified: April 15, 2008
|
|