Advertisement
English transcript
OK, let's go over the blog internals now.
Now there's two different parts of this project
for the most part.
There's the view, made up of these as FTL templates that
are processed by FreeMarker.
And then there's the controller and the model,
which is in Java code and uses a Spark framework for handling
of the routes, in the sense that Spark framework is really
part of the view as well.
And so I'm going to show you those two different parts.
This is the IntelliJ IDE.
You are welcome to use whatever IDE you
want or none at all.
I started by importing the palm file to get in here.
The main class is this BlogController file.
And this BlogController instantiates the other
objects, the session data access object and the user
data access object that handle the session management and the
user data management.
We can see right here at the top, we connect to MongoDB.
We actually called a new BlogController.
And then in there, we connect to MongoDB.
Here's the BlogController right here.
And you can see it calls mongoClient and gets
DB("blog").
It creates the two other data access objects, the user and
session data access objects.
Also sets our port up to be 80, 82.
Now if we look at any given route, we can see here--
this is the initialized route command.
You can see that we're--
let's say if you're looking at the sign-up route, this
handles the post to sign up, but is also a GET request to
sign up, which is in some ways easier to understand.
It creates a new FreeMarker based route, tells it that it
handles the path "signup", and it presents on this
"signup.ftl." template.
And for that one, it just puts a bunch of starting values for
the form elements.
Now over here, we've got these templates, down here.
Let's look at one of these templates, for instance, the
welcome template.
The welcome template.
It welcomes you by name and has a few other links in it.
This is a FreeMarker convention.
So we have in the middle of an HTML page, $, then this curly
brace, and then this literal username, which is used to
look up a value.
So it looks it up in a hash, the hash that was set up
before the template was invoked.
If we look here in BlogController, you can see
here we're presenting the welcome template, this is
actually a GET request.
You can see that it gets the cookie out of the request, and
then it looks up in the sessions, using a session data
access object to find the full username.
If it can't find it, it just says, I'm sorry, you don't
seem to be logged in.
But if it does find it, then it creates a new HashMap,
which it puts in this variable route.
Then it puts a key value pair in.
The key is "username", and the value is the actual username.
And then it calls template.process passing that
HashMap, which the FreeMarker process uses to fill it in
when it combines the data with the template itself.
So for this week you're going to be filling stuff in in the
user data access object that's going to read things out of
the user collection and write things in the user collection.
And I'm not going to show you that class because it has the
code that you'll need to write.
So I want to keep that hidden right now.
If you look here in the blog itself, you can see it's just
this placeholder thing that you saw when you went to the
blog, which is, this is my placeholder for the blog.
And in the subsequent weeks, we're going to fill this in
with more templates that we're going to give you, an
additional class here, so that you can access the posts and
you'll be writing the code to do the MongoDB part of that.
All right.
So that's how the internals of this blog system work.
Now there's two different parts of this project
for the most part.
There's the view, made up of these as FTL templates that
are processed by FreeMarker.
And then there's the controller and the model,
which is in Java code and uses a Spark framework for handling
of the routes, in the sense that Spark framework is really
part of the view as well.
And so I'm going to show you those two different parts.
This is the IntelliJ IDE.
You are welcome to use whatever IDE you
want or none at all.
I started by importing the palm file to get in here.
The main class is this BlogController file.
And this BlogController instantiates the other
objects, the session data access object and the user
data access object that handle the session management and the
user data management.
We can see right here at the top, we connect to MongoDB.
We actually called a new BlogController.
And then in there, we connect to MongoDB.
Here's the BlogController right here.
And you can see it calls mongoClient and gets
DB("blog").
It creates the two other data access objects, the user and
session data access objects.
Also sets our port up to be 80, 82.
Now if we look at any given route, we can see here--
this is the initialized route command.
You can see that we're--
let's say if you're looking at the sign-up route, this
handles the post to sign up, but is also a GET request to
sign up, which is in some ways easier to understand.
It creates a new FreeMarker based route, tells it that it
handles the path "signup", and it presents on this
"signup.ftl." template.
And for that one, it just puts a bunch of starting values for
the form elements.
Now over here, we've got these templates, down here.
Let's look at one of these templates, for instance, the
welcome template.
The welcome template.
It welcomes you by name and has a few other links in it.
This is a FreeMarker convention.
So we have in the middle of an HTML page, $, then this curly
brace, and then this literal username, which is used to
look up a value.
So it looks it up in a hash, the hash that was set up
before the template was invoked.
If we look here in BlogController, you can see
here we're presenting the welcome template, this is
actually a GET request.
You can see that it gets the cookie out of the request, and
then it looks up in the sessions, using a session data
access object to find the full username.
If it can't find it, it just says, I'm sorry, you don't
seem to be logged in.
But if it does find it, then it creates a new HashMap,
which it puts in this variable route.
Then it puts a key value pair in.
The key is "username", and the value is the actual username.
And then it calls template.process passing that
HashMap, which the FreeMarker process uses to fill it in
when it combines the data with the template itself.
So for this week you're going to be filling stuff in in the
user data access object that's going to read things out of
the user collection and write things in the user collection.
And I'm not going to show you that class because it has the
code that you'll need to write.
So I want to keep that hidden right now.
If you look here in the blog itself, you can see it's just
this placeholder thing that you saw when you went to the
blog, which is, this is my placeholder for the blog.
And in the subsequent weeks, we're going to fill this in
with more templates that we're going to give you, an
additional class here, so that you can access the posts and
you'll be writing the code to do the MongoDB part of that.
All right.
So that's how the internals of this blog system work.