Narrator: Hey in this video we're going to cover
the Sample App Architecture.
We'll cover the following topics.
We'll take a look at the application features,
then we'll step back and look at the big picture.
Next we'll discuss the Student Database Utility.
Then we'll look at the Student Controller,
and then finally we'll wrap it all up
by looking at the JSF Page.
Alright so, a lot of good stuff here,
let's go ahead and get started.
So here's just a quick refresher on our application.
So remember, we're building this app
for a fictional company,
or actually a fictional university called FooBar.
We're going to have four main features.
We're going to allow the user to list the students,
add a new student, update and delete a student.
So these are all the core features that you'll need
for a Java database application,
and we'll cover all this in detail.
So here's the big picture.
So we have three main components here.
So starting on the far left we have
a collection of JSF pages,
that we'll use for building our view for the application.
Then in the center we'll have this managed bean,
we'll call it a Student Controller.
Next we'll have this helper class,
which is our Student Database Utility,
and this will connect to the actual database.
Alright, so in the following slides
I'll actually go through each one of these components
and discuss them in detail.
Alright so let's start here with the back,
with our Student Database Utility.
So this is simply a helper class,
that's going to be responsible for accessing the database.
This helper class is going to make use of JDBC code
to access the database and give us the appropriate data.
Now, this is a very common design pattern.
It's called the Data Accessor Object design pattern, or DAO,
and when you join Enterprise Projects
or large scale projects,
you'll see other developers make use of this pattern,
so again, a very common design pattern.
The main purpose of this pattern
is to isolate or encapsulate your database code
to one specific class,
and then what this does, is that,
this actually promotes object reuse.
So other applications can make use of this helper class
and also it's just one self contained area
of where your database code is located.
So again, common pattern,
a best practice that's used in the industry,
and that's a helper class called
the Data Accessor Object or DAO.
If you want more details on this pattern,
you can simply go to Google,
type in the key word, Data Accessor Object,
and you'll get more details on this pattern,
but we'll make use of this pattern for our application.
Alright so let's take a look some of the methods,
here in this helper class, our Student DB Utility.
Alright so basically again, this class is responsible
for communicating with the database,
and giving us data from the database.
So this class actually makes use of another pattern
called a singleton pattern,
and where basically, we'll simply have
one instance of the class available.
I'll get into more details on the singleton pattern
when we move into the code,
but basically at the top we'll have a method called
getInstance that will give us a handle to
a given instance of this helper class.
Then, moving forward, we'll have some other methods here,
such as getStudent,
so we can get a given student based on their user ID.
Then we'll be able to get a list of students.
And then we have the other methods here for
addStudent, update and deleteStudent,
and those are all pretty straightforward,
as far as what they do or what they provide for us.
Again, we'll walk through all of these methods
in all the follow on videos
and you'll see how they work in detail.
Here I simply wanted to just give you an overview.
Alright, so then we also have this idea
of a Student Controller.
So what's the Student Controller?
Well the Student Controller's basically a managed bean,
and it's our interface between the JSF pages
and our helper class, the Student Database Utility.
So the JSF pages will make use of this managed bean,
they'll call a method like load students,
or add student, or update student,
and the managed bean will simply delegate this call
off to the helper class,
and the helper class will do all the heavy lifting
for the database specific work.
Alright so the Managed Bean here is basically
our interface between the JSF page
and the Student Database Utility.
Alright so we have two methods here,
load students and get students.
So basically I'm going to make use of this pattern,
where I basically load the students into memory
for a given view,
and then we call the get students
to give us a handle to that method.
Now behind the scenes, JSF has something called a
request processing lifecycle.
So, for a given web request,
JSF will call X number of methods on your beans.
If your beans have getter methods,
then JSF could possibly call those methods multiple times,
during a single web request.
So you want to make sure that you don't place
any heavy lifting
or intense code
inside of the getter methods.
You simply want to return an object and that's it.
So I place all of the heavy JDBC work,
inside this load students method.
That's mainly for performance reasons
and kind of real world development,
and I'll talk more about it later,
but anyway that's kind of how that'll work
and I'll show you how this all plays into
the examples with the JSF pages.
Alrighty, so let's go ahead and move forward here,
and let's takes a look at our JSF page.
So, in our JSF page we're going to
create a very simple page.
It's called test-db.xhtml.
At the beginning of the page
we're going to make use of an event listener.
So effectively, what this segment of code will do
is that this code will run only once,
when the page is first loaded.
Alright, so here I have, this f:event,
I have type="preRenderView",
meaning this code will run before the page is rendered,
and I have a listener,
and that's an actual call to a method in my managed bean.
So think of this if you've done
some JavaScript work before,
think of this as like doing an onload.
So Basically, when a page is loaded
then they'll execute this piece of code.
So we basically load the students into memory
for the JSF bean or the managed bean.
Then in our body section, we have the data,
and then we reference studentcontroller.students.
So this will basically call studentcontroller.getstudents.
In our managed bean we simply return
a reference for that object.
So again, this is more of a performance optimization
for real world applications.
So basically again, at the top you'll want to load your data
and then later on in the page, in the body,
you want to reference that data.
Because again, remember JSF could possibly
call those getter methods multiple times
and we want to avoid that.
Alright, so let's kind of pull it all together.
So here we're going to have this example,
that's going to make use of
that test-db page that we just saw,
so test-db.xhtml, that's our JSF page.
This page is going to call
the managed bean Student Controller.
The Student Controller will, in turn,
make use of our helper class, Student DbUtil,
and again remember our Student DbUtil,
is responsible for talking to the database
using the JDBC code.
And that's kind of the big picture,
as far as how this all works together,
and what I'll do is I'll actually move over into Eclipse
and then we'll kind of walk through this process
and then you'll see how it all works together.
But here, I just wanted to make sure
that you understood the big picture,
to see how these different pieces fit together.
Alright, good job.