JAVA RUNTIME OBJECTS METHOD
8.6 BUILD JAVA WEB PROJECT TO ACCESS AND MANIPULATE ORACLE DATABASE
8.6.8 Query the Faculty Table Using JavaServer Faces and
First, let ’ s build our Web page FacultyPage.jsp fi le.
Recalled that in Section 8.4.3.3 , we discussed how to build the Faculty.jsp page and saved that page in the local folder, such as C:\Temp folder. Now we need to modify that page to make it our JSF page FacultyPage.jsp .
8.6.8.1 Modify the Faculty.jsp to Make it Our JSF Page FacultyPage.jsp
Open the Windows Explorer and browse to our Temp folder to locate the Faculty.jsp page fi le. Copy that fi le and save it to our current project folder JavaWebDBJSPOracle that should be located at the folder C:\Book9\Chapter 8 . You can also fi nd this page fi le from the folder JSP Files that is located at the Wiley ftp site (refer to Figure 1.2 in Chapter 1).
Then launch the NetBeans IDE and our project, JavaWebDBJSPOracle . Then open the Files window, fi nd and copy the Faculty.jsp fi le from the Files window, and paste it to the Web Pages node in the Projects window.
Now perform the following operations to create our JSF page FacultyPage.jsp fi le and modify the Faculty.jsp fi le to make it our JSF page fi le.
1. Right click on our project JavaWebDBJSPOracle and select New > JSF Page item to open the New JSF File wizard.
2. Enter FacultyPage into the File Name fi eld and check the JSP File radio button under the Options group.
3. Click on the Finish button to complete this JSF page creation process.
4. On the opened JSF page, remove the <h:outputText value =″Hello World! ″/ >
tag.
5. Open the Palette window by going to Window > Palette menu item.
6. Drag a JSF Form from the Palette window, which is located under the JSF group, and place it under the <body > tag in our newly created JSF page. Your JSF page should match the one that is shown in Figure 8.132 .
7. Open the Faculty.jsp fi le and copy the codes between the <head > and </head > tag pair and paste them under the <head > tag in our new JSF page FacultyPage.jsp .
8. Copy the <![if !pub] > tag that is under the <div style … > tag in the Faculty.jsp fi le and paste it to the new FacultyPage.jsp page, just under the <body > tag.
9. Copy all codes between the <form > and </form > tag from the Faculty.jsp page and paste them into our new JSF page FacultyPage.jsp , exactly between the <h:form > and
</h:form > tag in the FacultyPage.jsp fi le.
10. Copy the <![endif] > tag from the Faculty.jsp fi le, which is located just under the
</form > tag, and paste it to our new JSF page FacultyPage.jsp , exactly just under the
</h:form > tag.
Now perform the following modifi cations to the related attributes of the tags to bind them to the associated properties or methods defi ned in our managed bean class
FacultyMBean.java that will be built in the following section.
A. Modify the margin and style of the labels displayed in this page by changing the following margins and text indent that are located under the / * Style Definitions * / group below the <style > tag:
a. Change the margin - top from 0 pt to − 10 pt. The result is margin - top: - 10pt;
b. Change the margin - bottom from 0 pt to 10 pt. The result is margin - bottom:10pt;
Figure 8.132. The code window of the JSF page FacultyPage.jsp.
c08.indd 712
c08.indd 712 7/20/2011 11:12:35 AM7/20/2011 11:12:35 AM
www.traintelco.com
B. Add an new id attribute for the <h:form > tag, and make this id equal to FacultyPage . The modifi ed <h:form > tag now becomes <h:form id =″FacultyPage ″> .
C. Modify the tag
<div v:shape =″_x0000_s1025 ″ style =′padding:2.88pt 2.88pt 2.88pt 2.88pt ′ class =shape > that is located under the
<table ... > tag and around line 108 to:
<div v:shape =″_x0000_s1025 ″ style =′padding: 20.88 pt 2.88pt 2.88pt 2.88pt ′ class =shape >
D. Replace the following tag, which is located at line 130 in our new JSF page,
<v:imagedata src =″Faculty_files/image295.emz ″ o:title =″<EM PTY >″/ >
with the tag:
<h:graphicImage id =″fImage ″ width =″140 ″ height =″150 ″ value =″#{FacultyMBean.facultyImage} ″/ >
E. Modify the tag <p class =MsoNormal ><span lang =en - US style =′font - weight:bold;language:en - US ′>Faculty Name </span ></p > that is around line 161 by removing the text label Faculty Name . The modifi ed tag is:
<p class =MsoNormal ><span lang =en - US style =′font - weight:bold;language:en - US ′></span ></p >
F. Modify the tag <p class =MsoNormal ><span lang =en - US style =′font - weight:bold;language:en - US ′>Faculty ID </span ></p > that is around line 191 by removing the text label Faculty ID . The modifi ed tag is:
<p class =MsoNormal ><span lang =en - US style =′font - weight:
bold;language:en - US ′></span ></p >
G. In a similar way as we did in steps E and F , remove the text label Name (around line 221),
Title (line 251), Offi ce (line 281), Phone (line 311), College (line 341), and Email (line 371), respectively.
H. Replace the tag <input name =FacultyImageField maxlength =255 size =18 value =″″... ... > with the tag:
<h:inputText id =″facultyImageName ″ value =″#{FacultyMBean.
facultyImageName} ″></h:inputText >
I. Replace the tag <input name =FacultyNameField maxlength =255 size =18 value =″″... ... > with the tags:
<h:outputLabel
style =″position:absolute;left: - 60pt;font - weight:bold;
font - size:10.0pt;language:en - US ″ value =″Faculty Name ″/ >
<h:inputText id =″facultyName ″ value =″#{FacultyMBean.
facultyName} ″></h:inputText >
J. Replace the tag <input name =FacultyIDField maxlength =255 size =21 value =″″ ... ... ... > with the tags:
<h:outputLabel
style =″position:absolute;left: - 50pt;font - weight:bold;
font - size:10.0pt;language:en - US ″ value =″Faculty ID ″/ >
<h:inputText id =″facultyID ″ value =″#{FacultyMBean.
facultyID} ″></h:inputText >
K. Replace the tag <input name =NameField maxlength =255 size =21 value =″″ ... ... ... > with the tags:
<h:outputLabel
style =″position:absolute;left: - 50pt;font - weight:bold;
font - size:10.0pt;language:en - US ″ value =″Name ″/ >
<h:inputText id =″name ″ value =″#{FacultyMBean.
name} ″></h:inputText >
L. Replace the tag <input name =TitleField maxlength =255 size =21 value =″″ ... ... ... > with the tags:
<h:outputLabel
style =″position:absolute;left: - 50pt;font - weight:bold;
font - size:10.0pt;language:en - US ″ value =″Title ″/ >
<h:inputText id =″title ″ value =″#{FacultyMBean.
title} ″></h:inputText >
M. Replace the tag <input name =OfficeField maxlength =255 size =21 value =″″ ... ... ... > with the tags:
<h:outputLabel
style =″position:absolute;left: - 50pt;font - weight:bold;
font - size:10.0pt;language:en - US ″ value =″Office ″/ >
<h:inputText id =″office ″ value =″#{FacultyMBean.
office} ″></h:inputText >
N. Replace the tag <input name =PhoneField maxlength =255 size =21 value =″″ ... ... ... > with the tags:
<h:outputLabel
style =″position:absolute;left: - 50pt;font - weight:bold;
font - size:10.0pt;language:en - US ″ value =″Phone ″/ >
<h:inputText id =″phone ″ value =″#{FacultyMBean.
phone} ″></h:inputText >
O. Replace the tag <input name =CollegeField maxlength =255 size =21 value =″″ ... ... ... > with the tags:
<h:outputLabel
style =″position:absolute;left: - 50pt;font - weight:bold;
font - size:10.0pt;language:en - US ″ value =″College ″/ >
<h:inputText id =″college ″ value =″#{FacultyMBean.
college} ″></h:inputText >
P. Replace the tag <input name =EmailField maxlength =255 size =21 value =″″ ... ... ... > with the tags:
<h:outputLabel
style =″position:absolute;left: - 50pt;font - weight:bold;
font - size:10.0pt;language:en - US ″ value =″Email ″/ >
<h:inputText id =″email ″ value =″#{FacultyMBean.
email} ″></h:inputText >
Q. Replace the tag <input type =submit value =Select v:shapes =″_x0000_
s1044 ″> with the tag <h:commandButton id =″Select ″ action =″# {FacultyMBean.Select} ″ value =″Select ″ / >
c08.indd 714
c08.indd 714 7/20/2011 11:12:35 AM7/20/2011 11:12:35 AM
www.traintelco.com
R. Replace the tag <input type =submit value =Insert v:shapes =″_x0000_
s1045 ″> with the tag <h:commandButton id =″Insert ″ action =″# {FacultyMBean.Insert} ″ value =″Insert ″ / >
S. Replace the tag <input type =submit value =Update v:shapes =″_x0000_
s1046 ″> with the tag <h:commandButton id =″Update ″ action =″# {FacultyMBean.Update} ″ value =″Update ″ / >
T. Replace the tag <input type =submit value =Delete v:shapes =″_x0000_
s1047 ″> with the tag <h:commandButton id =″Delete ″ action =″# {FacultyMBean.Delete} ″ value =″Delete ″ / >
U. Replace the tag <input type =submit value =Back v:shapes =″_x0000_
s1048 ″> with the tag <h:commandButton id =″Back ″ action =″# {FacultyMBean.Back} ″ value =″Back ″ / >
Your fi nished FacultyPage.jsp fi le should match one that is shown in Figure 8.133 . The modifi ed parts have been highlighted in bold.
A complete JSF page FacultyPage.jsp , including the modifi ed codes, can be found from the folder JSP Files that is located at the site: ftp://ftp.wiley.public.ISBN\BaiBook.
You can directly use this page by downloading it from that site and saving that fi le to your project.
Next, we need to create and build the Java managed bean class FacultyMBean.java to handle the business logics and database - related actions against our sample Oracle database using the Hibernate component.
Because of the relatively complex in the faculty data queries, we divide this data action into two parts: the Java managed bean class FacultyMBean.java that is used to manage the data actions, and the Java session bean FacultySessionBean.java that is used to perform the data actions.
First, let ’ s build the session bean class FacultySessionBean to perform the data query and actions against our sample Oracle database.
8.6.8.2 Build the Java Session Bean FacultySessionBean to Handle Data Actions
The Java session bean FacultySessionBean is used to perform the actual data query and actions against our database using the Hibernate API. Perform the following operations to create our Java session bean class FacultySessionBean.java :
1. Right click on our project JavaWebDBJSPOracle from the Projects window, and select the New > Other item to open the New File wizard.
2. Select Java EE from the Categories list and Session Bean from the File Types list, and then click on the Next button.
3. Enter FacultySessionBean into the Class Name fi eld. Select the JavaWebDBJSPOracle from the Package combo box, and check the Stateless radio button from the Session Type group. Your fi nished Name and Location wizard should match one that is shown in Figure 8.134 . Click on the Finish button to complete this session bean creation process.
On the opened FacultySessionBean.java class fi le, perform the following opera- tions to create the codes for this fi le, which is shown in Figure 8.135 .
Figure 8.133. The modifi ed codes for the FacultyPage.jsp fi le.
………
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal {margin-right:0pt;
text-indent:0pt;
margin-top:-10pt;
margin-bottom:10pt;
text-align:left;
………
</head>
<body>
<![if !pub]>
<h:form id="FacultyPage">
………
<div v:shape="_x0000_s1025" style='padding:20.88pt 2.88pt 2.88pt 2.88pt' class=shape>
………
<h:graphicImage id="fImage" width="140" height="150" value="#{FacultyMBean.facultyImage}"/>
………
<p class=MsoNormal><span lang=en-US style='font-weight:bold;language:en-US'></span></p>
………
<p class=MsoNormal><span lang=en-US style='font-weight:bold;language:en-US'></span></p>
………
<p class=MsoNormal><span lang=en-US style='font-weight:bold;language:en-US'></span></p>
………
<h:inputText id="facultyImageName" value="#{FacultyMBean.facultyImageName}">
</h:inputText>
………
<h:outputLabel style="position:absolute;left:-60pt;font-weight:bold; font-size:10.0pt;language:en-US"
value="Faculty Name"/>
<h:inputText id="facultyName" value="#{FacultyMBean.facultyName}"></h:inputText>
………
<h:outputLabel style="position:absolute;left:-50pt;font-weight:bold; font-size:10.0pt;language:en-US"
value="Faculty ID"/>
<h:inputText id="facultyID" value="#{FacultyMBean.facultyID}"></h:inputText>
………
<h:outputLabel style="position:absolute;left:-50pt;font-weight:bold; font-size:10.0pt;language:en-US"
value="Name"/>
<h:inputText id="name" value="#{FacultyMBean.name}"></h:inputText>
………
<h:outputLabel style="position:absolute;left:-50pt;font-weight:bold; font-size:10.0pt;language:en-US"
value="Title"/>
<h:inputText id="title" value="#{FacultyMBean.title}"></h:inputText>
………
<h:outputLabel style="position:absolute;left:-50pt;font-weight:bold; font-size:10.0pt;language:en-US"
value="Office"/>
<h:inputText id="office" value="#{FacultyMBean.office}"></h:inputText>
………
<h:outputLabel style="position:absolute;left:-50pt;font-weight:bold; font-size:10.0pt;language:en-US"
value="Phone"/>
<h:inputText id="phone" value="#{FacultyMBean.phone}"></h:inputText>
………
<h:outputLabel style="position:absolute;left:-50pt;font-weight:bold; font-size:10.0pt;language:en-US"
value="College"/>
<h:inputText id="college" value="#{FacultyMBean.college}"></h:inputText>
………
<h:outputLabel style="position:absolute;left:-50pt;font-weight:bold; font-size:10.0pt;language:en-US"
value="Email"/>
<h:inputText id="email" value="#{FacultyMBean.email}"></h:inputText>
………
<h:commandButton id="Select" action="#{FacultyMBean.Select}" value="Select" />
………
<h:commandButton id="Insert" action="#{FacultyMBean.Insert}" value="Insert" />
………
<h:commandButton id="Update" action="#{FacultyMBean.Update}" value="Update" />
………
<h:commandButton id="Delete" action="#{FacultyMBean.Delete}" value="Delete" />
………
<h:commandButton id="Back" action="#{FacultyMBean.Back}" value="Back" />
………
A
B C D E F G H
I
J
K
L
M
N
O
P
Q R S T U
c08.indd 716
c08.indd 716 7/20/2011 11:12:36 AM7/20/2011 11:12:36 AM
www.traintelco.com
Figure 8.134. The fi nished Name and Location wizard.
Figure 8.135. The codes for the Java session bean class FacultySessionBean.
package JavaWebDBJSPOracle;
import cse.entity.Faculty;
import cse.util.HibernateUtil;
import java.util.List;
import javax.ejb.Stateless;
import org.hibernate.Query;
import org.hibernate.Session;
@Stateless
public class FacultySessionBean { public Session session = null;
public FacultySessionBean() {
this.session = HibernateUtil.getSessionFactory().getCurrentSession();
}
public List QueryFaculty(String fname) { List<Faculty> facultyList = null;
MsgDialog msgDlg = new MsgDialog(new javax.swing.JFrame(), true);
this.session = HibernateUtil.getSessionFactory().getCurrentSession();
try {
org.hibernate.Transaction tx = session.beginTransaction();
Query f = session.createQuery ("from Faculty as f where f.facultyName like '"+fname+"'");
facultyList = (List<Faculty>) f.list();
} catch (Exception e) {
msgDlg.setMessage("Query is failed and no matched found!");
msgDlg.setVisible(true);
e.printStackTrace();
return null;
}
return facultyList;
} } A B C D E F G
H I
J
Let ’ s have a closer look at this piece of codes to see how it works.
A. Add a new Session object as a property to this class since we need to create a new session object to perform the data query using the Hibernate later.
B. Right click on any space of this code window and select the Insert Code item. Select the Constructor item to create a constructor for this class.
C. Inside the class constructor, call the getCurrentSession() method to obtain the current session object and assign it to our session property created at step A above.
D. Right click on any space inside this code window and select the Insert Code from the pop - up menu. Then select the Add Business Method item to open the Add Business Method wizard. Enter QueryFaculty into the Name fi eld and click on the Browse button to fi nd the returning data type for this method. On the opened wizard, type List into the top fi eld and select List (java.util) from the bottom list. Then click on the OK button, and the OK button again to create this new method. Add a String argument fname to this method.
E. Create two local variables for this method: facultyList , which is a java.util.List object, and a msgDlg, which is a JDialog object. The fi rst is used to hold the returned queried result from the Faculty table, and the latter is used to display the debug information.
F. Before the query can be executed, the getCurrentSession() method is executed again to make sure that a valid session object has been opened.
G. A try … catch block is used to perform the faculty information query from our Faculty table. First, the beginTransaction() method is executed to create a new transaction object.
Then the createQuery() method is called to perform this data query. A HQL statement works as an argument of this method and provides the query details.
H. The list() method is executed to perform this actual query operation. The queried result is returned and assigned to the local variable facultyList .
I. The catch block is used to track and detect any possible exception during this query opera- tion, and display any error if any exception occurred. A null will be returned to the calling method if any exception occurred.
J. Finally, the queried result is returned to the calling method defi ned in our Java managed bean for further processing.
During the coding process, you may encounter some real - time compiling errors, which are indicated with some red underscores for the error sources. Most of these errors are related to the missed packages. To fi x these errors, just right click on any space in this code window, and select the Fix Imports item to open the Fix All Imports wizard.
The point to be noted is that you must select the correct packages for those real - time compiling error sources. For example, in this application, you need to select the following packages or classes for this fi le:
• org.hibernate.Query for the Query class • org.hibernate.Session for the Session class • java.util.List for the List collection class
Now we have completed the coding process for the faculty query operation with the Hibernate API. This piece of codes is only used for the faculty data query process, and the QueryFaculty() method will be called by our Java managed bean class FacultyMBean
c08.indd 718
c08.indd 718 7/20/2011 11:12:36 AM7/20/2011 11:12:36 AM
www.traintelco.com
to execute this data query operation. We will add more methods and codes to perform other data actions, such as data insertion, updating, and deleting, against our sample database later in the following sections.
Next, let ’ s build our Java managed bean class FacultyMBean.java to call some methods defi ned in the session bean to perform the actual data query and actions against our database.
8.6.8.3 Build the Java Managed Bean FacultyMBean to Manage Data Actions The Java managed bean class FacultyMBean is used to manage and coordinate the faculty data queries and actions against our sample Oracle database. The session bean
FacultySessionBean is under the control of this managed bean to perform the actual data actions against our database using the Hibernate API. Perform the following opera- tions to create our Java managed bean class FacultyMBean.java :
1. Right click on our project JavaWebDBJSPOracle from the Projects window, and select the New > Other item to open the New File wizard.
2. Select JavaServer Faces from the Categories list and JSF Managed Bean from the File Types list, and then click on the Next button.
3. Enter FacultyMBean into the Class Name fi eld. Select the JavaWebDBJSPOracle from the Package combo box, and select the session from the Scope combo box. Make sure to check the Add data to confi guration fi le checkbox since we need to use this confi gura- tion fi le to build the page navigation rules later in this application. Your fi nished Name and Location wizard should match one that is shown in Figure 8.136 . Click on the Finish button to complete this managed bean creation process.
Figure 8.136. The fi nished Name and Location wizard.