Practical Database Programming With Java
.pdf
5.3 Exploring NetBeans IDE 6.8 239
Figure 5.77. The opened web page contained the Manufacturer data table.
screen is very raw and without any beautification. We will discuss these two issues and solutions in the following sections. First let’s take care of the deployment of your project, and then we can make this display better by showing only some selected columns in the
Faculty table.
5.3.5.10 Deploying the Project Using the Administration Console
Because of the complexity in building Java EE projects, in some cases, you may need to use different tools to help you to deploy your Java EE projects. A good candidate is the Administration Console tool provided by Java EE 6 SDK.
To administer the Enterprise Server and manage users, resources, and Java EE applications, you can use the Administration Console. One of the most important features provided by this tool is to deploy your Java EE project. One of the advantages of using the Administration Console tool to deploy Java EE applications is that more detailed and clear debug information can be obtained by using this tool compared with the Deploy process in the NetBeans IDE.
Perform the following operations to deploy your Java EE project using the
Administration Console:
1.Make sure that the Java EE 6 SDK has been installed in your computer since the Administration Console is a part of this SDK. Refer to Section 5.3.5.2.1 in this Chapter to complete this installation if the SDK has not been installed.
240 |
Chapter 5 Introduction to NetBeans IDE |
|
|
2. |
Before you can run the Administration Console tool, make sure that the Enterprise |
||
|
Server—here we used Glassfish v3—has been started. To start the |
Enterprise |
Server |
|
in Windows, go to Start > All Programs > Java EE |
6 SDK |
Web |
|
Profile > Start Application Server. |
|
|
3. |
As the server started, you can start the Administration Console by going to Start |
> All |
|
|
Programs > Java EE 6 SDK Web Profile > Administration Console. |
||
4.In the Login window, enter your Java EE Server username and password. In this application, they are admin and reback, and click on the Login button to continue.
5.In the Common Tasks window, click on the Deploy an Application button to start this deployment process.
6. |
In the opened Deploy |
Application window, check the Local Packaged File |
|
or Directory That Is Accessible from the Enterprise Server |
|
|
radio button. |
|
7. |
Click on the Browse |
Files button to scan and find our target application file, |
|
JavaEEDBManufacturer.ear, which is located at the folder C:\Book9\ |
|
|
DBProjects\Chapter 5\JavaEEDBManufacturer\dist. Click on the Choose |
|
|
File button to select this file. |
|
8. |
Check any desired checkboxes, such as Force Redeploy and Java Web Start to |
|
|
set up your desired deploying environment. |
|
9. |
Click on the OK button to begin this deployment process. |
|
If this deployment is successful, the successful deployment page should be displayed, and check the deployment application JavaEEDBManufacturer from the Name column, the detailed deployment result is displayed, as shown in Figure 5.78.
Now let’s do some sophisticated jobs to make our data query result look better.
Figure 5.78. The deployment result.
5.3 Exploring NetBeans IDE 6.8 241
Figure 5.79. Drag the JSF Form From Entity from the Palette window.
5.3.5.11 Creating the Manufacturer Details Web Page
Now we will handle the second issue to create the page where the details of the selected manufacturer are displayed. Perform the following operations to complete this displaying:
1.In the Projects window, right click on the Web project, JavaEEDBManufacturerwar, and select New > JSF Page, specify ManufacturerDetails as the File Name and JSP File under the Options. Click on the Finish button to complete this process.
2.In the code window, drag the item, JSF Form from Entity from the Palette window and drop it in between the <body> </body> tags of the newly generated file,
ManufacturerDetails.jsp, as shown in Figure 5.79.
3.In the JSF Form From Entity dialog, select our entity class file com.javaeedbmanufacturer.entity.Manufacturer from the Entity combo box and manufacturer.details from the Managed Bean Property combo box, as shown in Figure 5.80. Click on the OK button to complete this process. Notice the result of this are lines of codes automatically generated to display label and input field of all the attributes in Manufacturer object in a two-column grid.
To enable the navigation from the Listing page to the Details and vice versa, we need to create and edit the faces-config.xml with the PageFlow editor and connect these two pages together.
First, let’s have a clear picture and idea about the faces-config.xml file and the
PageFlow editor.
242 Chapter 5 Introduction to NetBeans IDE
Figure 5.80. The finished JSF Form From Entity dialog box.
5.3.5.12 Creating and Editing the faces-config.xml Configuration File
When you create a new Java EE Web application with JSF, the JSF also creates some configuration files, and all Web-related and JSF-related components are included in the following two configuration files:
•web.xml—Contains general Web application configuration file.
•faces-config.xml—Contains the configuration of the JSF application.
The detailed functions for these two configuration files are:
•web.xml: JSF requires the central configuration list web.xml in the directory WEB-INF of the application. This is similar to other web applications which are based on Servlets.
You must specify in web.xml that a FacesServlet is responsible for handling JSF applications. FacesServlet is the central controller for the JSF application, and it receives all requests for the JSF application and initializes the JSF components before the JSP is displayed.
•faces-config.xml: The faces-config.xml file allows the JSF to configure the application, managed beans, convertors, validators, and navigation.
The NetBeans IDE provides two distinct views for the faces-config.xml file: the XML View, which displays the XML source code, and the PageFlow view, which is a graphical interface that depicts JSF navigation rules defined in the faces-config.xml file.
The PageFlow view displays the navigation relationships between JSF pages, indicating that navigation from one JSF page to another JSF page occurs when response is passed to JSF’s NavigationHandler.
Double clicking on components in the PageFlow view enables you to navigate directly to the source file. Likewise, if you double click on the arrow between the two components, the editor will focus on the navigation rule defined in the faces-config.xml XML view.
Now let’s first create a faces-config.xml file for our JavaEEDBManufacturer application. Perform the following operations to create this configuration file:
1.Right click on our Web application JavaEEDBManufacturer-war and select the New > Other item from the pop-up menu to open the New File dialog.
5.3 Exploring NetBeans IDE 6.8 243
Figure 5.81. The New File dialog box.
Figure 5.82. The created faces-config.xml file.
2. |
Select the JavaServer Faces from the Categories list and JSF Faces |
|
Configuration from the File Types list, as shown in Figure 5.81. Click on the Next |
|
button to continue. |
3. |
In the opened New JSF Faces Configuration dialog, enter faces-config into |
|
the File Name field as the name of this file, and click on the Finish button. |
The newly created faces-config.xml file is opened and shown in Figure 5.82. First, let’s add our managed bean into this faces-config.xml file by perform the
following operations:
1. Right click on any location inside the opened faces-config.xml file, and select Insert > Managed Bean item from the popup menu. In some cases, you may need to close and re-open the NetBeans IDE to have this Insert menu item available.
2.In the opened Add Managed Bean dialog, enter manufacturer into the Bean Name
field, and click the Browse button to open the Find Type dialog. Type Manufacturer
244 Chapter 5 Introduction to NetBeans IDE
Figure 5.83. The finished Add Managed Bean dialog box.
<?xml version='1.0' encoding='UTF-8'?>
<!-- =========== FULL CONFIGURATION FILE ================================== -->
<faces-config version="2.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd">
<managed-bean>
A<managed-bean-name>manufacturer</managed-bean-name>
B<managed-bean-class>com.javaeedbmanufacturer.web.ManufacturerMBean</managed-bean-class>
C<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
</faces-config>
Figure 5.84. The added managed bean to the faces-config.xml file.
to the Type Name field and select the ManufacturerMBean item from the list, and click on the OK button.
3.Select the session from the Scope combo box.
4.You can enter some description for this managed bean into the Bean Description box if you like.
Your finished Add Managed Bean dialog box should match one that is shown in Figure 5.83.
Click on the Add button to add this ManufacturerMBean as our managed bean into this configuration file. Now you can find that the related XML tags have been added into this faces-config.xml file, which is shown in Figure 5.84. The new added codes have been highlighted in bold.
Let’s have a closer look at this piece of new added codes to see how it works.
A.The name of our managed bean, manufacturer, is added to the managed-bean name tag.
B.The class of our managed bean, ManufacturerMBean, with its namespace, is added under the managed-bean-class tag to indicate the managed bean class used in this application.
C.The scope of this managed bean is session and has been added under the managed-bean scope tag.
5.3 Exploring NetBeans IDE 6.8 245
Note: This faces-config.xml file may be located at two locations: (1) under the Web Pages\WEB-INF folder, and (2) under the Configuration Files folder. It is the same file that just resides at the different locations.
To set up navigation relationship between JSF pages, especially between the
ManufacturerList and ManufacturerDetails pages in this application, we need to edit this configuration file using the PageFlow editor.
Now click on the Clean and Build Main Project button to build our modified project to cover the newly added faces-config.xml file.
Then double click on our new added and edited faces-config.xml file from either location to open this file. Click on the PageFlow button to open its page flow view.
Note: In some cases, you may need to close and restart the NetBeans to make this new added faces-config.xml to have the PageFlow view.
In the opened PageFlow view, in total, there are four JSF pages that are displayed in this configuration view: index.jsp, index.xhtml, ManufacturerList.jsp, and ManufacturerDetails.jsp, as shown in Figure 5.85.
To set up the navigation relationships between the ManufacturerList and ManufacturerDetails JSF pages, perform the following operations:
1.Move your cursor to the starting arrow location as shown in Figure 5.85 until a square appears in the ManufacturerList.jsp page object. Then click on this square and drag this stating arrow and point to and stop at the center of the ManufacturerDetails.
1.Drag the starting arrow
and stop at the center of the ManufacturerDetails
2. Drag the starting arrow and stop at the center of the ManufacturerList
Figure 5.85. The opened PageFlow view of the faces-config.xml file.
246 Chapter 5 Introduction to NetBeans IDE
Figure 5.86. The established default navigation link.
Figure 5.87. The finished PageFlow view of the JSF page objects.
jsp, as shown in Figure 5.85-1. A navigation link is established with the default name case1, as shown in Figure 5.86.
2.Double click on the default navigation link case1 and change its name to DETAILS.
3.Perform a similar operation to create another navigation link from the
ManufacturerDetails to the ManufacturerList, as shown in Figure 5.85-2.
4.Double click on the new established link and change its name to LIST. Your finished PageFlow view of two JSF page objects should match one that is shown in Figure 5.87.
Now if you click on the XML button to open the XML view of this faces-config. xml file, you can find that the navigation rules shown in Figure 5.88 have been added into this file. The new added codes have been highlighted in bold.
As shown in Figure 5.88, two navigation rules, which are indicted by A and B, have been added into this configuration file. The first one is from the FacultyList to the
FacultyDetails, and the second is from the FacultyDetails to the FacultyList.
5.3 Exploring NetBeans IDE 6.8 247
<?xml version='1.0' encoding='UTF-8'?>
<!-- =========== FULL CONFIGURATION FILE ================================== -->
<faces-config version="2.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd">
<!-- a normal Managed Bean --> <managed-bean>
<managed-bean-name>CustomerMBean</managed-bean-name> <managed-bean-class>view.CustomerMBean</managed-bean-class> <managed-bean-scope>session</managed-bean-scope>
</managed-bean>
A <navigation-rule> <from-view-id>/ManufacturerList.jsp</from-view-id> <navigation-case>
<from-outcome>DETAILS</from-outcome> <to-view-id>/ManufacturerDetails.jsp</to-view-id>
</navigation-case> </navigation-rule>
B <navigation-rule> <from-view-id>/ManufacturerDetails.jsp</from-view-id> <navigation-case>
<from-outcome>LIST</from-outcome> <to-view-id>/ManufacturerList.jsp</to-view-id>
</navigation-case> </navigation-rule>
</faces-config>
Figure 5.88. The newly added navigation rules.
Notes: the <from-outcome> strings LIST and DETAILS must match the return String of the list() and showDetails() methods defined in the CustomerMBean.
5.3.5.13 Editing the General Web Application Configuration File web.xml
As we mentioned in the last section, the web.xml file contains the central configuration list, including all configuration descriptions about the JSF pages built in the project. To include our new added faces-config.xml configuration file into our project, we need to add some XML tags to this web.xml file to enable system to know that a new edited faces-config.xml file has been added and will be implemented in this project.
Perform the following operations to complete this addition process:
1.Open the web.xml file by double clicking on it from the Projects window. Regularly, this file should be located at the WEB-INF folder or the Configuration Files folder.
2.On the opened web.xml file, add the XML tags that are shown in Figure 5.89 into this configuration file. The new added XML tags have been highlighted in bold.
Your completed web.xml configuration file should match one that is shown in Figure 5.89
Next, let’s modify our ManufacturerList.jsp page to set up a connection relationship with our ManufacturerDetails.jsp page.
248 Chapter 5 Introduction to NetBeans IDE
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchemainstance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<context-param> <param-name>javax.faces.PROJECT_STAGE</param-name> <param-value>Development</param-value>
</context-param>
<context-param> <param-name>javax.faces.CONFIG_FILES</param-name> <param-value>/WEB-INF/faces-config.xml</param-value>
</context-param>
<servlet>
<servlet-name>Faces Servlet</servlet-name> <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> <load-on-startup>1</load-on-startup>
</servlet> <servlet-mapping>
<servlet-name>Faces Servlet</servlet-name> <url-pattern>/faces/*</url-pattern>
</servlet-mapping> <session-config>
<session-timeout> 30
</session-timeout> </session-config> <welcome-file-list>
<welcome-file>faces/index.xhtml</welcome-file> </welcome-file-list>
</web-app>
Figure 5.89. The modified general Web configuration file web.xml.
5.3.5.14 Modifying the JSF Pages to Perform Page Switching
In the last section, we have established the navigation relationships between the
ManufacturerList and the ManufacturerDetails JSF pages using the navigation rules in the faces-config.xml file. In order to trigger those rules and switch from the
ManufacturerList to the ManufacturerDetails page, we need to modify some part of the ManufacturerList page to accomplish this navigation.We want to use the manufacturerID as a connection key or link to display the detailed record for only one manufacturer based on the manufacturerID.
To do this modification, open the ManufacturerList.jsp page from the Projects window and replace the coding line
<h:outputText value=“#{item. manufacturerId}”/>
With the following coding lines
<h:commandLink action=”#{manufacturer.showDetails(item)}” value=“#{item.manufacturerId}”/>
Your modified part on the ManufacturerList.jsp is shown in Figure 5.90.
Next, open the ManufacturerDetails.jsp page and add one JSF tag to the end of this page, as shown in Figure 5.91. The newly added tag has been highlighted in bold. This command button will enable users to switch the ManufacturerDetails page back to the ManufacturerList page, and it has a similar function as that of Back button on a Web browser.
