﻿WEBVTT

1
00:00:01.321 --> 00:00:02.866
<v Narrator>Hey welcome back.</v>

2
00:00:02.866 --> 00:00:05.384
In this video, I'm gonna show you how to call methods

3
00:00:05.384 --> 00:00:06.884
on a Managed Bean.

4
00:00:08.586 --> 00:00:10.753
We'll cover the following topics.

5
00:00:10.753 --> 00:00:13.132
I'll show you how to call methods on a Managed Bean

6
00:00:13.132 --> 00:00:14.465
from a JSF page.

7
00:00:16.814 --> 00:00:21.436
We'll also learn about conditional navigation.

8
00:00:21.436 --> 00:00:25.603
And then we'll wrap it all up with a full JSF page example.

9
00:00:26.593 --> 00:00:28.871
So, we have a lot of good things in store here.

10
00:00:28.871 --> 00:00:31.121
Let's go ahead and dive in.

11
00:00:31.960 --> 00:00:34.158
So, what we'll do here is we'll have a JSF page

12
00:00:34.158 --> 00:00:36.028
that'll call a Managed Bean

13
00:00:36.028 --> 00:00:37.999
and I'll actually call a specific method

14
00:00:37.999 --> 00:00:39.627
on the Managed Bean.

15
00:00:39.627 --> 00:00:41.540
Now this method will execute

16
00:00:41.540 --> 00:00:43.463
and then based on it's own results,

17
00:00:43.463 --> 00:00:46.884
it'll determine which view page it will render.

18
00:00:46.884 --> 00:00:50.621
So, this will be an example of conditional navigation;

19
00:00:50.621 --> 00:00:52.075
the bean is in control as far as

20
00:00:52.075 --> 00:00:53.753
which page will be rendered

21
00:00:53.753 --> 00:00:54.911
and I'll show how this works here

22
00:00:54.911 --> 00:00:57.880
with a complete code example.

23
00:00:57.880 --> 00:00:59.358
Now before we get into the code,

24
00:00:59.358 --> 00:01:01.177
let's take a look at a quick demo.

25
00:01:01.177 --> 00:01:02.545
So, this demo's gonna present the user

26
00:01:02.545 --> 00:01:06.229
with a form of tour options and, based on the selection,

27
00:01:06.229 --> 00:01:08.433
it'll send them to a different JSF page.

28
00:01:08.433 --> 00:01:10.739
So again, conditional navigation.

29
00:01:10.739 --> 00:01:13.489
So, they'll select the tour of City or Country

30
00:01:13.489 --> 00:01:17.733
and it'll go to a different page, accordingly.

31
00:01:17.733 --> 00:01:19.460
So, here's the demo in action.

32
00:01:19.460 --> 00:01:22.646
So, we'll have form where the user can select a tour,

33
00:01:22.646 --> 00:01:24.920
they can select City or Country.

34
00:01:24.920 --> 00:01:28.431
So, in this case we'll select City and hit the submit button

35
00:01:28.431 --> 00:01:30.748
and then it will give the results for the City Tour.

36
00:01:30.748 --> 00:01:33.676
So we'll hit the big City and view some sites.

37
00:01:33.676 --> 00:01:35.421
Alright, so that's the City Tour form.

38
00:01:35.421 --> 00:01:37.492
Now, let's go ahead and click back

39
00:01:37.492 --> 00:01:41.659
and then I can choose a different option here for Country.

40
00:01:43.361 --> 00:01:45.280
And then I'll go ahead and hit the submit button

41
00:01:45.280 --> 00:01:47.126
and, now, this will send us to a different page,

42
00:01:47.126 --> 00:01:48.927
this was for the Country Tour

43
00:01:48.927 --> 00:01:52.184
and here we'll stroll through the countryside.

44
00:01:52.184 --> 00:01:55.720
So, this is a different JSF page that we're rendering,

45
00:01:55.720 --> 00:01:57.408
so the bean is handling the navigation

46
00:01:57.408 --> 00:01:59.641
between two different pages

47
00:01:59.641 --> 00:02:04.556
and we'll see how this works as far as the source code.

48
00:02:04.556 --> 00:02:06.461
Alright, so let's kinda pull it all together,

49
00:02:06.461 --> 00:02:08.945
so let's look at it from a very big picture here.

50
00:02:08.945 --> 00:02:11.517
So, over on the far left, we have our form

51
00:02:11.517 --> 00:02:13.533
that the user selects their information,

52
00:02:13.533 --> 00:02:14.650
they'll hit submit,

53
00:02:14.650 --> 00:02:18.139
it'll go across to our Managed Bean, our TourBean.

54
00:02:18.139 --> 00:02:21.509
So, our TourBean handles the conditional navigation,

55
00:02:21.509 --> 00:02:24.342
so, based on the results, of the form entry,

56
00:02:24.342 --> 00:02:26.719
then we'll actually send the user to either the

57
00:02:26.719 --> 00:02:30.507
City Tour form or Country Tour form.

58
00:02:30.507 --> 00:02:33.142
So again, conditional navigation.

59
00:02:33.142 --> 00:02:35.206
Now, granted, we could keep just one page

60
00:02:35.206 --> 00:02:38.023
and make use of a database and populate it on the fly,

61
00:02:38.023 --> 00:02:40.767
but here, you know, I wanted to just show you how we can use

62
00:02:40.767 --> 00:02:43.622
conditional navigation in a Managed Bean.

63
00:02:43.622 --> 00:02:46.171
We'll get to database work later on in the course,

64
00:02:46.171 --> 00:02:48.893
so here we'll just focus on conditional navigation

65
00:02:48.893 --> 00:02:49.893
in the bean.

66
00:02:52.774 --> 00:02:54.873
Alright, so if you've been following along with me so far,

67
00:02:54.873 --> 00:02:56.664
you know how I love To Do Lists,

68
00:02:56.664 --> 00:02:58.087
so the first thing we need to do is

69
00:02:58.087 --> 00:03:01.387
call the Managed Bean from the JSF page

70
00:03:01.387 --> 00:03:03.301
and then, the second item is,

71
00:03:03.301 --> 00:03:06.481
creating a public method in the Managed Bean,

72
00:03:06.481 --> 00:03:08.748
that's where we'll perform our business logic

73
00:03:08.748 --> 00:03:11.712
and, also, return the name of the view page.

74
00:03:11.712 --> 00:03:13.387
Alright, so we have our work cut out for us.

75
00:03:13.387 --> 00:03:14.470
Let's dig in.

76
00:03:16.441 --> 00:03:18.411
Alright, so our first step is

77
00:03:18.411 --> 00:03:21.146
calling the Managed Bean from the JSF page,

78
00:03:21.146 --> 00:03:22.420
so what we'll do is we'll start off with

79
00:03:22.420 --> 00:03:25.150
building up our JSF form

80
00:03:25.150 --> 00:03:28.071
and then we'll have a reference to the Managed Bean.

81
00:03:28.071 --> 00:03:31.270
So, starting here at the top, we have Select tour,

82
00:03:31.270 --> 00:03:33.647
and then we have selectOneMenu

83
00:03:33.647 --> 00:03:35.687
and then we reference the data,

84
00:03:35.687 --> 00:03:38.520
meaning value tourBean.kindOfTour.

85
00:03:39.431 --> 00:03:42.301
That's our Managed Bean with the drop down entries.

86
00:03:42.301 --> 00:03:44.835
Near the bottom, here, we have our commandButton,

87
00:03:44.835 --> 00:03:45.992
the value of Submit.

88
00:03:45.992 --> 00:03:50.159
Here, for action, we say action=tourBean.startTheTour.

89
00:03:51.069 --> 00:03:53.245
We call the method, in our Managed Bean,

90
00:03:53.245 --> 00:03:55.495
using this snippet of code.

91
00:03:57.975 --> 00:03:59.972
Now, the next step, is creating the method

92
00:03:59.972 --> 00:04:03.233
in the Managed Bean, so, basically, what we need to do is

93
00:04:03.233 --> 00:04:06.269
expose a public method method in the Managed Bean,

94
00:04:06.269 --> 00:04:08.792
we'll perform our business logic and then,

95
00:04:08.792 --> 00:04:12.932
we'll return the name of the destination JSF page.

96
00:04:12.932 --> 00:04:16.802
Alright, let's look at the coding for this.

97
00:04:16.802 --> 00:04:20.038
Alright, so here's our TourBean, it's a Managed Bean.

98
00:04:20.038 --> 00:04:23.472
On line eight, we'll have a private field, kindOfTour.

99
00:04:23.472 --> 00:04:25.074
That information is actually set

100
00:04:25.074 --> 00:04:26.998
when we submit the form data.

101
00:04:26.998 --> 00:04:29.987
Then, we have our JSF submit,

102
00:04:29.987 --> 00:04:33.839
where we'll actually call this method, startTheTour,

103
00:04:33.839 --> 00:04:36.110
so this method will return a string.

104
00:04:36.110 --> 00:04:38.943
The string is gonna be the name of the view page we want

105
00:04:38.943 --> 00:04:40.382
the JSF to render.

106
00:04:40.382 --> 00:04:42.477
So here, starting on line 14,

107
00:04:42.477 --> 00:04:45.185
I just check that value, kindOfTour.

108
00:04:45.185 --> 00:04:47.858
I see if kindOfTour is not equal to null

109
00:04:47.858 --> 00:04:52.025
and kindOfTour equals city, that means return city_tour,

110
00:04:53.878 --> 00:04:56.324
else return country_tour.

111
00:04:56.324 --> 00:05:00.291
Now one thing to note is that we don't have the .xhtml.

112
00:05:00.291 --> 00:05:05.117
Remember, JSF will automatically add the .xhtml extension,

113
00:05:05.117 --> 00:05:08.565
so it'll know which page to render, for this result.

114
00:05:08.565 --> 00:05:12.732
So, that's basically it for this method, startTheTour.

115
00:05:14.567 --> 00:05:16.609
Alright, so we covered a lot of code here

116
00:05:16.609 --> 00:05:19.018
but let's kinda step back and just review everything,

117
00:05:19.018 --> 00:05:21.843
just so we're on track with everything.

118
00:05:21.843 --> 00:05:24.068
So, on the top left, we started off with the

119
00:05:24.068 --> 00:05:26.032
tour_form xhtml,

120
00:05:26.032 --> 00:05:28.648
we called our Managed Bean, called TourBean,

121
00:05:28.648 --> 00:05:31.399
and based on the results, the TourBean will send it

122
00:05:31.399 --> 00:05:35.469
either to the City Tour or the Country Tour page

123
00:05:35.469 --> 00:05:38.238
and that's basically how it works, in the big picture.

124
00:05:38.238 --> 00:05:39.836
Now, let's go ahead and switch over

125
00:05:39.836 --> 00:05:43.110
and look at the coding for it.

126
00:05:43.110 --> 00:05:45.285
Now, what I'll do is I'll move into Eclipse

127
00:05:45.285 --> 00:05:48.016
and I'll take a look at this project called bean-demo.

128
00:05:48.016 --> 00:05:50.860
This bean-demo project is available in the source code

129
00:05:50.860 --> 00:05:54.360
that you downloaded earlier in the course.

130
00:05:56.458 --> 00:05:58.129
So, in this bean-demo project,

131
00:05:58.129 --> 00:06:02.651
I'm gonna move down to the folder called WebContent

132
00:06:02.651 --> 00:06:05.650
and WebContent will have all of my JSF pages.

133
00:06:05.650 --> 00:06:07.483
So, here, I'll start with the first one here,

134
00:06:07.483 --> 00:06:08.816
tour_form.xhtml.

135
00:06:13.320 --> 00:06:16.161
So, in this file, this is basically where we set up the form

136
00:06:16.161 --> 00:06:19.994
and we present the user with the form options.

137
00:06:22.527 --> 00:06:26.277
So, here's our body section, here's our form.

138
00:06:28.665 --> 00:06:30.742
Our form, basically, has two main elements;

139
00:06:30.742 --> 00:06:33.441
it has the menu, the dropdown menu,

140
00:06:33.441 --> 00:06:36.003
and also the submit button.

141
00:06:36.003 --> 00:06:37.913
So, I'll start here with just prompting the user,

142
00:06:37.913 --> 00:06:39.355
Select a tour

143
00:06:39.355 --> 00:06:41.439
and then I'm make use of this selectOneMenu,

144
00:06:41.439 --> 00:06:43.455
which is basically our dropdown list

145
00:06:43.455 --> 00:06:47.538
and I bind it to this value, tourBean.kindOfTour.

146
00:06:48.463 --> 00:06:49.937
So, when this data's submitted,

147
00:06:49.937 --> 00:06:53.416
it'll call the set kindOfTour on that tourBean

148
00:06:53.416 --> 00:06:55.836
and we can use that value inside of our Managed Bean

149
00:06:55.836 --> 00:06:57.846
to determine where we need to process

150
00:06:57.846 --> 00:06:59.612
or where we need to go.

151
00:06:59.612 --> 00:07:03.612
So here, I give two items here, City and Country

152
00:07:05.636 --> 00:07:09.729
and then the important part here is this commandButton.

153
00:07:09.729 --> 00:07:12.740
So, this is where we have our Submit button

154
00:07:12.740 --> 00:07:14.242
and, when we submit,

155
00:07:14.242 --> 00:07:18.223
we're gonna call a specific method in our Managed Bean,

156
00:07:18.223 --> 00:07:22.390
so we're gonna call tourBean.startTheTour().

157
00:07:24.444 --> 00:07:28.935
So, and that's how we call the method in our Managed Bean.

158
00:07:28.935 --> 00:07:30.269
Now, let's go ahead and look at the code

159
00:07:30.269 --> 00:07:32.497
for the Managed Bean, itself.

160
00:07:32.497 --> 00:07:35.580
So, I'll move into my Java Resources,

161
00:07:36.437 --> 00:07:38.687
expand source, the package,

162
00:07:39.672 --> 00:07:43.839
and then I'll look at this file called TourBean.java.

163
00:07:49.940 --> 00:07:53.069
So, in lines five and six, it's a Managed Bean,

164
00:07:53.069 --> 00:07:57.236
public class TourBean, we've seen all that before.

165
00:07:59.828 --> 00:08:02.843
On line eight we have our private field for kindOfTour

166
00:08:02.843 --> 00:08:06.301
and this is actually set when the user submits the form.

167
00:08:06.301 --> 00:08:08.718
A standard no arg constructor

168
00:08:10.774 --> 00:08:12.084
and our getter and setter methods

169
00:08:12.084 --> 00:08:14.334
for that field, kindOfTour,

170
00:08:17.431 --> 00:08:20.517
and then we move down to the real meat of this demo,

171
00:08:20.517 --> 00:08:21.917
or this example.

172
00:08:21.917 --> 00:08:25.417
So, this is the method here, startTheTour.

173
00:08:26.328 --> 00:08:28.781
So, this is a public method, startTheTour.

174
00:08:28.781 --> 00:08:30.911
It returns something of type string

175
00:08:30.911 --> 00:08:33.307
and that's gonna actually be the name of the page

176
00:08:33.307 --> 00:08:35.918
that we want JSF to view.

177
00:08:35.918 --> 00:08:38.410
And so here, I just do a little if statement.

178
00:08:38.410 --> 00:08:42.577
If kindOfTour != null, equals city then return city_tour,

179
00:08:43.990 --> 00:08:46.026
else return coutry_tour.

180
00:08:46.026 --> 00:08:49.202
And again, remember, JSF will automatically append

181
00:08:49.202 --> 00:08:53.006
the .xhtml extension on those string names,

182
00:08:53.006 --> 00:08:55.163
so it'll go to the appropriate page.

183
00:08:55.163 --> 00:08:56.907
And that's really it for our methods,

184
00:08:56.907 --> 00:08:58.806
so it's conditional navigation,

185
00:08:58.806 --> 00:09:02.973
based on some input we'll send the user to a different page.

186
00:09:07.851 --> 00:09:09.200
Alright, so now let's just go back

187
00:09:09.200 --> 00:09:11.268
and let's just take a look at some of the output pages here,

188
00:09:11.268 --> 00:09:12.487
real quick.

189
00:09:12.487 --> 00:09:14.381
I'm not gonna spend much time on them but,

190
00:09:14.381 --> 00:09:18.048
we have our city_tour page, city_tour.xhtml,

191
00:09:20.474 --> 00:09:23.295
just a very standard JSF page,

192
00:09:23.295 --> 00:09:25.446
now Welcome to the City Tour.

193
00:09:25.446 --> 00:09:29.613
And then, we have another page here for country_tour.xhtml.

194
00:09:31.934 --> 00:09:34.340
And, again, just a very standard output page,

195
00:09:34.340 --> 00:09:36.020
hey, Welcome to the Country Tour.

196
00:09:36.020 --> 00:09:38.187
And that's, basically, it.

197
00:09:39.026 --> 00:09:41.931
Alright, so let's go ahead and run the application,

198
00:09:41.931 --> 00:09:44.541
so just go to your tour_form.xhtml.

199
00:09:44.541 --> 00:09:46.152
Just do a right click on it

200
00:09:46.152 --> 00:09:47.735
and select, Run As,

201
00:09:52.018 --> 00:09:54.870
and then say Run on Server.

202
00:09:54.870 --> 00:09:57.866
Now, your system may prompt you for choosing a server,

203
00:09:57.866 --> 00:09:59.692
just go ahead and select all the defaults,

204
00:09:59.692 --> 00:10:00.925
but, once, you make it past that,

205
00:10:00.925 --> 00:10:02.919
you'll make it to this page here,

206
00:10:02.919 --> 00:10:04.778
so this is your JSF page,

207
00:10:04.778 --> 00:10:08.361
you can select a tour with the dropdown options.

208
00:10:08.361 --> 00:10:11.342
So here I'll go ahead and select city, for my tour,

209
00:10:11.342 --> 00:10:12.589
I'll hit submit,

210
00:10:12.589 --> 00:10:14.034
and then I should get the City page,

211
00:10:14.034 --> 00:10:18.455
so I'm viewing the results of the city_tour.xhtml.

212
00:10:18.455 --> 00:10:20.094
I can click on back,

213
00:10:20.094 --> 00:10:21.960
I can choose a different option here,

214
00:10:21.960 --> 00:10:22.793
country,

215
00:10:27.012 --> 00:10:28.545
and then perform a submit,

216
00:10:28.545 --> 00:10:30.026
and now I get the Country Tour.

217
00:10:30.026 --> 00:10:32.490
So again, we're making it to different pages

218
00:10:32.490 --> 00:10:35.741
and so, this is an example of conditional navigation,

219
00:10:35.741 --> 00:10:38.606
so the bean is controlling, or determining,

220
00:10:38.606 --> 00:10:41.028
which output page, we should view,

221
00:10:41.028 --> 00:10:43.852
based on its own, internal, business logic.

222
00:10:43.852 --> 00:10:45.473
So, this is some really good stuff,

223
00:10:45.473 --> 00:10:46.556
so, good job.

224
00:10:50.351 --> 00:10:53.724
Alright, so let's go ahead and wrap up this video.

225
00:10:53.724 --> 00:10:55.234
We covered a lot of good things, here.

226
00:10:55.234 --> 00:10:57.473
We learned how to call methods in a Managed Bean

227
00:10:57.473 --> 00:10:59.100
from a JSF page.

228
00:10:59.100 --> 00:11:02.268
We also learned how to set up conditional navigation

229
00:11:02.268 --> 00:11:04.573
and, we pulled it all together,

230
00:11:04.573 --> 00:11:07.229
with a full JSF page example,

231
00:11:07.229 --> 00:11:09.631
so this is a very good technique for you to add to your

232
00:11:09.631 --> 00:11:10.798
JSF skill set.

233
00:11:11.954 --> 00:11:12.787
Good job.

