This repository was archived by the owner on Jan 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 101
Expand file tree
/
Copy pathcdi-basicexamples003.html
More file actions
639 lines (591 loc) · 20.5 KB
/
Copy pathcdi-basicexamples003.html
File metadata and controls
639 lines (591 loc) · 20.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<title>The guessnumber-cdi CDI Example</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="css/style.css" rel="stylesheet">
<script src="https://use.fontawesome.com/96c4d89611.js"></script>
</head>
<body>
<table id="doc-title" cellspacing="0" cellpadding="0">
<tr>
<td align="left" valign="top">
<b>Java Platform, Enterprise Edition (Java EE) 8</b><br />
<b>The Java EE Tutorial</b>
<!-- <p class="beta">Beta Draft (Pre-General Availability)</p> -->
</td>
</tr>
</table>
<hr />
<table width="90%" id="top-nav" cellspacing="0" cellpadding="0">
<colgroup>
<col width="12%"/>
<col width="12%"/>
<col width="*"/>
</colgroup>
<tr>
<td align="left">
<a href="cdi-basicexamples002.html">
<span class="vector-font"><i class="fa fa-arrow-circle-left" aria-hidden="true"></i></span>
<span style="position:relative;top:-2px;">Previous</span>
</a>
</td>
<td align="left">
<a href="cdi-adv.html">
<span class=" vector-font"><i class="fa fa-arrow-circle-right vector-font" aria-hidden="true"></i></span>
<span style="position:relative;top:-2px;">Next</span>
</a>
</td>
<td align="right">
<a href="toc.html">
<span class=" vector-font"><i class="fa fa-list vector-font" aria-hidden="true"></i></span>
<span style="position:relative;top:-2px;">Contents</span>
</a>
</td>
</tr>
</table>
<div id="preamble">
<div class="sectionbody">
<div class="paragraph">
<p><a id="GJCXV"></a><a id="the-guessnumber-cdi-cdi-example"></a></p>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_the_guessnumber_cdi_cdi_example">The guessnumber-cdi CDI Example</h2>
<div class="sectionbody">
<div class="paragraph">
<p>The <code>guessnumber-cdi</code> example, somewhat more complex than the
<code>simplegreeting</code> example, illustrates the use of producer methods and of
session and application scope. The example is a game in which you try to
guess a number in fewer than ten attempts. It is similar to the
<code>guessnumber-jsf</code> example described in
<a href="jsf-facelets.html#GIEPX">Chapter 8, "Introduction to Facelets"</a>,
except that you can keep guessing until you get the right answer or
until you use up your ten attempts.</p>
</div>
<div class="paragraph">
<p>The example includes four source files, a Facelets page and template,
and configuration files. The configuration files and the template are
the same as those used for the <code>simplegreeting</code> example.</p>
</div>
<div class="paragraph">
<p>The following topics are addressed here:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="#GJDJU">The guessnumber-cdi Source Files</a></p>
</li>
<li>
<p><a href="#GJDON">The Facelets Page</a></p>
</li>
<li>
<p><a href="#GJDPW">Running the guessnumber-cdi Example</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p><a id="GJDJU"></a><a id="the-guessnumber-cdi-source-files"></a></p>
</div>
<div class="sect2">
<h3 id="_the_guessnumber_cdi_source_files">The guessnumber-cdi Source Files</h3>
<div class="paragraph">
<p>The four source files for the <code>guessnumber-cdi</code> example are:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>The <code>@MaxNumber</code> qualifier interface</p>
</li>
<li>
<p>The <code>@Random</code> qualifier interface</p>
</li>
<li>
<p>The <code>Generator</code> managed bean, which defines producer methods</p>
</li>
<li>
<p>The <code>UserNumberBean</code> managed bean</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>The source files are located in the
<code><em>tut-install</em>/examples/cdi/guessnumber-cdi/src/main/java/javaeetutorial/guessnumber</code>
directory.</p>
</div>
<div class="paragraph">
<p><a id="GJDJP"></a><a id="the-maxnumber-and-random-qualifier-interfaces"></a></p>
</div>
<div class="sect3">
<h4 id="_the_maxnumber_and_random_qualifier_interfaces">The @MaxNumber and @Random Qualifier Interfaces</h4>
<div class="paragraph">
<p>The <code>@MaxNumber</code> qualifier interface is defined as follows:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-oac_no_warn" data-lang="oac_no_warn">package guessnumber;
import java.lang.annotation.Documented;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.ElementType.TYPE;
import java.lang.annotation.Retention;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Target;
import javax.inject.Qualifier;
@Target({TYPE, METHOD, PARAMETER, FIELD})
@Retention(RUNTIME)
@Documented
@Qualifier
public @interface MaxNumber {
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>The <code>@Random</code> qualifier interface is defined as follows:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-oac_no_warn" data-lang="oac_no_warn">package guessnumber;
import java.lang.annotation.Documented;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.ElementType.TYPE;
import java.lang.annotation.Retention;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Target;
import javax.inject.Qualifier;
@Target({TYPE, METHOD, PARAMETER, FIELD})
@Retention(RUNTIME)
@Documented
@Qualifier
public @interface Random {
}</code></pre>
</div>
</div>
<div class="paragraph">
<p><a id="GJDJN"></a><a id="the-generator-managed-bean"></a></p>
</div>
</div>
<div class="sect3">
<h4 id="_the_generator_managed_bean">The Generator Managed Bean</h4>
<div class="paragraph">
<p>The <code>Generator</code> managed bean contains the two producer methods for the
application. The bean has the <code>@ApplicationScoped</code> annotation to specify
that its context extends for the duration of the user’s interaction with
the application:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-oac_no_warn" data-lang="oac_no_warn">package guessnumber;
import java.io.Serializable;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Produces;
@ApplicationScoped
public class Generator implements Serializable {
private static final long serialVersionUID = -7213673465118041882L;
private final java.util.Random random =
new java.util.Random( System.currentTimeMillis() );
private final int maxNumber = 100;
java.util.Random getRandom() {
return random;
}
@Produces @Random int next() {
return getRandom().nextInt(maxNumber + 1);
}
@Produces @MaxNumber int getMaxNumber() {
return maxNumber;
}
}</code></pre>
</div>
</div>
<div class="paragraph">
<p><a id="GJDHY"></a><a id="the-usernumberbean-managed-bean"></a></p>
</div>
</div>
<div class="sect3">
<h4 id="_the_usernumberbean_managed_bean">The UserNumberBean Managed Bean</h4>
<div class="paragraph">
<p>The <code>UserNumberBean</code> managed bean, the managed bean for the JavaServer
Faces application, provides the basic logic for the game. This bean does
the following:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>Implements setter and getter methods for the bean fields</p>
</li>
<li>
<p>Injects the two qualifier objects</p>
</li>
<li>
<p>Provides a <code>reset</code> method that allows you to begin a new game after
you complete one</p>
</li>
<li>
<p>Provides a <code>check</code> method that determines whether the user has guessed
the number</p>
</li>
<li>
<p>Provides a <code>validateNumberRange</code> method that determines whether the
user’s input is correct</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>The bean is defined as follows:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-oac_no_warn" data-lang="oac_no_warn">package guessnumber;
import java.io.Serializable;
import javax.annotation.PostConstruct;
import javax.enterprise.context.SessionScoped;
import javax.enterprise.inject.Instance;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
@Named
@SessionScoped
public class UserNumberBean implements Serializable {
private static final long serialVersionUID = -7698506329160109476L;
private int number;
private Integer userNumber;
private int minimum;
private int remainingGuesses;
@MaxNumber
@Inject
private int maxNumber;
private int maximum;
@Random
@Inject
Instance<Integer> randomInt;
public UserNumberBean() {
}
public int getNumber() {
return number;
}
public void setUserNumber(Integer user_number) {
userNumber = user_number;
}
public Integer getUserNumber() {
return userNumber;
}
public int getMaximum() {
return (this.maximum);
}
public void setMaximum(int maximum) {
this.maximum = maximum;
}
public int getMinimum() {
return (this.minimum);
}
public void setMinimum(int minimum) {
this.minimum = minimum;
}
public int getRemainingGuesses() {
return remainingGuesses;
}
public String check() throws InterruptedException {
if (userNumber > number) {
maximum = userNumber - 1;
}
if (userNumber < number) {
minimum = userNumber + 1;
}
if (userNumber == number) {
FacesContext.getCurrentInstance().addMessage(null,
new FacesMessage("Correct!"));
}
remainingGuesses--;
return null;
}
@PostConstruct
public void reset() {
this.minimum = 0;
this.userNumber = 0;
this.remainingGuesses = 10;
this.maximum = maxNumber;
this.number = randomInt.get();
}
public void validateNumberRange(FacesContext context,
UIComponent toValidate,
Object value) {
int input = (Integer) value;
if (input < minimum || input > maximum) {
((UIInput) toValidate).setValid(false);
FacesMessage message = new FacesMessage("Invalid guess");
context.addMessage(toValidate.getClientId(context), message);
}
}
}</code></pre>
</div>
</div>
<div class="paragraph">
<p><a id="GJDON"></a><a id="the-facelets-page"></a></p>
</div>
</div>
</div>
<div class="sect2">
<h3 id="_the_facelets_page">The Facelets Page</h3>
<div class="paragraph">
<p>This example uses the same template that the <code>simplegreeting</code> example
uses. The <code>index.xhtml</code> file, however, is more complex.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-oac_no_warn" data-lang="oac_no_warn"><?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<ui:composition template="/template.xhtml">
<ui:define name="title">Guess My Number</ui:define>
<ui:define name="head">Guess My Number</ui:define>
<ui:define name="content">
<h:form id="GuessMain">
<div style="color: black; font-size: 24px;">
<p>I'm thinking of a number from
<span style="color: blue">#{userNumberBean.minimum}</span>
to
<span style="color: blue">#{userNumberBean.maximum}</span>.
You have
<span style="color: blue">
#{userNumberBean.remainingGuesses}
</span>
guesses.</p>
</div>
<h:panelGrid border="0" columns="5" style="font-size: 18px;">
<h:outputLabel for="inputGuess">Number:</h:outputLabel>
<h:inputText id="inputGuess"
value="#{userNumberBean.userNumber}"
required="true" size="3"
disabled="#{userNumberBean.number eq userNumberBean.userNumber or userNumberBean.remainingGuesses le 0}"
validator="#{userNumberBean.validateNumberRange}">
</h:inputText>
<h:commandButton id="GuessButton" value="Guess"
action="#{userNumberBean.check}"
disabled="#{userNumberBean.number eq userNumberBean.userNumber or userNumberBean.remainingGuesses le 0}"/>
<h:commandButton id="RestartButton" value="Reset"
action="#{userNumberBean.reset}"
immediate="true" />
<h:outputText id="Higher" value="Higher!"
rendered="#{userNumberBean.number gt userNumberBean.userNumber and userNumberBean.userNumber ne 0}"
style="color: #d20005"/>
<h:outputText id="Lower" value="Lower!"
rendered="#{userNumberBean.number lt userNumberBean.userNumber and userNumberBean.userNumber ne 0}"
style="color: #d20005"/>
</h:panelGrid>
<div style="color: #d20005; font-size: 14px;">
<h:messages id="messages" globalOnly="false"/>
</div>
</h:form>
</ui:define>
</ui:composition>
</html></code></pre>
</div>
</div>
<div class="paragraph">
<p>The Facelets page presents the user with the minimum and maximum values
and the number of guesses remaining. The user’s interaction with the
game takes place within the <code>panelGrid</code> table, which contains an input
field, <strong>Guess</strong> and <strong>Reset</strong> buttons, and a field that appears if the guess is
higher or lower than the correct number. Every time the user clicks
<strong>Guess</strong>, the <code>userNumberBean.check</code> method is called to reset the
maximum or minimum value or, if the guess is correct, to generate a
<code>FacesMessage</code> to that effect. The method that determines whether each
guess is valid is <code>userNumberBean.validateNumberRange</code>.</p>
</div>
<div class="paragraph">
<p><a id="GJDPW"></a><a id="running-the-guessnumber-cdi-example"></a></p>
</div>
</div>
<div class="sect2">
<h3 id="_running_the_guessnumber_cdi_example">Running the guessnumber-cdi Example</h3>
<div class="paragraph">
<p>You can use either NetBeans IDE or Maven to build, package, deploy, and
run the <code>guessnumber-cdi</code> application.</p>
</div>
<div class="paragraph">
<p>The following topics are addressed here:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="#GJDPS">To Build, Package, and Deploy the guessnumber-cdi Example
Using NetBeans IDE</a></p>
</li>
<li>
<p><a href="#GJDPR">To Build, Package, and Deploy the guessnumber-cdi Example
Using Maven</a></p>
</li>
<li>
<p><a href="#GJDQB">To Run the guessnumber Example</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p><a id="GJDPS"></a><a id="to-build-package-and-deploy-the-guessnumber-cdi-example-using-netbeans-ide"></a></p>
</div>
<div class="sect3">
<h4 id="_to_build_package_and_deploy_the_guessnumber_cdi_example_using_netbeans_ide">To Build, Package, and Deploy the guessnumber-cdi Example Using NetBeans IDE</h4>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Make sure that GlassFish Server has been started (see
<a href="usingexamples002.html#BNADI">Starting and Stopping GlassFish
Server</a>).</p>
</li>
<li>
<p>From the <strong>File</strong> menu, choose <strong>Open Project</strong>.</p>
</li>
<li>
<p>In the <strong>Open Project</strong> dialog box, navigate to:</p>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-oac_no_warn" data-lang="oac_no_warn">tut-install/examples/cdi</code></pre>
</div>
</div>
</li>
<li>
<p>Select the <code>guessnumber-cdi</code> folder.</p>
</li>
<li>
<p>Click <strong>Open Project</strong>.</p>
</li>
<li>
<p>In the <strong>Projects</strong> tab, right-click the <code>guessnumber-cdi</code> project and
select <strong>Build</strong>.</p>
<div class="paragraph">
<p>This command builds and packages the application into a WAR file,
<code>guessnumber-cdi.war</code>, located in the <code>target</code> directory, and then
deploys it to GlassFish Server.</p>
</div>
</li>
</ol>
</div>
<div class="paragraph">
<p><a id="GJDPR"></a><a id="to-build-package-and-deploy-the-guessnumber-cdi-example-using-maven"></a></p>
</div>
</div>
<div class="sect3">
<h4 id="_to_build_package_and_deploy_the_guessnumber_cdi_example_using_maven">To Build, Package, and Deploy the guessnumber-cdi Example Using Maven</h4>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Make sure that GlassFish Server has been started (see
<a href="usingexamples002.html#BNADI">Starting and Stopping GlassFish
Server</a>).</p>
</li>
<li>
<p>In a terminal window, change to the following directory:</p>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-oac_no_warn" data-lang="oac_no_warn">tut-install/examples/cdi/guessnumber-cdi/</code></pre>
</div>
</div>
</li>
<li>
<p>Enter the following command to deploy the application:</p>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-oac_no_warn" data-lang="oac_no_warn">mvn install</code></pre>
</div>
</div>
<div class="paragraph">
<p>This command builds and packages the application into a WAR file,
<code>guessnumber-cdi.war</code>, located in the <code>target</code> directory, and then
deploys it to GlassFish Server.</p>
</div>
</li>
</ol>
</div>
<div class="paragraph">
<p><a id="GJDQB"></a><a id="to-run-the-guessnumber-example"></a></p>
</div>
</div>
<div class="sect3">
<h4 id="_to_run_the_guessnumber_example">To Run the guessnumber Example</h4>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>In a web browser, enter the following URL:</p>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-oac_no_warn" data-lang="oac_no_warn">http://localhost:8080/guessnumber-cdi</code></pre>
</div>
</div>
<div class="paragraph">
<p>The <strong>Guess My Number</strong> page opens.</p>
</div>
</li>
<li>
<p>On the <strong>Guess My Number</strong> page, enter a number in the <strong>Number</strong> field and
click <strong>Guess</strong>.</p>
<div class="paragraph">
<p>The minimum and maximum values are modified, along with the remaining
number of guesses.</p>
</div>
</li>
<li>
<p>Keep guessing numbers until you get the right answer or run out of
guesses.</p>
<div class="paragraph">
<p>If you get the right answer or run out of guesses, the input field and
<strong>Guess</strong> button are grayed out.</p>
</div>
</li>
<li>
<p>Click <strong>Reset</strong> to play the game again with a new random number.</p>
</li>
</ol>
</div>
</div>
</div>
</div>
</div>
<hr />
<table width="90%" id="bottom-nav" cellspacing="0" cellpadding="0">
<colgroup>
<col width="12%"/>
<col width="12%"/>
<col width="*"/>
</colgroup>
<tr>
<td align="left">
<a href="cdi-basicexamples002.html">
<span class=" vector-font"><i class="fa fa-arrow-circle-left" aria-hidden="true"></i></span>
<span style="position:relative;top:-2px;">Previous</span>
</a>
</td>
<td align="left">
<a href="cdi-adv.html">
<span class="vector-font"><i class="fa fa-arrow-circle-right vector-font" aria-hidden="true"></i></span>
<span style="position:relative;top:-2px;">Next</span>
</a>
</td>
<td align="right">
<a href="toc.html">
<span class="vector-font"><i class="fa fa-list vector-font" aria-hidden="true"></i></span>
<span style="position:relative;top:-2px;">Contents</span>
</a>
</td>
</tr>
</table>
<span id="copyright">
<a href="img/cpyr.adoc">
<img src="img/oracle.gif" height="10px" alt="Oracle Logo" />
<span>Copyright © 2017, Oracle and/or its affiliates. All rights reserved.</span>
</a>
</span>
<!--<p align="center" class="beta">Beta Draft (Pre-General Availability)</p> -->
</body>
</html>