Narrator: Hey welcome back.
In this video, I'm gonna show you how to call methods
on a Managed Bean.
We'll cover the following topics.
I'll show you how to call methods on a Managed Bean
from a JSF page.
We'll also learn about conditional navigation.
And then we'll wrap it all up with a full JSF page example.
So, we have a lot of good things in store here.
Let's go ahead and dive in.
So, what we'll do here is we'll have a JSF page
that'll call a Managed Bean
and I'll actually call a specific method
on the Managed Bean.
Now this method will execute
and then based on it's own results,
it'll determine which view page it will render.
So, this will be an example of conditional navigation;
the bean is in control as far as
which page will be rendered
and I'll show how this works here
with a complete code example.
Now before we get into the code,
let's take a look at a quick demo.
So, this demo's gonna present the user
with a form of tour options and, based on the selection,
it'll send them to a different JSF page.
So again, conditional navigation.
So, they'll select the tour of City or Country
and it'll go to a different page, accordingly.
So, here's the demo in action.
So, we'll have form where the user can select a tour,
they can select City or Country.
So, in this case we'll select City and hit the submit button
and then it will give the results for the City Tour.
So we'll hit the big City and view some sites.
Alright, so that's the City Tour form.
Now, let's go ahead and click back
and then I can choose a different option here for Country.
And then I'll go ahead and hit the submit button
and, now, this will send us to a different page,
this was for the Country Tour
and here we'll stroll through the countryside.
So, this is a different JSF page that we're rendering,
so the bean is handling the navigation
between two different pages
and we'll see how this works as far as the source code.
Alright, so let's kinda pull it all together,
so let's look at it from a very big picture here.
So, over on the far left, we have our form
that the user selects their information,
they'll hit submit,
it'll go across to our Managed Bean, our TourBean.
So, our TourBean handles the conditional navigation,
so, based on the results, of the form entry,
then we'll actually send the user to either the
City Tour form or Country Tour form.
So again, conditional navigation.
Now, granted, we could keep just one page
and make use of a database and populate it on the fly,
but here, you know, I wanted to just show you how we can use
conditional navigation in a Managed Bean.
We'll get to database work later on in the course,
so here we'll just focus on conditional navigation
in the bean.
Alright, so if you've been following along with me so far,
you know how I love To Do Lists,
so the first thing we need to do is
call the Managed Bean from the JSF page
and then, the second item is,
creating a public method in the Managed Bean,
that's where we'll perform our business logic
and, also, return the name of the view page.
Alright, so we have our work cut out for us.
Let's dig in.
Alright, so our first step is
calling the Managed Bean from the JSF page,
so what we'll do is we'll start off with
building up our JSF form
and then we'll have a reference to the Managed Bean.
So, starting here at the top, we have Select tour,
and then we have selectOneMenu
and then we reference the data,
meaning value tourBean.kindOfTour.
That's our Managed Bean with the drop down entries.
Near the bottom, here, we have our commandButton,
the value of Submit.
Here, for action, we say action=tourBean.startTheTour.
We call the method, in our Managed Bean,
using this snippet of code.
Now, the next step, is creating the method
in the Managed Bean, so, basically, what we need to do is
expose a public method method in the Managed Bean,
we'll perform our business logic and then,
we'll return the name of the destination JSF page.
Alright, let's look at the coding for this.
Alright, so here's our TourBean, it's a Managed Bean.
On line eight, we'll have a private field, kindOfTour.
That information is actually set
when we submit the form data.
Then, we have our JSF submit,
where we'll actually call this method, startTheTour,
so this method will return a string.
The string is gonna be the name of the view page we want
the JSF to render.
So here, starting on line 14,
I just check that value, kindOfTour.
I see if kindOfTour is not equal to null
and kindOfTour equals city, that means return city_tour,
else return country_tour.
Now one thing to note is that we don't have the .xhtml.
Remember, JSF will automatically add the .xhtml extension,
so it'll know which page to render, for this result.
So, that's basically it for this method, startTheTour.
Alright, so we covered a lot of code here
but let's kinda step back and just review everything,
just so we're on track with everything.
So, on the top left, we started off with the
tour_form xhtml,
we called our Managed Bean, called TourBean,
and based on the results, the TourBean will send it
either to the City Tour or the Country Tour page
and that's basically how it works, in the big picture.
Now, let's go ahead and switch over
and look at the coding for it.
Now, what I'll do is I'll move into Eclipse
and I'll take a look at this project called bean-demo.
This bean-demo project is available in the source code
that you downloaded earlier in the course.
So, in this bean-demo project,
I'm gonna move down to the folder called WebContent
and WebContent will have all of my JSF pages.
So, here, I'll start with the first one here,
tour_form.xhtml.
So, in this file, this is basically where we set up the form
and we present the user with the form options.
So, here's our body section, here's our form.
Our form, basically, has two main elements;
it has the menu, the dropdown menu,
and also the submit button.
So, I'll start here with just prompting the user,
Select a tour
and then I'm make use of this selectOneMenu,
which is basically our dropdown list
and I bind it to this value, tourBean.kindOfTour.
So, when this data's submitted,
it'll call the set kindOfTour on that tourBean
and we can use that value inside of our Managed Bean
to determine where we need to process
or where we need to go.
So here, I give two items here, City and Country
and then the important part here is this commandButton.
So, this is where we have our Submit button
and, when we submit,
we're gonna call a specific method in our Managed Bean,
so we're gonna call tourBean.startTheTour().
So, and that's how we call the method in our Managed Bean.
Now, let's go ahead and look at the code
for the Managed Bean, itself.
So, I'll move into my Java Resources,
expand source, the package,
and then I'll look at this file called TourBean.java.
So, in lines five and six, it's a Managed Bean,
public class TourBean, we've seen all that before.
On line eight we have our private field for kindOfTour
and this is actually set when the user submits the form.
A standard no arg constructor
and our getter and setter methods
for that field, kindOfTour,
and then we move down to the real meat of this demo,
or this example.
So, this is the method here, startTheTour.
So, this is a public method, startTheTour.
It returns something of type string
and that's gonna actually be the name of the page
that we want JSF to view.
And so here, I just do a little if statement.
If kindOfTour != null, equals city then return city_tour,
else return coutry_tour.
And again, remember, JSF will automatically append
the .xhtml extension on those string names,
so it'll go to the appropriate page.
And that's really it for our methods,
so it's conditional navigation,
based on some input we'll send the user to a different page.
Alright, so now let's just go back
and let's just take a look at some of the output pages here,
real quick.
I'm not gonna spend much time on them but,
we have our city_tour page, city_tour.xhtml,
just a very standard JSF page,
now Welcome to the City Tour.
And then, we have another page here for country_tour.xhtml.
And, again, just a very standard output page,
hey, Welcome to the Country Tour.
And that's, basically, it.
Alright, so let's go ahead and run the application,
so just go to your tour_form.xhtml.
Just do a right click on it
and select, Run As,
and then say Run on Server.
Now, your system may prompt you for choosing a server,
just go ahead and select all the defaults,
but, once, you make it past that,
you'll make it to this page here,
so this is your JSF page,
you can select a tour with the dropdown options.
So here I'll go ahead and select city, for my tour,
I'll hit submit,
and then I should get the City page,
so I'm viewing the results of the city_tour.xhtml.
I can click on back,
I can choose a different option here,
country,
and then perform a submit,
and now I get the Country Tour.
So again, we're making it to different pages
and so, this is an example of conditional navigation,
so the bean is controlling, or determining,
which output page, we should view,
based on its own, internal, business logic.
So, this is some really good stuff,
so, good job.
Alright, so let's go ahead and wrap up this video.
We covered a lot of good things, here.
We learned how to call methods in a Managed Bean
from a JSF page.
We also learned how to set up conditional navigation
and, we pulled it all together,
with a full JSF page example,
so this is a very good technique for you to add to your
JSF skill set.
Good job.