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-basicexamples002.html
More file actions
461 lines (443 loc) · 14.9 KB
/
Copy pathcdi-basicexamples002.html
File metadata and controls
461 lines (443 loc) · 14.9 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<title>The simplegreeting 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-basicexamples001.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-basicexamples003.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="GJBJU"></a><a id="the-simplegreeting-cdi-example"></a></p>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_the_simplegreeting_cdi_example">The simplegreeting CDI Example</h2>
<div class="sectionbody">
<div class="paragraph">
<p>The <code>simplegreeting</code> example illustrates some of the most basic features
of CDI: scopes, qualifiers, bean injection, and accessing a managed bean
in a JavaServer Faces application. When you run the example, you click a
button that presents either a formal or an informal greeting, depending
on how you edited one of the classes. The example includes four source
files, a Facelets page and template, and configuration files.</p>
</div>
<div class="paragraph">
<p>The following topics are addressed here:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="#GJCQS">The simplegreeting Source Files</a></p>
</li>
<li>
<p><a href="#GJDOJ">The Facelets Template and Page</a></p>
</li>
<li>
<p><a href="#GJCYM">Running the simplegreeting Example</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p><a id="GJCQS"></a><a id="the-simplegreeting-source-files"></a></p>
</div>
<div class="sect2">
<h3 id="_the_simplegreeting_source_files">The simplegreeting Source Files</h3>
<div class="paragraph">
<p>The four source files for the <code>simplegreeting</code> example are:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>The default <code>Greeting</code> class, shown in
<a href="cdi-basic005.html#GIZKS">Beans as Injectable Objects</a></p>
</li>
<li>
<p>The <code>@Informal</code> qualifier interface definition and the
<code>InformalGreeting</code> class that implements the interface, both shown in
<a href="cdi-basic006.html#GJBCK">Using Qualifiers</a></p>
</li>
<li>
<p>The <code>Printer</code> managed bean class, which injects one of the two
interfaces, shown in full in <a href="cdi-basic010.html#GJBBP">Adding Setter
and Getter Methods</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>The source files are located in the
<code><em>tut-install</em>/examples/cdi/simplegreeting/src/main/java/javaeetutorial/simplegreeting</code>
directory.</p>
</div>
<div class="paragraph">
<p><a id="GJDOJ"></a><a id="the-facelets-template-and-page"></a></p>
</div>
</div>
<div class="sect2">
<h3 id="_the_facelets_template_and_page">The Facelets Template and Page</h3>
<div class="paragraph">
<p>To use the managed bean in a simple Facelets application:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Use a very simple template file and <code>index.xhtml</code> page.</p>
<div class="paragraph">
<p>The template page, <code>template.xhtml</code>, looks like this:</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:h="http://xmlns.jcp.org/jsf/html"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<h:outputStylesheet library="css" name="default.css"/>
<title><ui:insert name="title">Default Title</ui:insert></title>
</h:head>
<body>
<div id="container">
<div id="header">
<h2><ui:insert name="head">Head</ui:insert></h2>
</div>
<div id="space">
<p></p>
</div>
<div id="content">
<ui:insert name="content"/>
</div>
</div>
</body>
</html></code></pre>
</div>
</div>
</li>
<li>
<p>To create the Facelets page, redefine the title and head, then add a
small form to the content:</p>
<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">Simple Greeting</ui:define>
<ui:define name="head">Simple Greeting</ui:define>
<ui:define name="content">
<h:form id="greetme">
<p><h:outputLabel value="Enter your name: " for="name"/>
<h:inputText id="name" value="#{printer.name}"/></p>
<p><h:commandButton value="Say Hello"
action="#{printer.createSalutation}"/></p>
<p><h:outputText value="#{printer.salutation}"/> </p>
</h:form>
</ui:define>
</ui:composition>
</html></code></pre>
</div>
</div>
<div class="paragraph">
<p>The form asks the user to enter a name. The button is labeled <strong>Say Hello</strong>,
and the action defined for it is to call the <code>createSalutation</code> method
of the <code>Printer</code> managed bean. This method in turn calls the <code>greet</code>
method of the defined <code>Greeting</code> class.</p>
</div>
<div class="paragraph">
<p>The output text for the form is the value of the greeting returned by
the setter method. Depending on whether the default or the <code>@Informal</code>
version of the greeting is injected, this is one of the following, where
<code>name</code> is the name entered by the user:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-oac_no_warn" data-lang="oac_no_warn">Hello, name.
Hi, name!</code></pre>
</div>
</div>
<div class="paragraph">
<p>The Facelets page and template are located in the
<code><em>tut-install</em>/examples/cdi/simplegreeting/src/main/webapp/</code> directory.</p>
</div>
<div class="paragraph">
<p>The simple CSS file that is used by the Facelets page is in the
following location:</p>
</div>
<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/simplegreeting/src/main/webapp/resources/css/default.css</code></pre>
</div>
</div>
</li>
</ol>
</div>
<div class="paragraph">
<p><a id="GJCYM"></a><a id="running-the-simplegreeting-example"></a></p>
</div>
</div>
<div class="sect2">
<h3 id="_running_the_simplegreeting_example">Running the simplegreeting Example</h3>
<div class="paragraph">
<p>You can use either NetBeans IDE or Maven to build, package, deploy, and
run the <code>simplegreeting</code> application.</p>
</div>
<div class="paragraph">
<p>The following topics are addressed here:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="#GJCXP">To Build, Package, and Run the simplegreeting Example
Using NetBeans IDE</a></p>
</li>
<li>
<p><a href="#GJCZT">To Build, Package, and Deploy the simplegreeting Example
Using Maven</a></p>
</li>
<li>
<p><a href="#GJCZE">To Run the simplegreeting Example</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p><a id="GJCXP"></a><a id="to-build-package-and-run-the-simplegreeting-example-using-netbeans-ide"></a></p>
</div>
<div class="sect3">
<h4 id="_to_build_package_and_run_the_simplegreeting_example_using_netbeans_ide">To Build, Package, and Run the simplegreeting 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>simplegreeting</code> folder.</p>
</li>
<li>
<p>Click <strong>Open Project</strong>.</p>
</li>
<li>
<p>To modify the <code>Printer.java</code> file, perform these steps:</p>
<div class="olist loweralpha">
<ol class="loweralpha" type="a">
<li>
<p>Expand the <strong>Source Packages</strong> node.</p>
</li>
<li>
<p>Expand the <code>greetings</code> node.</p>
</li>
<li>
<p>Double-click the <code>Printer.java</code> file.</p>
</li>
<li>
<p>In the editor, comment out the <code>@Informal</code> annotation:</p>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-oac_no_warn" data-lang="oac_no_warn">@Inject
//@Informal
Greeting greeting;</code></pre>
</div>
</div>
</li>
<li>
<p>Save the file.</p>
</li>
</ol>
</div>
</li>
<li>
<p>In the <strong>Projects</strong> tab, right-click the <code>simplegreeting</code> project and
select <strong>Build</strong>.</p>
<div class="paragraph">
<p>This command builds and packages the application into a WAR file,
<code>simplegreeting.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="GJCZT"></a><a id="to-build-package-and-deploy-the-simplegreeting-example-using-maven"></a></p>
</div>
</div>
<div class="sect3">
<h4 id="_to_build_package_and_deploy_the_simplegreeting_example_using_maven">To Build, Package, and Deploy the simplegreeting 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, go 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/simplegreeting/</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>simplegreeting.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="GJCZE"></a><a id="to-run-the-simplegreeting-example"></a></p>
</div>
</div>
<div class="sect3">
<h4 id="_to_run_the_simplegreeting_example">To Run the simplegreeting 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/simplegreeting</code></pre>
</div>
</div>
<div class="paragraph">
<p>The <strong>Simple Greeting</strong> page opens.</p>
</div>
</li>
<li>
<p>Enter a name in the field.</p>
<div class="paragraph">
<p>For example, suppose that you enter <code>Duke</code>.</p>
</div>
</li>
<li>
<p>Click <strong>Say Hello</strong>.</p>
<div class="paragraph">
<p>If you did not modify the <code>Printer.java</code> file, then the following text string
appears below the button:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-oac_no_warn" data-lang="oac_no_warn">Hi, Duke!</code></pre>
</div>
</div>
<div class="paragraph">
<p>If you commented out the <code>@Informal</code> annotation in the <code>Printer.java</code>
file, then the following text string appears below the button:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-oac_no_warn" data-lang="oac_no_warn">Hello, Duke.</code></pre>
</div>
</div>
</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-basicexamples001.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-basicexamples003.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>